refactor: improve stock sending logic to handle rate limits and chunk processing
This commit is contained in:
@@ -45,34 +45,38 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi):
|
|||||||
logging.warning(f'Skipping marketplace [{self.marketplace.id}] because of invalid token')
|
logging.warning(f'Skipping marketplace [{self.marketplace.id}] because of invalid token')
|
||||||
return
|
return
|
||||||
max_stocks = 1000
|
max_stocks = 1000
|
||||||
chunks = utils.chunk_list(data, max_stocks)
|
chunks = list(utils.chunk_list(data, max_stocks))
|
||||||
if not chunks:
|
if not chunks:
|
||||||
return
|
return
|
||||||
self.init_session()
|
self.init_session()
|
||||||
limiter = BatchLimiter()
|
limiter = BatchLimiter()
|
||||||
|
|
||||||
async def send_stock_chunk(chunk):
|
chunk = chunks.pop()
|
||||||
|
while True:
|
||||||
try:
|
try:
|
||||||
|
if not chunks:
|
||||||
|
break
|
||||||
await limiter.acquire_wildberries(self.limiter_key)
|
await limiter.acquire_wildberries(self.limiter_key)
|
||||||
request_data = {'stocks': chunk}
|
request_data = {'stocks': chunk}
|
||||||
response = await self._method('PUT', f'/api/v3/stocks/{self.marketplace.warehouse_id}',
|
response = await self._method('PUT', f'/api/v3/stocks/{self.marketplace.warehouse_id}',
|
||||||
data=request_data)
|
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()
|
response = await response.json()
|
||||||
error_message = response.get('message')
|
error_message = response.get('message')
|
||||||
error_code = response.get('code')
|
error_code = response.get('code')
|
||||||
logging.warning(
|
logging.warning(
|
||||||
f'Error occurred when sending stocks to [{self.marketplace.id}]: {error_message} ({error_code})')
|
f'Error occurred when sending stocks to [{self.marketplace.id}]: {error_message} ({error_code})')
|
||||||
return False
|
break
|
||||||
return True
|
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:
|
except Exception as e:
|
||||||
logging.error(
|
logging.error(
|
||||||
f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}')
|
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]
|
# tasks = [send_stock_chunk(chunk) for chunk in chunks]
|
||||||
# first_request = tasks[0]
|
# first_request = tasks[0]
|
||||||
# first_response = await first_request
|
# first_response = await first_request
|
||||||
|
|||||||
Reference in New Issue
Block a user