v1.0
This commit is contained in:
58
marketplaces/yandexmarket.py
Normal file
58
marketplaces/yandexmarket.py
Normal 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)
|
||||
Reference in New Issue
Block a user