import json import logging from typing import Union import utils from database import Marketplace from limiter import BatchLimiter from marketplaces.base import BaseJsonMarketplace class WildberriesMarketplace(BaseJsonMarketplace): def __init__(self, marketplace: Marketplace): self.marketplace = marketplace auth_data = json.loads(marketplace.auth_data) self.headers = { 'Authorization': auth_data.get('token'), 'Content-Type': 'application/json' } def get_headers(self): return self.headers def api_url(self): return 'https://suppliers-api.wildberries.ru' async def update_stocks(self, data: Union[list, dict]): if type(data) is not list: return max_stocks = 1000 chunks = utils.chunk_list(data, max_stocks) limiter = BatchLimiter(max_requests=300, period=60) for chunk in chunks: try: await limiter.acquire() response = await self._method('PUT', '/api/v3/stocks/{warehouseId}', chunk) if response.status != 204: response = await response.json() error_message = response.get('message') error_code = response.get('code') 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)}')