import logging
from typing import Any, Optional

import aiohttp

logger = logging.getLogger(__name__)


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

    Returns ``None`` when the request fails for any reason.
    """
    try:
        async with session.get(url, params=params, headers=headers) as response:
            response.raise_for_status()
            return await response.json()
    except aiohttp.ClientConnectionError:
        logger.error("Failed to connect, network problem.")
    except aiohttp.ClientResponseError as http_err:
        logger.error("Unsuccessful status code: %s", http_err)
    except TimeoutError:
        logger.error("Request has timed out.")
    except aiohttp.ClientError as client_err:
        logger.error("Request error: %s", client_err)
    return None
