import tempfile
import requests


def file_from_url(url: str):
    """
    file_from_url requests a file from an URL.
    Raises an exception if the request fails.

    Parameters
    ==========

    url : str
        The resource URL.

    Returns
    =======

    _TemporaryFileWrapper
        Requested file as temporary file handler. 
    """
    CHUNK_SIZE = 1024*1024

    file = tempfile.NamedTemporaryFile()
    res = requests.get(url, stream=True)

    if not res.ok:
        raise Exception(
            'request failed with status code {0}'.format(res.status_code))

    for chunk in res.iter_content(chunk_size=CHUNK_SIZE):
        file.write(chunk)

    return file