More docstrings

This commit is contained in:
Alex Root Junior 2019-12-12 00:52:38 +02:00
parent 54d7940691
commit 02cefa1cf3
2 changed files with 60 additions and 4 deletions

View file

@ -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)

View file

@ -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}",