From a46cd6d001ce07fe271a671e04457884f66b029a Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Sun, 17 Nov 2019 22:20:29 +0200 Subject: [PATCH] Small refactoring of telegram api server --- .github/workflows/docs.yml | 3 +++ Makefile | 2 +- aiogram/api/client/session/base.py | 27 ++++--------------- aiogram/api/client/telegram.py | 19 +++++++++++++ .../{test_session => }/test_api_server.py | 2 +- .../test_session/test_base_session.py | 16 +++++++++-- 6 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 aiogram/api/client/telegram.py rename tests/test_api/test_client/{test_session => }/test_api_server.py (87%) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index e51fa917..0719de71 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -19,6 +19,9 @@ jobs: python -m pip install --upgrade pip pip install poetry poetry install + - name: Run tests + run: | + poetry pytest tests - name: Build docs run: | poetry run mkdocs build diff --git a/Makefile b/Makefile index 87d10075..f53beb5b 100644 --- a/Makefile +++ b/Makefile @@ -79,7 +79,7 @@ lint: isort black flake8 mypy .PHONY: test test: - $(py) pytest --cov=aiogram --cov-config .coveragerc tests/ -sq + $(py) pytest --cov=aiogram --cov-config .coveragerc tests/ # ================================================================================================= diff --git a/aiogram/api/client/session/base.py b/aiogram/api/client/session/base.py index 113af58e..874af7bc 100644 --- a/aiogram/api/client/session/base.py +++ b/aiogram/api/client/session/base.py @@ -4,38 +4,21 @@ import datetime import json from typing import Any, Callable, Optional, TypeVar, Union -from pydantic.dataclasses import dataclass - -from aiogram.api.methods import Response, TelegramMethod +from ...methods import Response, TelegramMethod +from ..telegram import PRODUCTION, TelegramAPIServer 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): def __init__( self, - api: TelegramAPIServer = PRODUCTION, + api: Optional[TelegramAPIServer] = None, json_loads: Optional[Callable] = None, json_dumps: Optional[Callable] = None, ): + if api is None: + api = PRODUCTION if json_loads is None: json_loads = json.loads if json_dumps is None: diff --git a/aiogram/api/client/telegram.py b/aiogram/api/client/telegram.py new file mode 100644 index 00000000..64d8d18c --- /dev/null +++ b/aiogram/api/client/telegram.py @@ -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}", +) diff --git a/tests/test_api/test_client/test_session/test_api_server.py b/tests/test_api/test_client/test_api_server.py similarity index 87% rename from tests/test_api/test_client/test_session/test_api_server.py rename to tests/test_api/test_client/test_api_server.py index e48f2684..3102568f 100644 --- a/tests/test_api/test_client/test_session/test_api_server.py +++ b/tests/test_api/test_client/test_api_server.py @@ -1,4 +1,4 @@ -from aiogram.api.client.session.base import PRODUCTION +from aiogram.api.client.telegram import PRODUCTION class TestAPIServer: diff --git a/tests/test_api/test_client/test_session/test_base_session.py b/tests/test_api/test_client/test_session/test_base_session.py index 34c2e8fb..eb260ba5 100644 --- a/tests/test_api/test_client/test_session/test_base_session.py +++ b/tests/test_api/test_client/test_session/test_base_session.py @@ -2,11 +2,11 @@ import datetime from unittest.mock import patch import pytest -from asynctest import CoroutineMock - 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.utils.mixins import DataMixin +from asynctest import CoroutineMock class TestBaseSession(DataMixin): @@ -17,6 +17,18 @@ class TestBaseSession(DataMixin): def teardown(self): 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): session = BaseSession()