Small refactoring of telegram api server

This commit is contained in:
Alex Root Junior 2019-11-17 22:20:29 +02:00
parent 0e35b6f55e
commit a46cd6d001
6 changed files with 43 additions and 26 deletions

View file

@ -19,6 +19,9 @@ jobs:
python -m pip install --upgrade pip python -m pip install --upgrade pip
pip install poetry pip install poetry
poetry install poetry install
- name: Run tests
run: |
poetry pytest tests
- name: Build docs - name: Build docs
run: | run: |
poetry run mkdocs build poetry run mkdocs build

View file

@ -79,7 +79,7 @@ lint: isort black flake8 mypy
.PHONY: test .PHONY: test
test: test:
$(py) pytest --cov=aiogram --cov-config .coveragerc tests/ -sq $(py) pytest --cov=aiogram --cov-config .coveragerc tests/
# ================================================================================================= # =================================================================================================

View file

@ -4,38 +4,21 @@ import datetime
import json import json
from typing import Any, Callable, Optional, TypeVar, Union from typing import Any, Callable, Optional, TypeVar, Union
from pydantic.dataclasses import dataclass from ...methods import Response, TelegramMethod
from ..telegram import PRODUCTION, TelegramAPIServer
from aiogram.api.methods import Response, TelegramMethod
T = TypeVar("T") T = TypeVar("T")
@dataclass
class TelegramAPIServer:
base: str
file: str
def api_url(self, token: str, method: str) -> str:
return self.base.format(token=token, method=method)
def file_url(self, token: str, path: str) -> str:
return self.file.format(token=token, path=path)
PRODUCTION = TelegramAPIServer(
base="https://api.telegram.org/bot{token}/{method}",
file="https://api.telegram.org/file/bot{token}/{path}",
)
class BaseSession(abc.ABC): class BaseSession(abc.ABC):
def __init__( def __init__(
self, self,
api: TelegramAPIServer = PRODUCTION, api: Optional[TelegramAPIServer] = None,
json_loads: Optional[Callable] = None, json_loads: Optional[Callable] = None,
json_dumps: Optional[Callable] = None, json_dumps: Optional[Callable] = None,
): ):
if api is None:
api = PRODUCTION
if json_loads is None: if json_loads is None:
json_loads = json.loads json_loads = json.loads
if json_dumps is None: if json_dumps is None:

View file

@ -0,0 +1,19 @@
from pydantic.dataclasses import dataclass
@dataclass
class TelegramAPIServer:
base: str
file: str
def api_url(self, token: str, method: str) -> str:
return self.base.format(token=token, method=method)
def file_url(self, token: str, path: str) -> str:
return self.file.format(token=token, path=path)
PRODUCTION = TelegramAPIServer(
base="https://api.telegram.org/bot{token}/{method}",
file="https://api.telegram.org/file/bot{token}/{path}",
)

View file

@ -1,4 +1,4 @@
from aiogram.api.client.session.base import PRODUCTION from aiogram.api.client.telegram import PRODUCTION
class TestAPIServer: class TestAPIServer:

View file

@ -2,11 +2,11 @@ import datetime
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
from asynctest import CoroutineMock
from aiogram.api.client.session.base import BaseSession from aiogram.api.client.session.base import BaseSession
from aiogram.api.client.telegram import PRODUCTION, TelegramAPIServer
from aiogram.api.methods import GetMe, Response from aiogram.api.methods import GetMe, Response
from aiogram.utils.mixins import DataMixin from aiogram.utils.mixins import DataMixin
from asynctest import CoroutineMock
class TestBaseSession(DataMixin): class TestBaseSession(DataMixin):
@ -17,6 +17,18 @@ class TestBaseSession(DataMixin):
def teardown(self): def teardown(self):
BaseSession.__abstractmethods__ = self["__abstractmethods__"] BaseSession.__abstractmethods__ = self["__abstractmethods__"]
def test_init_api(self):
session = BaseSession()
assert session.api == PRODUCTION
def test_init_custom_api(self):
api = TelegramAPIServer(
base="http://example.com/{token}/{method}",
file="http://example.com/{token}/file/{path{",
)
session = BaseSession(api=api)
assert session.api == api
def test_sync_close(self): def test_sync_close(self):
session = BaseSession() session = BaseSession()