refactor: add conflict handling for stock updates and filter conflicting SKUs
This commit is contained in:
@@ -38,6 +38,15 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi):
|
||||
def api_url(self):
|
||||
return 'https://marketplace-api.wildberries.ru'
|
||||
|
||||
def _filter_chunk_with_conflict(self, chunk: dict, response: list):
|
||||
if not isinstance(response, list):
|
||||
return chunk
|
||||
filter_skus = []
|
||||
for error in response:
|
||||
for sku in error.get('data', []):
|
||||
filter_skus.append(sku['sku'])
|
||||
return list(filter(lambda x: x['sku'] not in filter_skus, chunk))
|
||||
|
||||
async def update_stocks(self, data: Union[list, dict]):
|
||||
if type(data) is not list:
|
||||
return
|
||||
@@ -61,7 +70,7 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi):
|
||||
response = await self._method('PUT', f'/api/v3/stocks/{self.marketplace.warehouse_id}',
|
||||
data=request_data)
|
||||
current_retry += 1
|
||||
if (response.status not in [204, 409]) and (response.status != 429):
|
||||
if (response.status not in [204, 409, 429]):
|
||||
response = await response.json()
|
||||
error_message = response.get('message')
|
||||
error_code = response.get('code')
|
||||
@@ -72,6 +81,14 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi):
|
||||
logging.warning(f'WB rate limit exceeded for marketplace [{self.marketplace.id}]')
|
||||
await asyncio.sleep(1)
|
||||
continue
|
||||
if response.status == 409:
|
||||
response_data = await response.json()
|
||||
|
||||
logging.warning(
|
||||
f'Conflict occurred when sending stocks to [{self.marketplace.id}]: {data}')
|
||||
await asyncio.sleep(1)
|
||||
chunk = self._filter_chunk_with_conflict(chunk, response_data)
|
||||
continue
|
||||
await asyncio.sleep(0.2)
|
||||
break
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user