This commit is contained in:
2024-07-06 04:48:06 +03:00
parent 24df5aaacd
commit d821273160
3 changed files with 37 additions and 17 deletions

View File

@@ -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:])

View File

@@ -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:])

View File

@@ -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:])