123
This commit is contained in:
@@ -1,17 +1,15 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from typing import Union
|
||||
|
||||
from aiolimiter import AsyncLimiter
|
||||
from asynciolimiter import StrictLimiter
|
||||
|
||||
import utils
|
||||
from database import Marketplace
|
||||
from limiter import BatchLimiter
|
||||
from marketplaces.base import BaseJsonMarketplace
|
||||
from marketplaces.base import BaseMarketplaceApi
|
||||
|
||||
|
||||
class OzonMarketplace(BaseJsonMarketplace):
|
||||
class OzonMarketplaceApi(BaseMarketplaceApi):
|
||||
|
||||
def __init__(self, marketplace: Marketplace):
|
||||
self.marketplace = marketplace
|
||||
@@ -25,6 +23,7 @@ class OzonMarketplace(BaseJsonMarketplace):
|
||||
def get_headers(self):
|
||||
return self.headers
|
||||
|
||||
@property
|
||||
def api_url(self):
|
||||
return 'https://api-seller.ozon.ru'
|
||||
|
||||
@@ -33,22 +32,23 @@ class OzonMarketplace(BaseJsonMarketplace):
|
||||
return
|
||||
max_stocks = 100
|
||||
chunks = utils.chunk_list(data, max_stocks)
|
||||
limiter = BatchLimiter(max_requests=80,
|
||||
period=60)
|
||||
for chunk in chunks:
|
||||
limiter = BatchLimiter(max_requests=80, period=60)
|
||||
|
||||
async def send_stock_chunk(chunk):
|
||||
try:
|
||||
await limiter.acquire()
|
||||
response = await self._method('POST',
|
||||
'/v2/products/stocks',
|
||||
data=chunk)
|
||||
request_data = {'stocks': chunk}
|
||||
response = await self._method('POST', '/v2/products/stocks', data=request_data)
|
||||
print(request_data)
|
||||
response = await response.json()
|
||||
# response = await
|
||||
error_message = response.get('message')
|
||||
error_code = response.get('code')
|
||||
if error_message:
|
||||
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