import requests
import zipfile
import io


def _download_data(zip_name, url, fpath):
    """Downloads the specified NREL EFS data for :py:func:`download_demand_data` and
    :py:func:`download_flexibility_data`.

    :param str zip_name: The name of the specified .zip file.
    :param str url: The specified URL to access the desired .zip file.
    :param str fpath: The input file path.
    :return: (*zipfile.ZipFile*) -- The .zip file stored in memory for attempted
        extraction using Python's zipfile module.
    """

    # Save a local copy of the .zip file for extraction
    r = requests.get(url, stream=True)
    if r.status_code != requests.codes.ok:
        r.raise_for_status()
    with open(zip_name, "wb") as f:
        f.write(r.content)
    print(f"{zip_name} successfully downloaded!")

    # Return the data to try extracting with Python's zipfile module
    return zipfile.ZipFile(io.BytesIO(r.content))