Added base code and make code improvements

This commit is contained in:
Alex Root Junior 2021-09-20 22:28:52 +03:00
parent 5bd1162f57
commit bd2a348aa0
16 changed files with 522 additions and 424 deletions

View file

@ -1,4 +1,10 @@
from dataclasses import dataclass
from typing import Any, Protocol
class WrapLocalFileCallbackCallbackProtocol(Protocol):
def __call__(self, value: str) -> str:
pass
@dataclass(frozen=True)
@ -8,8 +14,13 @@ class TelegramAPIServer:
"""
base: str
"""Base URL"""
file: str
"""Files URL"""
is_local: bool = False
"""Mark this server is in `local mode <https://core.telegram.org/bots/api#using-a-local-bot-api-server>`_."""
wrap_local_file: WrapLocalFileCallbackCallbackProtocol = lambda v: v
"""Callback to wrap files path in local mode"""
def api_url(self, token: str, method: str) -> str:
"""
@ -32,19 +43,18 @@ class TelegramAPIServer:
return self.file.format(token=token, path=path)
@classmethod
def from_base(cls, base: str, is_local: bool = False) -> "TelegramAPIServer":
def from_base(cls, base: str, **kwargs: Any) -> "TelegramAPIServer":
"""
Use this method to auto-generate TelegramAPIServer instance from base URL
:param base: Base URL
:param is_local: Mark this server is in `local mode <https://core.telegram.org/bots/api#using-a-local-bot-api-server>`_.
:return: instance of :class:`TelegramAPIServer`
"""
base = base.rstrip("/")
return cls(
base=f"{base}/bot{{token}}/{{method}}",
file=f"{base}/file/bot{{token}}/{{path}}",
is_local=is_local,
**kwargs,
)