Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions click_up/classes/http.py
Original file line number Diff line number Diff line change
@@ -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",
}
Expand All @@ -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()
2 changes: 1 addition & 1 deletion click_up/typing/response/shop_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


@dataclass
class ClickShopApiRespone:
class ClickShopApiResponse:
"""
Represents a payment transaction in the CLICK system.

Expand Down
38 changes: 24 additions & 14 deletions clickup_fastapi/core/http.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests==2.*
dataclasses==0.*
djangorestframework==3.*
httpx[http2]==0.28.*
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@
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'",
],

extras_require={
'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',
],
Expand Down