feat: ozon sync
This commit is contained in:
		
							
								
								
									
										40
									
								
								external/marketplace/base/product_synchronizer.py
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								external/marketplace/base/product_synchronizer.py
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
			
		||||
from abc import ABC, abstractmethod
 | 
			
		||||
 | 
			
		||||
from sqlalchemy.ext.asyncio import AsyncSession
 | 
			
		||||
 | 
			
		||||
from external.marketplace.base import BaseMarketplaceApi
 | 
			
		||||
from models import Product, ProductBarcode, ProductImage
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class BaseProductSynchronizer(ABC):
 | 
			
		||||
    products: list[Product] = []
 | 
			
		||||
    barcodes: list[ProductBarcode] = []
 | 
			
		||||
    images: list[ProductImage] = []
 | 
			
		||||
    marketplace_products: list = []
 | 
			
		||||
    api: BaseMarketplaceApi
 | 
			
		||||
    session: AsyncSession
 | 
			
		||||
 | 
			
		||||
    def __init__(self, session, marketplace, api):
 | 
			
		||||
        self.session = session
 | 
			
		||||
        self.marketplace = marketplace
 | 
			
		||||
        self.api = api
 | 
			
		||||
        self._clear()
 | 
			
		||||
 | 
			
		||||
    def _clear(self):
 | 
			
		||||
        self.products = []
 | 
			
		||||
        self.barcodes = []
 | 
			
		||||
        self.images = []
 | 
			
		||||
        self.marketplace_products = []
 | 
			
		||||
 | 
			
		||||
    async def _write(self):
 | 
			
		||||
        instances = self.products + self.marketplace_products + self.barcodes + self.images
 | 
			
		||||
        self.session.add_all(instances)
 | 
			
		||||
        await self.session.commit()
 | 
			
		||||
 | 
			
		||||
    @abstractmethod
 | 
			
		||||
    async def create_products(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    @abstractmethod
 | 
			
		||||
    async def synchronize_products(self):
 | 
			
		||||
        pass
 | 
			
		||||
							
								
								
									
										52
									
								
								external/marketplace/ozon/core.py
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										52
									
								
								external/marketplace/ozon/core.py
									
									
									
									
										vendored
									
									
								
							@@ -1,15 +1,61 @@
 | 
			
		||||
from typing import AsyncIterator
 | 
			
		||||
 | 
			
		||||
from external.marketplace.base.core import BaseMarketplaceApi
 | 
			
		||||
from models import Marketplace
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class OzonMarketplaceApi(BaseMarketplaceApi):
 | 
			
		||||
    def __init__(self, marketplace: Marketplace):
 | 
			
		||||
        pass
 | 
			
		||||
        client_id = marketplace.auth_data.get('Client-Id')
 | 
			
		||||
        api_key = marketplace.auth_data.get('Api-Key')
 | 
			
		||||
        if not client_id or not api_key:
 | 
			
		||||
            raise ValueError(
 | 
			
		||||
                f"Client-Id or Api-Key is missing for Marketplace ID: {marketplace.id}. "
 | 
			
		||||
                "Please check the marketplace credentials."
 | 
			
		||||
            )
 | 
			
		||||
        self.headers = marketplace.auth_data
 | 
			
		||||
        self.marketplace = marketplace
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def get_headers(self) -> dict:
 | 
			
		||||
        return {}
 | 
			
		||||
        return self.headers
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def base_url(self) -> str:
 | 
			
		||||
        return ""
 | 
			
		||||
        return "https://api-seller.ozon.ru"
 | 
			
		||||
 | 
			
		||||
    async def get_products(self, data: dict) -> dict:
 | 
			
		||||
        method = '/v2/product/list'
 | 
			
		||||
        response = await  self._method('POST', method, json=data)
 | 
			
		||||
        return response
 | 
			
		||||
 | 
			
		||||
    async def get_all_products(self) -> AsyncIterator[dict]:
 | 
			
		||||
        limit = 100
 | 
			
		||||
        last_id = ''
 | 
			
		||||
        while True:
 | 
			
		||||
            data = {
 | 
			
		||||
                'limit': limit,
 | 
			
		||||
                'last_id': last_id,
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
            response = await self.get_products(data)
 | 
			
		||||
            if not response:
 | 
			
		||||
                break
 | 
			
		||||
            result = response.get('result')
 | 
			
		||||
            if not result:
 | 
			
		||||
                break
 | 
			
		||||
            items = result.get('items')
 | 
			
		||||
            if not items:
 | 
			
		||||
                break
 | 
			
		||||
            for item in items:
 | 
			
		||||
                yield item
 | 
			
		||||
            last_id = result.get('last_id')
 | 
			
		||||
            if not last_id:
 | 
			
		||||
                break
 | 
			
		||||
 | 
			
		||||
    async def get_products_info(self, data: dict) -> dict:
 | 
			
		||||
        method = '/v2/product/info/list'
 | 
			
		||||
        response = await self._method('POST', method, json=data)
 | 
			
		||||
        return response
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
		Reference in New Issue
	
	Block a user