diff --git a/marketplaces/wildberries.py b/marketplaces/wildberries.py index 1311977..aeefd1c 100644 --- a/marketplaces/wildberries.py +++ b/marketplaces/wildberries.py @@ -45,34 +45,38 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi): logging.warning(f'Skipping marketplace [{self.marketplace.id}] because of invalid token') return max_stocks = 1000 - chunks = utils.chunk_list(data, max_stocks) + chunks = list(utils.chunk_list(data, max_stocks)) if not chunks: return self.init_session() limiter = BatchLimiter() - async def send_stock_chunk(chunk): + chunk = chunks.pop() + while True: try: + if not chunks: + break await limiter.acquire_wildberries(self.limiter_key) request_data = {'stocks': chunk} response = await self._method('PUT', f'/api/v3/stocks/{self.marketplace.warehouse_id}', data=request_data) - if response.status not in [204, 409]: + if (response.status not in [204, 409]) and (response.status != 429): 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})') - return False - return True + break + if response.status == 429: + await asyncio.sleep(1) + logging.warning(f'Rate limit exceeded for marketplace [{self.marketplace.id}]') + continue + chunk = chunks.pop() + await asyncio.sleep(0.2) + except Exception as e: logging.error( f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}') - return False - - for chunk in chunks: - await send_stock_chunk(chunk) - await asyncio.sleep(0.2) # tasks = [send_stock_chunk(chunk) for chunk in chunks] # first_request = tasks[0] # first_response = await first_request