import urllib3
import json
import logging


def _get_from_country_code(country_code):
    """Retrieve the IPv4 and IPv6 ranges for the provided country code.

    Args:
        country_code (str): The country code for which the ranges will be retrieved

    Returns:
        ([str], [str]): A tuple of lists (ipv4_blocks, ipv6_blocks)
    """
    try:
        response = urllib3.PoolManager().request(
            "GET",
            "https://stat.ripe.net/data/country-resource-list/data.json?v4_format=prefix&resource={}".format(
                country_code.lower()
            ),
        )
        ripe_json = json.loads(response.data.decode("utf-8"))
    except Exception as e:
        logging.error(
            f"Unable to retrieve list of addresses for country code {country_code} from RIPE: {e}"
        )
        return None, None
    if "data" in ripe_json and "resources" in ripe_json["data"]:
        return (
            ripe_json["data"]["resources"]["ipv4"],
            ripe_json["data"]["resources"]["ipv6"],
        )
    else:
        logging.error("No IP data in response. Malformed or format changed?")
        return None, None