import requests


def uri_valid_image(uri):
    """Check if provided `uri` is a valid URI to
    an image, as allowed by the extension whitelist.

    Returns:
        bool: True if `uri` is both an accessible
            URI without any errors *and* the file
            extension is in the whitelist.

    """

    image_extension_whitelist = (".jpg", ".jpeg", ".png", ".gif")

    if not uri.lower().endswith(image_extension_whitelist):

        return False

    # actually fetch the resource to see if it's real or not
    try:
        request = requests.get(uri)
        assert request.status_code == 200

    except (requests.exceptions.InvalidSchema,
            requests.exceptions.MissingSchema,
            requests.exceptions.ConnectionError,
            AssertionError):

        return False

    return True