This commit is contained in:
2024-07-03 08:11:08 +03:00
parent 7ba3426989
commit c9ddfaf8b4
17 changed files with 751 additions and 42 deletions

View File

@@ -28,6 +28,7 @@ class BaseMarketplaceApi(ABC):
async def _method(self, http_method: Literal['POST', 'GET', 'PATCH', 'PUT', 'DELETE'],
method: str,
data: dict) -> ClientResponse:
return
async with aiohttp.ClientSession() as session:
return await session.request(http_method,
f'{self.api_url}{method}',

View File

@@ -4,6 +4,7 @@ from database import Marketplace
from database.sipro.enums.general import BaseMarketplace
from .wildberries import WildberriesMarketplaceApi
from .ozon import OzonMarketplaceApi
from .yandexmarket import YandexmarketMarketplaceApi
class MarketplaceApiFactory:
@@ -11,9 +12,12 @@ class MarketplaceApiFactory:
def get_marketplace_api(marketplace: Marketplace) -> Union[
WildberriesMarketplaceApi,
OzonMarketplaceApi,
YandexmarketMarketplaceApi
]:
match marketplace.base_marketplace:
case BaseMarketplace.OZON:
return OzonMarketplaceApi(marketplace)
case BaseMarketplace.WILDBERRIES:
return WildberriesMarketplaceApi(marketplace)
case BaseMarketplace.YANDEX_MARKET:
return YandexmarketMarketplaceApi(marketplace)

View File

@@ -0,0 +1,58 @@
import asyncio
import json
import logging
from typing import Union
from backend.config import YANDEX_CLIENT_ID
from database import Marketplace
from limiter import BatchLimiter
from marketplaces.base import BaseMarketplaceApi
from utils import chunk_list
class YandexmarketMarketplaceApi(BaseMarketplaceApi):
def __init__(self, marketplace: Marketplace):
self.marketplace = marketplace
auth_data = json.loads(marketplace.auth_data)
access_token = auth_data.get('accessToken')
self.headers = {
'Authorization': f'OAuth oauth_token="{access_token}", oauth_client_id="{YANDEX_CLIENT_ID}"'
}
def get_headers(self):
return self.headers
@property
def api_url(self):
return 'https://api.partner.market.yandex.ru/v2'
async def update_stocks(self, data: Union[list, dict]):
if type(data) is not list:
return
campaign_id = self.marketplace.campaign_id
max_stocks = 2000
chunks = chunk_list(data, max_stocks)
limiter = BatchLimiter(max_requests=50, period=60)
async def send_stock_chunk(chunk):
try:
await limiter.acquire()
request_data = {
'skus': chunk
}
response = await self._method('PUT',
f'/campaigns/{campaign_id}/offers/stocks',
data=request_data)
print(request_data)
rsp = await response.json()
print(rsp)
if response.status != 200:
logging.warning(
f'Error occurred when sending stocks to [{self.marketplace.id}]')
except Exception as e:
logging.error(
f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}')
tasks = [send_stock_chunk(chunk) for chunk in chunks]
await asyncio.gather(*tasks)