Files
Fulfillment-Backend/external/marketplace/wildberries/core.py

86 lines
2.8 KiB
Python

import time
from enum import StrEnum
from typing import AsyncIterator
from external.marketplace.base.core import BaseMarketplaceApi
from models import Marketplace
class WildberriesApiUrl(StrEnum):
CONTENT = 'https://content-api.wildberries.ru'
DISCOUNTS_PRICES = 'https://discounts-prices-api.wildberries.ru'
SUPPLIES = 'https://supplies-api.wildberries.ru'
MARKETPLACE = 'https://marketplace-api.wildberries.ru'
STATISTICS = 'https://statistics-api.wildberries.ru'
SELLER_ANALYTICS = 'https://seller-analytics-api.wildberries.ru'
ADVERT = 'https://advert-api.wildberries.ru'
RECOMMEND = 'https://recommend-api.wildberries.ru'
FEEDBACKS = 'https://feedbacks-api.wildberries.ru'
COMMON = 'https://common-api.wildberries.ru'
BUYER_CHAT = 'https://buyer-chat-api.wildberries.ru'
RETURNS = 'https://returns-api.wildberries.ru'
DOCUMENTS = 'https://documents-api.wildberries.ru'
class WildberriesMarketplaceApi(BaseMarketplaceApi):
def __init__(self, marketplace: Marketplace):
token = marketplace.auth_data.get('Authorization')
if not token:
raise ValueError(
f"Authorization token is missing for Marketplace ID: {marketplace.id}. "
"Please check the marketplace credentials."
)
self.token = token
self.headers = {'Authorization': token, 'User-Agent': 'wbas_seller.denco.store3547'}
self.marketplace = marketplace
@property
def get_headers(self) -> dict:
return self.headers
@property
def base_url(self) -> str:
return ""
async def get_products(self, data: dict) -> dict:
method = WildberriesApiUrl.CONTENT + '/content/v2/get/cards/list'
response = await self._method('POST', method, json=data)
return response
async def get_all_products(self) -> AsyncIterator[dict]:
limit = 100
updated_at = None
nm_id = None
while True:
data = {
'settings': {
'cursor': {
'limit': limit,
'updatedAt': updated_at,
'nmID': nm_id
},
'filter': {
'withPhoto': -1
}
}
}
start = time.time()
response = await self.get_products(data)
print(f'Request elapsed: {round(time.time() - start, 2)}')
if not response:
break
cards = response.get('cards')
if not cards:
break
for card in cards:
yield card
cursor = response.get('cursor')
total = cursor.get('total')
if total < limit:
break
updated_at = cursor.get('updatedAt')
nm_id = cursor.get('nmID')