import requests
import tempfile
import tarfile
import io


def extract_tar_gz(response: requests.Response) -> tempfile.TemporaryDirectory:
    """Extract a tar.gz source from a response and return the filepath."""
    temp_dir = tempfile.TemporaryDirectory()

    with tarfile.open(fileobj=io.BytesIO(response.content), mode="r:gz") as tar:
        tar.extractall(temp_dir.name)

    return temp_dir