From bc2fef91c4f897397210acd59901aab1418d64ef Mon Sep 17 00:00:00 2001 From: fakz9 Date: Sat, 6 Jul 2024 05:47:11 +0300 Subject: [PATCH] v1.0 --- marketplaces/base.py | 19 ++++++++++++------- marketplaces/ozon.py | 4 ++++ marketplaces/wildberries.py | 4 ++++ marketplaces/yandexmarket.py | 3 +++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/marketplaces/base.py b/marketplaces/base.py index cae9580..8b71c89 100644 --- a/marketplaces/base.py +++ b/marketplaces/base.py @@ -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() + ) diff --git a/marketplaces/ozon.py b/marketplaces/ozon.py index ceeaf06..fbf50de 100644 --- a/marketplaces/ozon.py +++ b/marketplaces/ozon.py @@ -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() diff --git a/marketplaces/wildberries.py b/marketplaces/wildberries.py index 0cb07b6..0892870 100644 --- a/marketplaces/wildberries.py +++ b/marketplaces/wildberries.py @@ -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() diff --git a/marketplaces/yandexmarket.py b/marketplaces/yandexmarket.py index b200da0..72e568d 100644 --- a/marketplaces/yandexmarket.py +++ b/marketplaces/yandexmarket.py @@ -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()