import logging
from typing import Any, Optional

import requests
from requests.exceptions import (
    ConnectionError,
    HTTPError,
    RequestException,
    Timeout,
    TooManyRedirects,
)

logger = logging.getLogger(__name__)


def http_get(
    url: str,
    params: Optional[dict] = None,
    headers: Optional[dict] = None,
) -> Optional[Any]:
    """
    Perform an HTTP GET request and return the parsed JSON response.

    Returns ``None`` when the request fails for any reason.
    """
    try:
        response = requests.get(url=url, params=params, headers=headers)
        response.raise_for_status()
        return response.json()
    except ConnectionError:
        logger.error("Failed to connect, network problem.")
    except HTTPError as http_err:
        logger.error("Unsuccessful status code: %s", http_err)
    except Timeout:
        logger.error("Request has timed out.")
    except TooManyRedirects:
        logger.error("Maximum number of redirects exceeded.")
    except RequestException as req_err:
        logger.error("Request error: %s", req_err)
    return None
