rewritten crap
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from typing import Union
|
||||
|
||||
import utils
|
||||
from aiohttp import ClientResponse
|
||||
|
||||
from database import Marketplace
|
||||
from limiter import BatchLimiter
|
||||
from marketplaces.base import BaseMarketplaceApi
|
||||
|
||||
|
||||
@@ -35,47 +34,5 @@ class OzonMarketplaceApi(BaseMarketplaceApi):
|
||||
def api_url(self):
|
||||
return 'https://api-seller.ozon.ru'
|
||||
|
||||
async def update_stocks(self, data: Union[list, dict]):
|
||||
if type(data) is not list:
|
||||
return
|
||||
if not self.is_valid:
|
||||
return
|
||||
max_stocks = 100
|
||||
chunks = utils.chunk_list(data, max_stocks)
|
||||
if not chunks:
|
||||
return
|
||||
|
||||
self.init_session()
|
||||
limiter = BatchLimiter()
|
||||
max_retries = 10
|
||||
while chunks:
|
||||
current_retry = 0
|
||||
chunk = chunks.pop()
|
||||
while current_retry <= max_retries:
|
||||
try:
|
||||
await limiter.acquire_ozon(self.limiter_key)
|
||||
request_data = {'stocks': chunk}
|
||||
response = await self._method('POST', '/v2/products/stocks', data=request_data)
|
||||
current_retry += 1
|
||||
response = await response.json()
|
||||
error_message = response.get('message')
|
||||
error_code = response.get('code')
|
||||
if error_message:
|
||||
if error_code == 8:
|
||||
logging.warning(f'Ozon rate limit exceeded for marketplace [{self.marketplace.id}]')
|
||||
await asyncio.sleep(1)
|
||||
continue
|
||||
else:
|
||||
logging.warning(
|
||||
f'Error occurred when sending stocks to [{self.marketplace.id}]: {error_message} ({error_code})')
|
||||
break
|
||||
else:
|
||||
break
|
||||
|
||||
except Exception as e:
|
||||
logging.error(
|
||||
f'Exception occurred while sending stocks to marketplace ID [{self.marketplace.id}]: {str(e)}')
|
||||
break
|
||||
|
||||
|
||||
await self.session.close()
|
||||
async def update_stocks(self, data: Union[list, dict]) -> ClientResponse:
|
||||
return await self._method('POST', '/v2/products/stocks', data={'stocks': data})
|
||||
|
||||
Reference in New Issue
Block a user