This commit is contained in:
2024-07-04 05:22:28 +03:00
parent c9ddfaf8b4
commit c75017c1a8
13 changed files with 189 additions and 553 deletions

View File

@@ -14,9 +14,11 @@ class OzonMarketplaceApi(BaseMarketplaceApi):
def __init__(self, marketplace: Marketplace):
self.marketplace = marketplace
auth_data = json.loads(marketplace.auth_data)
client_id = auth_data.get('clientId')
self.limiter_key = str(marketplace.company_id) + str(client_id)
self.headers = {
'Client-Id': auth_data.get('clientId'),
'Client-Id': client_id,
'Api-Key': auth_data.get('clientToken')
}
@@ -32,14 +34,13 @@ class OzonMarketplaceApi(BaseMarketplaceApi):
return
max_stocks = 100
chunks = utils.chunk_list(data, max_stocks)
limiter = BatchLimiter(max_requests=80, period=60)
limiter = BatchLimiter()
async def send_stock_chunk(chunk):
try:
await limiter.acquire()
await limiter.acquire_ozon(self.limiter_key)
request_data = {'stocks': chunk}
response = await self._method('POST', '/v2/products/stocks', data=request_data)
print(request_data)
response = await response.json()
error_message = response.get('message')
error_code = response.get('code')
@@ -48,7 +49,7 @@ class OzonMarketplaceApi(BaseMarketplaceApi):
f'Error occurred when sending stocks to [{self.marketplace.id}]: {error_message} ({error_code})')
except Exception as e:
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)}')
tasks = [send_stock_chunk(chunk) for chunk in chunks]
await asyncio.gather(*tasks)

View File

@@ -3,6 +3,8 @@ import json
import logging
from typing import Union
import jwt
import utils
from database import Marketplace
from limiter import BatchLimiter
@@ -13,9 +15,16 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi):
def __init__(self, marketplace: Marketplace):
self.marketplace = marketplace
auth_data = json.loads(marketplace.auth_data)
token = auth_data.get('token')
try:
decoded_token = jwt.decode(token, algorithms=["HS256"], options={"verify_signature": False})
except Exception:
logging.error(f"Couldn't decode token for {marketplace.id}")
return None
self.limiter_key = str(marketplace.company_id) + str(decoded_token.get('sid'))
self.headers = {
'Authorization': auth_data.get('token'),
'Authorization': token,
'Content-Type': 'application/json'
}
@@ -31,15 +40,14 @@ class WildberriesMarketplaceApi(BaseMarketplaceApi):
return
max_stocks = 1000
chunks = utils.chunk_list(data, max_stocks)
limiter = BatchLimiter(max_requests=300, period=60)
limiter = BatchLimiter()
async def send_stock_chunk(chunk):
try:
await limiter.acquire()
await limiter.acquire_wildberries(self.limiter_key)
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')

View File

@@ -16,6 +16,8 @@ class YandexmarketMarketplaceApi(BaseMarketplaceApi):
auth_data = json.loads(marketplace.auth_data)
access_token = auth_data.get('accessToken')
self.limiter_key = str(marketplace.company_id) + str(access_token) + str(self.marketplace.campaign_id)
self.headers = {
'Authorization': f'OAuth oauth_token="{access_token}", oauth_client_id="{YANDEX_CLIENT_ID}"'
}
@@ -33,20 +35,17 @@ class YandexmarketMarketplaceApi(BaseMarketplaceApi):
campaign_id = self.marketplace.campaign_id
max_stocks = 2000
chunks = chunk_list(data, max_stocks)
limiter = BatchLimiter(max_requests=50, period=60)
limiter = BatchLimiter()
async def send_stock_chunk(chunk):
try:
await limiter.acquire()
await limiter.acquire_yandexmarket(self.limiter_key)
request_data = {
'skus': chunk
}
response = await self._method('PUT',
f'/campaigns/{campaign_id}/offers/stocks',
data=request_data)
print(request_data)
rsp = await response.json()
print(rsp)
if response.status != 200:
logging.warning(
f'Error occurred when sending stocks to [{self.marketplace.id}]')