v1.0
This commit is contained in:
@@ -2,7 +2,7 @@ from abc import ABC, abstractmethod
|
|||||||
from typing import Literal, Union
|
from typing import Literal, Union
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aiohttp import ClientResponse, TCPConnector
|
from aiohttp import ClientResponse, TCPConnector, ClientSession
|
||||||
|
|
||||||
from database import Marketplace
|
from database import Marketplace
|
||||||
|
|
||||||
@@ -10,6 +10,8 @@ shared_connector = TCPConnector(limit=50000)
|
|||||||
|
|
||||||
|
|
||||||
class BaseMarketplaceApi(ABC):
|
class BaseMarketplaceApi(ABC):
|
||||||
|
session: ClientSession
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def __init__(self, marketplace: Marketplace):
|
def __init__(self, marketplace: Marketplace):
|
||||||
pass
|
pass
|
||||||
@@ -27,11 +29,14 @@ class BaseMarketplaceApi(ABC):
|
|||||||
def api_url(self):
|
def api_url(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def init_session(self):
|
||||||
|
self.session = ClientSession()
|
||||||
|
|
||||||
async def _method(self, http_method: Literal['POST', 'GET', 'PATCH', 'PUT', 'DELETE'],
|
async def _method(self, http_method: Literal['POST', 'GET', 'PATCH', 'PUT', 'DELETE'],
|
||||||
method: str,
|
method: str,
|
||||||
data: dict) -> ClientResponse:
|
data: dict) -> ClientResponse:
|
||||||
async with aiohttp.ClientSession(connector=shared_connector) as session:
|
return await self.session.request(
|
||||||
return await session.request(http_method,
|
http_method,
|
||||||
f'{self.api_url}{method}',
|
f'{self.api_url}{method}',
|
||||||
json=data,
|
json=data,
|
||||||
headers=self.get_headers()
|
headers=self.get_headers()
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ class OzonMarketplaceApi(BaseMarketplaceApi):
|
|||||||
chunks = utils.chunk_list(data, max_stocks)
|
chunks = utils.chunk_list(data, max_stocks)
|
||||||
if not chunks:
|
if not chunks:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
self.init_session()
|
||||||
limiter = BatchLimiter()
|
limiter = BatchLimiter()
|
||||||
|
|
||||||
async def send_stock_chunk(chunk) -> bool:
|
async def send_stock_chunk(chunk) -> bool:
|
||||||
@@ -69,6 +71,8 @@ class OzonMarketplaceApi(BaseMarketplaceApi):
|
|||||||
first_response = await first_request
|
first_response = await first_request
|
||||||
if not first_response:
|
if not first_response:
|
||||||
logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful')
|
logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful')
|
||||||
|
await self.session.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
await asyncio.gather(*tasks[1:])
|
await asyncio.gather(*tasks[1:])
|
||||||
|
await self.session.close()
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi):
|
|||||||
chunks = utils.chunk_list(data, max_stocks)
|
chunks = utils.chunk_list(data, max_stocks)
|
||||||
if not chunks:
|
if not chunks:
|
||||||
return
|
return
|
||||||
|
self.init_session()
|
||||||
limiter = BatchLimiter()
|
limiter = BatchLimiter()
|
||||||
|
|
||||||
async def send_stock_chunk(chunk):
|
async def send_stock_chunk(chunk):
|
||||||
@@ -67,11 +68,14 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi):
|
|||||||
logging.error(
|
logging.error(
|
||||||
f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}')
|
f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
tasks = [send_stock_chunk(chunk) for chunk in chunks]
|
tasks = [send_stock_chunk(chunk) for chunk in chunks]
|
||||||
first_request = tasks[0]
|
first_request = tasks[0]
|
||||||
first_response = await first_request
|
first_response = await first_request
|
||||||
if not first_response:
|
if not first_response:
|
||||||
logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful')
|
logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful')
|
||||||
|
await self.session.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
await asyncio.gather(*tasks[1:])
|
await asyncio.gather(*tasks[1:])
|
||||||
|
await self.session.close()
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ class YandexmarketMarketplaceApi(BaseMarketplaceApi):
|
|||||||
chunks = chunk_list(data, max_stocks)
|
chunks = chunk_list(data, max_stocks)
|
||||||
if not chunks:
|
if not chunks:
|
||||||
return
|
return
|
||||||
|
self.init_session()
|
||||||
limiter = BatchLimiter()
|
limiter = BatchLimiter()
|
||||||
|
|
||||||
async def send_stock_chunk(chunk):
|
async def send_stock_chunk(chunk):
|
||||||
@@ -71,6 +72,8 @@ class YandexmarketMarketplaceApi(BaseMarketplaceApi):
|
|||||||
first_response = await first_request
|
first_response = await first_request
|
||||||
if not first_response:
|
if not first_response:
|
||||||
logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful')
|
logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful')
|
||||||
|
await self.session.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
await asyncio.gather(*tasks[1:])
|
await asyncio.gather(*tasks[1:])
|
||||||
|
await self.session.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user