Files
Sipro-Stocks/marketplaces/base.py
2025-05-11 07:46:57 +03:00

42 lines
1023 B
Python

from abc import ABC, abstractmethod
from typing import Literal, Union
from aiohttp import ClientResponse, ClientSession
from database import Marketplace
class BaseMarketplaceApi(ABC):
session: ClientSession
is_valid: bool
@abstractmethod
def __init__(self, marketplace: Marketplace):
pass
@abstractmethod
async def update_stocks(self, data: Union[list, dict]) -> ClientResponse:
pass
@abstractmethod
def get_headers(self):
pass
@property
@abstractmethod
def api_url(self):
pass
async def _method(self, http_method: Literal['POST', 'GET', 'PATCH', 'PUT', 'DELETE'],
method: str,
data: dict) -> ClientResponse:
self.session = ClientSession()
response = await self.session.request(
http_method,
f'{self.api_url}{method}',
json=data,
headers=self.get_headers()
)
await self.session.close()
return response