123
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from typing import Union
|
||||
@@ -5,10 +6,10 @@ from typing import Union
|
||||
import utils
|
||||
from database import Marketplace
|
||||
from limiter import BatchLimiter
|
||||
from marketplaces.base import BaseJsonMarketplace
|
||||
from marketplaces.base import BaseMarketplaceApi
|
||||
|
||||
|
||||
class WildberriesMarketplace(BaseJsonMarketplace):
|
||||
class WildberriesMarketplaceApi(BaseMarketplaceApi):
|
||||
def __init__(self, marketplace: Marketplace):
|
||||
self.marketplace = marketplace
|
||||
auth_data = json.loads(marketplace.auth_data)
|
||||
@@ -21,6 +22,7 @@ class WildberriesMarketplace(BaseJsonMarketplace):
|
||||
def get_headers(self):
|
||||
return self.headers
|
||||
|
||||
@property
|
||||
def api_url(self):
|
||||
return 'https://suppliers-api.wildberries.ru'
|
||||
|
||||
@@ -29,21 +31,24 @@ class WildberriesMarketplace(BaseJsonMarketplace):
|
||||
return
|
||||
max_stocks = 1000
|
||||
chunks = utils.chunk_list(data, max_stocks)
|
||||
limiter = BatchLimiter(max_requests=300,
|
||||
period=60)
|
||||
for chunk in chunks:
|
||||
limiter = BatchLimiter(max_requests=300, period=60)
|
||||
|
||||
async def send_stock_chunk(chunk):
|
||||
try:
|
||||
await limiter.acquire()
|
||||
response = await self._method('PUT',
|
||||
'/api/v3/stocks/{warehouseId}',
|
||||
chunk)
|
||||
if response.status != 204:
|
||||
request_data = {'stocks': chunk}
|
||||
response = await self._method('PUT', f'/api/v3/stocks/{self.marketplace.warehouse_id}',
|
||||
data=request_data)
|
||||
print(request_data)
|
||||
if response.status not in [204, 409]:
|
||||
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})')
|
||||
break
|
||||
except Exception as e:
|
||||
logging.error(
|
||||
f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}')
|
||||
|
||||
tasks = [send_stock_chunk(chunk) for chunk in chunks]
|
||||
await asyncio.gather(*tasks)
|
||||
|
||||
Reference in New Issue
Block a user