diff --git a/.gitignore b/.gitignore index 8b1f9b5..e92b37b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,8 +14,8 @@ dist/ downloads/ eggs/ .eggs/ -lib/ -lib64/ +#lib/ +#lib64/ parts/ sdist/ var/ diff --git a/src/app/lib/__init__.py b/src/app/lib/__init__.py new file mode 100644 index 0000000..b3a9ca5 --- /dev/null +++ b/src/app/lib/__init__.py @@ -0,0 +1 @@ +from .tbank_client import TBankClient \ No newline at end of file diff --git a/src/app/lib/base_client/__init__.py b/src/app/lib/base_client/__init__.py new file mode 100644 index 0000000..433ca53 --- /dev/null +++ b/src/app/lib/base_client/__init__.py @@ -0,0 +1,3 @@ +from .client import BaseClient +from .httpx_client import HTTPXClient +from .niquests_client import NiquestsClient diff --git a/src/app/lib/base_client/client.py b/src/app/lib/base_client/client.py new file mode 100644 index 0000000..45ad8f4 --- /dev/null +++ b/src/app/lib/base_client/client.py @@ -0,0 +1,18 @@ +from abc import ABC, abstractmethod +from typing import Optional + + +class BaseClient(ABC): + @abstractmethod + async def _method(self, method: str, path: str, data: Optional[dict] = None, params: Optional[dict] = None) -> dict: + raise NotImplementedError("_method must be implemented by subclasses") + + @property + @abstractmethod + def base_url(self) -> str: + raise NotImplementedError("Subclasses must implement base_url property") + + @property + @abstractmethod + def headers(self) -> dict: + raise NotImplementedError("Subclasses must implement headers property") diff --git a/src/app/lib/base_client/httpx_client.py b/src/app/lib/base_client/httpx_client.py new file mode 100644 index 0000000..859c1d6 --- /dev/null +++ b/src/app/lib/base_client/httpx_client.py @@ -0,0 +1,22 @@ +from abc import ABC +from typing import Optional + +import httpx + +from .client import BaseClient + + +class HTTPXClient(BaseClient, ABC): + async def _method(self, method: str, path: str, data: Optional[dict] = None, params: Optional[dict] = None) -> dict: + async with httpx.AsyncClient() as client: + url = self.base_url + path + headers = self.headers + request = client.build_request( + method, + url, + json=data, + params=params, + headers=headers + ) + response = await client.send(request) + return response.json() diff --git a/src/app/lib/base_client/niquests_client.py b/src/app/lib/base_client/niquests_client.py new file mode 100644 index 0000000..2973fac --- /dev/null +++ b/src/app/lib/base_client/niquests_client.py @@ -0,0 +1,14 @@ +from abc import ABC +from typing import Optional + +import niquests.async_api + +from .client import BaseClient + + +class NiquestsClient(BaseClient, ABC): + async def _method(self, method: str, path: str, data: Optional[dict] = None, params: Optional[dict] = None) -> dict: + url = self.base_url + path + headers = self.headers + response = await niquests.async_api.request(method, url, params=params, json=data, headers=headers) + return response.json() diff --git a/src/app/lib/tbank_client/__init__.py b/src/app/lib/tbank_client/__init__.py new file mode 100644 index 0000000..1d90892 --- /dev/null +++ b/src/app/lib/tbank_client/__init__.py @@ -0,0 +1 @@ +from .client import TBankClient \ No newline at end of file diff --git a/src/app/lib/tbank_client/client.py b/src/app/lib/tbank_client/client.py new file mode 100644 index 0000000..c8b6139 --- /dev/null +++ b/src/app/lib/tbank_client/client.py @@ -0,0 +1,19 @@ +from typing import Any, Coroutine + +from ..base_client import BaseClient, NiquestsClient + + +class TBankClient(NiquestsClient): + @property + def headers(self) -> dict: + return {'Authorization': 'Bearer ' + self.api_key} + + @property + def base_url(self) -> str: + return "https://business.tbank.ru/openapi/api" + + def __init__(self, api_key: str): + self.api_key = api_key + + async def get_counterparty_excerpt_by_inn(self, inn: str) -> dict: + return await self._method('GET', '/v1/counterparty/excerpt/by-inn', params={'inn': inn})