From d8212731605356711232719a3ecfd01ba8fc8ab9 Mon Sep 17 00:00:00 2001 From: fakz9 Date: Sat, 6 Jul 2024 04:48:06 +0300 Subject: [PATCH] v1.0 --- marketplaces/ozon.py | 19 +++++++++++++------ marketplaces/wildberries.py | 18 ++++++++++++------ marketplaces/yandexmarket.py | 17 ++++++++++++----- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/marketplaces/ozon.py b/marketplaces/ozon.py index f59e758..ceeaf06 100644 --- a/marketplaces/ozon.py +++ b/marketplaces/ozon.py @@ -42,11 +42,11 @@ class OzonMarketplaceApi(BaseMarketplaceApi): return max_stocks = 100 chunks = utils.chunk_list(data, max_stocks) + if not chunks: + return limiter = BatchLimiter() - async def send_stock_chunk(chunk): - if not self.is_valid: - return + async def send_stock_chunk(chunk) -> bool: try: await limiter.acquire_ozon(self.limiter_key) request_data = {'stocks': chunk} @@ -57,11 +57,18 @@ class OzonMarketplaceApi(BaseMarketplaceApi): if error_message: logging.warning( f'Error occurred when sending stocks to [{self.marketplace.id}]: {error_message} ({error_code})') - self.is_valid = False + return False + return True except Exception as e: logging.error( f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}') - self.is_valid = False + return False tasks = [send_stock_chunk(chunk) for chunk in chunks] - await asyncio.gather(*tasks) + first_request = tasks[0] + first_response = await first_request + if not first_response: + logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful') + return + + await asyncio.gather(*tasks[1:]) diff --git a/marketplaces/wildberries.py b/marketplaces/wildberries.py index c4b80df..0cb07b6 100644 --- a/marketplaces/wildberries.py +++ b/marketplaces/wildberries.py @@ -45,11 +45,11 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi): return max_stocks = 1000 chunks = utils.chunk_list(data, max_stocks) + if not chunks: + return limiter = BatchLimiter() async def send_stock_chunk(chunk): - if not self.is_valid: - return try: await limiter.acquire_wildberries(self.limiter_key) request_data = {'stocks': chunk} @@ -61,11 +61,17 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi): error_code = response.get('code') logging.warning( f'Error occurred when sending stocks to [{self.marketplace.id}]: {error_message} ({error_code})') - self.is_valid = False + return False + return True except Exception as e: logging.error( f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}') - self.is_valid = False - + return False tasks = [send_stock_chunk(chunk) for chunk in chunks] - await asyncio.gather(*tasks) + first_request = tasks[0] + first_response = await first_request + if not first_response: + logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful') + return + + await asyncio.gather(*tasks[1:]) diff --git a/marketplaces/yandexmarket.py b/marketplaces/yandexmarket.py index 84758d0..b200da0 100644 --- a/marketplaces/yandexmarket.py +++ b/marketplaces/yandexmarket.py @@ -43,11 +43,11 @@ class YandexmarketMarketplaceApi(BaseMarketplaceApi): campaign_id = self.marketplace.campaign_id max_stocks = 2000 chunks = chunk_list(data, max_stocks) + if not chunks: + return limiter = BatchLimiter() async def send_stock_chunk(chunk): - if not self.is_valid: - return try: await limiter.acquire_yandexmarket(self.limiter_key) request_data = { @@ -59,11 +59,18 @@ class YandexmarketMarketplaceApi(BaseMarketplaceApi): if response.status != 200: logging.warning( f'Error occurred when sending stocks to [{self.marketplace.id}]') - self.is_valid = False + return False + return True except Exception as e: logging.error( f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}') - self.is_valid = False + return False tasks = [send_stock_chunk(chunk) for chunk in chunks] - await asyncio.gather(*tasks) + first_request = tasks[0] + first_response = await first_request + if not first_response: + logging.error(f'Skipping marketplace [{self.marketplace.id}] because first request was unsuccessful') + return + + await asyncio.gather(*tasks[1:])