import requests


def image_exists_on_dockerhub(image_name: str, image_tag: str) -> bool:
    """
    Given an image name and image_tag, check if it is
    publicly-accessible on DockerHub.

    Based on the code from this blog post:
    * htttps://ops.tips/blog/inspecting-docker-image-without-pull/

    :param image_name: Name of an image, such as ``continuumio/miniconda3``
    :param image_tag: Tag for the image, such as ``4.8.2``

    :Example:

    .. code-block:: python

        # should be True
        image_exists_on_dockerhub("continuumio/miniconda3", "4.8.2")

        # should be False
        import uuid
        image_exists_on_dockerhub(str(uuid.uuid4()), "0.0.1")
    """
    url = (
        "https://auth.docker.io/token?scope=repository:"
        f"{image_name}:pull&service=registry.docker.io"
    )
    res = requests.get(url=url)
    res.raise_for_status()
    token = res.json()["token"]
    res = requests.get(
        url=f"https://registry-1.docker.io/v2/{image_name}/manifests/{image_tag}",
        headers={
            "Accept": "application/vnd.docker.distribution.manifest.v2+json",
            "Authorization": f"Bearer {token}",
        },
    )
    return res.status_code == 200