This commit is contained in:
2024-07-06 05:47:11 +03:00
parent 2fac36f005
commit bc2fef91c4
4 changed files with 23 additions and 7 deletions

View File

@@ -2,7 +2,7 @@ from abc import ABC, abstractmethod
from typing import Literal, Union
import aiohttp
from aiohttp import ClientResponse, TCPConnector
from aiohttp import ClientResponse, TCPConnector, ClientSession
from database import Marketplace
@@ -10,6 +10,8 @@ shared_connector = TCPConnector(limit=50000)
class BaseMarketplaceApi(ABC):
session: ClientSession
@abstractmethod
def __init__(self, marketplace: Marketplace):
pass
@@ -27,12 +29,15 @@ class BaseMarketplaceApi(ABC):
def api_url(self):
pass
def init_session(self):
self.session = ClientSession()
async def _method(self, http_method: Literal['POST', 'GET', 'PATCH', 'PUT', 'DELETE'],
method: str,
data: dict) -> ClientResponse:
async with aiohttp.ClientSession(connector=shared_connector) as session:
return await session.request(http_method,
f'{self.api_url}{method}',
json=data,
headers=self.get_headers()
)
return await self.session.request(
http_method,
f'{self.api_url}{method}',
json=data,
headers=self.get_headers()
)