diff --git a/click_up/classes/http.py b/click_up/classes/http.py index 1d2299b..2f2744d 100644 --- a/click_up/classes/http.py +++ b/click_up/classes/http.py @@ -1,14 +1,19 @@ import json -from requests import request +from httpx import Client, HTTPTransport + +# Global reusable sync client +sync_http_transport = HTTPTransport(retries=3, http2=True) +http_client = Client(timeout=10, transport=sync_http_transport) class Http: """ A class for making HTTP requests. """ + def __init__(self): - self.headers = { + self.default_headers = { "Content-Type": "application/json", "Accept": "application/json", } @@ -27,11 +32,13 @@ def post(self, url, body, headers, timeout=10): Returns: dict: The response from the server. """ - headers = self.headers | headers + headers = self.default_headers | headers data = json.dumps(body) - result = request( - method="POST", url=url, - headers=headers, data=data, timeout=timeout + result = http_client.post( + url=url, + headers=headers, + json=data, + timeout=timeout ) result.raise_for_status() return result.json() diff --git a/click_up/typing/response/shop_api.py b/click_up/typing/response/shop_api.py index 6f0ed2c..0d3e32f 100644 --- a/click_up/typing/response/shop_api.py +++ b/click_up/typing/response/shop_api.py @@ -3,7 +3,7 @@ @dataclass -class ClickShopApiRespone: +class ClickShopApiResponse: """ Represents a payment transaction in the CLICK system. diff --git a/clickup_fastapi/core/http.py b/clickup_fastapi/core/http.py index 0e63bff..98c6cc4 100644 --- a/clickup_fastapi/core/http.py +++ b/clickup_fastapi/core/http.py @@ -1,27 +1,37 @@ -import httpx -import json +from httpx import AsyncClient, AsyncHTTPTransport + +# Global reusable async client +async_http_transport = AsyncHTTPTransport(retries=3, http2=True) +async_http_client = AsyncClient( + transport=async_http_transport, + timeout=10, +) class Http: def __init__(self): - self.headers = { + self.default_headers = { "Content-Type": "application/json", "Accept": "application/json", } async def post( - self, url: str, body: dict, headers: dict, timeout: int = 10 + self, + url: str, + body: dict, + headers: dict, + timeout: int = 10 ): """ POST so‘rovini asinxron yuborish. """ - headers = self.headers | headers - async with httpx.AsyncClient() as client: - result = await client.post( - url, - headers=headers, - content=json.dumps(body), - timeout=timeout - ) - result.raise_for_status() - return result.json() + headers = self.default_headers | headers + + response = await async_http_client.post( + url, + headers=headers, + json=body, + timeout=timeout, + ) + response.raise_for_status() + return response.json() diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 7e05367..92fe86b 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -1,3 +1,3 @@ -requests==2.* dataclasses==0.* djangorestframework==3.* +httpx[http2]==0.28.* diff --git a/setup.py b/setup.py index 63a8e8a..4e9bf2e 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,7 @@ python_requires='>=3.6', install_requires=[ - 'requests>=2.0,<3.0', + 'httpx[http2]>=0.28.1,<1.0', "dataclasses>=0.6,<1.0; python_version<'3.7'", ], @@ -27,10 +27,11 @@ 'django': [ 'django>=3.0,<5.0', 'djangorestframework>=3.0,<4.0', + 'httpx[http2]>=0.28.1,<1.0', ], 'fastapi': [ 'sqlalchemy>=1.4,<3.0', - 'httpx>=0.20,<1.0', + 'httpx[http2]>=0.28.1,<1.0', 'python-multipart==0.0.20', 'pydantic>=1.8,<2.0', ],