from json import dump

from requests import get

from constants import FILES, URL


def format_url(dataset) -> str:
    """
    Constructs the API's URL for the requested dataset
    """
    link = URL.format(dataset)
    return link


def save_json(data, dataset):
    """
    Dumps the data into a JSON file
    """
    with open(FILES[dataset], "w") as f:
        dump(data, f, ensure_ascii=False)


def request_dataset(dataset):
    """
    Fetches the requested dataset from opendata's API
    Raises an exception if there's an HTTP error
    """
    url = format_url(dataset)
    response = get(url)
    response.raise_for_status()
    data = response.json()
    save_json(data=data, dataset=dataset)
