import importlib.util
from typing import Any

from ..errors import APIError
from .abstract import AbstractAPIClient


class SyncAPIClient(AbstractAPIClient):

    def _check_required_packages(self) -> None:
        if not importlib.util.find_spec("requests"):
            raise ImportError("You need to install the `requests` package to use sync client")

    def _request(
        self,
        method: str,
        url: str,
        params: dict | None = None,
        headers: dict | None = None,
        json: dict[Any, Any] | list[Any] | None = None,
    ) -> Any:
        import requests  # type: ignore

        response = requests.request(
            method=method,
            url=url,
            headers=headers,
            json=json,
            params=params,
        )
        if not response.ok:
            raise APIError(response.json(), response.url)
        return response.json()
