import requests
from pathlib import Path
import tempfile
import shutil


def fetch(url, name):
    """
    Write content retrieved from provided url to a file.

    :param url: the URL to GET content from.
    :param name: the name of the file to write to in the TMPDIR.

    :returns: the path to the file.
    """
    r = requests.get(url)
    r.raise_for_status()
    path = Path(tempfile.gettempdir(), name)
    with path.open('wb') as f:
        r.raw.decode_content = True
        shutil.copyfileobj(r.raw, f)
    return path