From 02cefa1cf31ce34b1e8ba360e00e8cea5e9403cc Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Thu, 12 Dec 2019 00:52:38 +0200 Subject: [PATCH] More docstrings --- aiogram/api/client/base.py | 45 +++++++++++++++++++++++++++++++--- aiogram/api/client/telegram.py | 19 ++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/aiogram/api/client/base.py b/aiogram/api/client/base.py index cb0ea192..156125e7 100644 --- a/aiogram/api/client/base.py +++ b/aiogram/api/client/base.py @@ -13,7 +13,13 @@ T = TypeVar("T") class BaseBot(ContextInstanceMixin, DataMixin): - def __init__(self, token: str, session: BaseSession = None, parse_mode: Optional[str] = None): + """ + Base class for bots + """ + + def __init__( + self, token: str, session: BaseSession = None, parse_mode: Optional[str] = None + ) -> None: validate_token(token) if session is None: @@ -24,17 +30,37 @@ class BaseBot(ContextInstanceMixin, DataMixin): self.__token = token @property - def id(self): + def id(self) -> int: + """ + Get bot ID from token + + :return: + """ return extract_bot_id(self.__token) async def emit(self, method: TelegramMethod[T]) -> T: + """ + Call API method + + :param method: + :return: + """ return await self.session.make_request(self.__token, method) - async def close(self): + async def close(self) -> None: + """ + Close bot session + """ await self.session.close() @asynccontextmanager async def context(self, auto_close: bool = True): + """ + Generate bot context + + :param auto_close: + :return: + """ token = self.set_current(self) try: yield self @@ -43,10 +69,21 @@ class BaseBot(ContextInstanceMixin, DataMixin): await self.close() self.reset_current(token) - def __hash__(self): + def __hash__(self) -> int: + """ + Get hash for the token + + :return: + """ return hash(self.__token) def __eq__(self, other: Any) -> bool: + """ + Compare current bot with another bot instance + + :param other: + :return: + """ if not isinstance(other, BaseBot): return False return hash(self) == hash(other) diff --git a/aiogram/api/client/telegram.py b/aiogram/api/client/telegram.py index d45c19a9..97775e03 100644 --- a/aiogram/api/client/telegram.py +++ b/aiogram/api/client/telegram.py @@ -3,16 +3,35 @@ from dataclasses import dataclass @dataclass class TelegramAPIServer: + """ + Base config for API Endpoints + """ + base: str file: str def api_url(self, token: str, method: str) -> str: + """ + Generate URL for API methods + + :param token: Bot token + :param method: API method name (case insensitive) + :return: URL + """ return self.base.format(token=token, method=method) def file_url(self, token: str, path: str) -> str: + """ + Generate URL for downloading files + + :param token: Bot token + :param path: file path + :return: URL + """ return self.file.format(token=token, path=path) +# Main API server PRODUCTION = TelegramAPIServer( base="https://api.telegram.org/bot{token}/{method}", file="https://api.telegram.org/file/bot{token}/{path}",