import requests
import zlib
import json


def get_json_asset(asset_url, asset_name = "file"):
    """
    This function takes a URL of a JSON asset that would return a
    compressed file, decompresses it, and returns it.
    :param asset_url: URL of the compressed JSON asset.
    :returns The JSON asset as a dict. In case of error, it will be
    {"error_message": "string" , "status_code": int}
    """
    print("Getting {} at {}".format(asset_name, asset_url))
    asset_response = requests.get(asset_url)
    if asset_response:
        try:
            decompressed_file = zlib.decompress( \
            asset_response.content, 16+zlib.MAX_WBITS) \
            .decode('utf-8')
            json_asset = json.loads(decompressed_file)
            print("Success")
            return json_asset
        except zlib.error:
          return {"error_message": "Problems decompressing" \
                  , "status_code": asset_response.status_code}
        except json.JSONDecodeError:
          return {"error_message": "Problems decoding from JSON" \
                  , "status_code": asset_response.status_code}
        except:
            return {"error_message": "Unknown Error" \
                  , "status_code": asset_response.status_code}
    return {"error_message": "Error getting from URL" \
            ,"status_code": asset_response.status_code}