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()
)

View File

@@ -44,6 +44,8 @@ class OzonMarketplaceApi(BaseMarketplaceApi):
chunks = utils.chunk_list(data, max_stocks)
if not chunks:
return
self.init_session()
limiter = BatchLimiter()
async def send_stock_chunk(chunk) -> bool:
@@ -69,6 +71,8 @@ class OzonMarketplaceApi(BaseMarketplaceApi):
first_response = await first_request
if not first_response:
logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful')
await self.session.close()
return
await asyncio.gather(*tasks[1:])
await self.session.close()

View File

@@ -47,6 +47,7 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi):
chunks = utils.chunk_list(data, max_stocks)
if not chunks:
return
self.init_session()
limiter = BatchLimiter()
async def send_stock_chunk(chunk):
@@ -67,11 +68,14 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi):
logging.error(
f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}')
return False
tasks = [send_stock_chunk(chunk) for chunk in chunks]
first_request = tasks[0]
first_response = await first_request
if not first_response:
logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful')
await self.session.close()
return
await asyncio.gather(*tasks[1:])
await self.session.close()

View File

@@ -45,6 +45,7 @@ class YandexmarketMarketplaceApi(BaseMarketplaceApi):
chunks = chunk_list(data, max_stocks)
if not chunks:
return
self.init_session()
limiter = BatchLimiter()
async def send_stock_chunk(chunk):
@@ -71,6 +72,8 @@ class YandexmarketMarketplaceApi(BaseMarketplaceApi):
first_response = await first_request
if not first_response:
logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful')
await self.session.close()
return
await asyncio.gather(*tasks[1:])
await self.session.close()