Skip to content

BaseHttpClient

Bases: ABC

Base interface for all HTTP clients.

You can create your own HTTP client by subclassing this class.

Source code in quickapi/http_clients/base.py
Python
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
class BaseHttpClient(ABC):
    """Base interface for all HTTP clients.

    You can create your own HTTP client by subclassing this class.
    """

    @abstractmethod
    def __init__(self, *args, **kwargs): ...  # type: ignore [no-untyped-def]

    @abstractmethod
    def get(self, *args, **kwargs) -> BaseHttpClientResponse:  # type: ignore [no-untyped-def]
        raise NotImplementedError

    @abstractmethod
    def options(self, *args, **kwargs) -> BaseHttpClientResponse:  # type: ignore [no-untyped-def]
        raise NotImplementedError

    @abstractmethod
    def head(self, *args, **kwargs) -> BaseHttpClientResponse:  # type: ignore [no-untyped-def]
        raise NotImplementedError

    @abstractmethod
    def post(self, *args, **kwargs) -> BaseHttpClientResponse:  # type: ignore [no-untyped-def]
        raise NotImplementedError

    @abstractmethod
    def put(self, *args, **kwargs) -> BaseHttpClientResponse:  # type: ignore [no-untyped-def]
        raise NotImplementedError

    @abstractmethod
    def patch(self, *args, **kwargs) -> BaseHttpClientResponse:  # type: ignore [no-untyped-def]
        raise NotImplementedError

    @abstractmethod
    def delete(self, *args, **kwargs) -> BaseHttpClientResponse:  # type: ignore [no-untyped-def]
        raise NotImplementedError

    def send_request(
        self,
        method: BaseHttpMethod,
        url: str,
        auth: BaseHttpClientAuth,
        params: dict | None,
        json: dict | None,
    ) -> BaseHttpClientResponse:
        match method:
            case BaseHttpMethod.GET:
                client_response = self.get(
                    url=url,
                    auth=auth,
                    params=params,
                )
            case BaseHttpMethod.OPTIONS:
                client_response = self.options(
                    url=url,
                    auth=auth,
                    params=params,
                )
            case BaseHttpMethod.HEAD:
                client_response = self.head(
                    url=url,
                    auth=auth,
                    params=params,
                )
            case BaseHttpMethod.POST:
                client_response = self.post(
                    url=url,
                    auth=auth,
                    params=params,
                    json=json,
                )
            case BaseHttpMethod.PUT:
                client_response = self.put(
                    url=url,
                    auth=auth,
                    params=params,
                    json=json,
                )
            case BaseHttpMethod.PATCH:
                client_response = self.patch(
                    url=url,
                    auth=auth,
                    params=params,
                    json=json,
                )
            case BaseHttpMethod.DELETE:
                client_response = self.delete(
                    url=url,
                    auth=auth,
                    params=params,
                )
            case _:
                raise NotImplementedError(f"Method {method} not implemented.")

        return client_response

BaseHttpMethod

Bases: str, Enum

Supported HTTP methods.

Source code in quickapi/http_clients/types.py
Python
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class BaseHttpMethod(str, Enum):
    """Supported HTTP methods."""

    GET = "GET"
    POST = "POST"
    PUT = "PUT"
    DELETE = "DELETE"
    PATCH = "PATCH"
    OPTIONS = "OPTIONS"
    HEAD = "HEAD"
    TRACE = "TRACE"

    @staticmethod
    def values() -> dict[str, Enum]:
        return BaseHttpMethod._value2member_map_

HTTPx (Default)

HTTPxClient

Bases: BaseHttpClient

A thin wrapper around HTTPx. This is the default client.

Source code in quickapi/http_clients/httpx.py
Python
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class HTTPxClient(BaseHttpClient):
    """A thin wrapper around HTTPx. This is the default client."""

    def __init__(self, client: type[httpx.Client] | None = None):
        self._client = client or httpx.Client()

    def get(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.get(*args, **kwargs)

    def options(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.options(*args, **kwargs)

    def head(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.head(*args, **kwargs)

    def post(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.post(*args, **kwargs)

    def put(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.put(*args, **kwargs)

    def patch(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.patch(*args, **kwargs)

    def delete(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.delete(*args, **kwargs)

Requests

RequestsClient

Bases: BaseHttpClient

A thin wrapper around requests.

This client is only available if the requests library is installed with:

pip install quickapiclient[requests] or poetry add quickapiclient[requests].

Source code in quickapi/http_clients/requests.py
Python
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class RequestsClient(BaseHttpClient):
    """
    A thin wrapper around requests.

    This client is only available if the requests library is installed with:

    `pip install quickapiclient[requests]`
    or `poetry add quickapiclient[requests]`.
    """

    def __init__(self, client: type["requests.sessions.Session"] | None = None):
        if requests_installed is False:
            raise MissingDependencyError(dependency="requests")

        self._client = client or requests.sessions.Session()

    def get(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.get(*args, **kwargs)

    def options(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.options(*args, **kwargs)

    def head(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.head(*args, **kwargs)

    def post(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.post(*args, **kwargs)

    def put(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.put(*args, **kwargs)

    def patch(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.patch(*args, **kwargs)

    def delete(self, *args, **kwargs):  # type: ignore [no-untyped-def]
        return self._client.delete(*args, **kwargs)