Files
Sipro-Stocks/marketplaces/yandexmarket.py
2024-07-04 05:22:28 +03:00

58 lines
2.0 KiB
Python

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.limiter_key = str(marketplace.company_id) + str(access_token) + str(self.marketplace.campaign_id)
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()
async def send_stock_chunk(chunk):
try:
await limiter.acquire_yandexmarket(self.limiter_key)
request_data = {
'skus': chunk
}
response = await self._method('PUT',
f'/campaigns/{campaign_id}/offers/stocks',
data=request_data)
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)