import requests
import warnings


def fetch_url(url, filename, target_dir, rename_file=False):
    """Download filename from url into target_dir."""
    if rename_file:
        url_to_retrieve = url
    else:
        url_to_retrieve = url + filename
    response = requests.get(url_to_retrieve)
    if response.status_code != 200:
        warnings.warn(
            f"Received status {response.status_code} when trying to retrieve {url}{filename}"
        )
    # Save to file
    with open(target_dir / filename, "wb") as f:
        f.write(response.content)
    print(f"Successfully downloaded {filename} into {target_dir.resolve()}")
    return target_dir / filename