Files
Sipro-Stocks/marketplaces/ozon.py
2024-07-01 06:01:50 +03:00

55 lines
1.8 KiB
Python

import json
import logging
from typing import Union
from aiolimiter import AsyncLimiter
from asynciolimiter import StrictLimiter
import utils
from database import Marketplace
from limiter import BatchLimiter
from marketplaces.base import BaseJsonMarketplace
class OzonMarketplace(BaseJsonMarketplace):
def __init__(self, marketplace: Marketplace):
self.marketplace = marketplace
auth_data = json.loads(marketplace.auth_data)
self.headers = {
'Client-Id': auth_data.get('clientId'),
'Api-Key': auth_data.get('clientToken')
}
def get_headers(self):
return self.headers
def api_url(self):
return 'https://api-seller.ozon.ru'
async def update_stocks(self, data: Union[list, dict]):
if type(data) is not list:
return
max_stocks = 100
chunks = utils.chunk_list(data, max_stocks)
limiter = BatchLimiter(max_requests=80,
period=60)
for chunk in chunks:
try:
await limiter.acquire()
response = await self._method('POST',
'/v2/products/stocks',
data=chunk)
response = await response.json()
# response = await
error_message = response.get('message')
error_code = response.get('code')
if error_message:
logging.warning(
f'Error occurred when sending stocks to [{self.marketplace.id}]: {error_message} ({error_code})')
break
except Exception as e:
logging.error(
f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}')