Rework polling and start covering

This commit is contained in:
Alex Root Junior 2019-12-10 01:14:58 +02:00
parent 38c725db46
commit db397e3a05
6 changed files with 84 additions and 12 deletions

View file

@ -1,5 +1,6 @@
from __future__ import annotations
from contextlib import asynccontextmanager
from typing import Any, Optional, TypeVar
from ...utils.mixins import ContextInstanceMixin, DataMixin
@ -32,11 +33,15 @@ class BaseBot(ContextInstanceMixin, DataMixin):
async def close(self):
await self.session.close()
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.session.close()
@asynccontextmanager
async def context(self, auto_close: bool = True):
token = self.set_current(self)
try:
yield self
finally:
if auto_close:
await self.close()
self.reset_current(token)
def __hash__(self):
return hash(self.__token)

View file

@ -5,6 +5,8 @@ import datetime
import json
from typing import Any, Callable, Optional, TypeVar, Union
from aiogram.utils.exceptions import TelegramAPIError
from ...methods import Response, TelegramMethod
from ..telegram import PRODUCTION, TelegramAPIServer
@ -32,7 +34,7 @@ class BaseSession(abc.ABC):
def raise_for_status(self, response: Response[T]) -> None:
if response.ok:
return
raise Exception(response.description)
raise TelegramAPIError(response.description)
@abc.abstractmethod
async def close(self): # pragma: no cover

View file

@ -24,3 +24,9 @@ class User(TelegramObject):
"""Users or bots username"""
language_code: Optional[str] = None
"""IETF language tag of the user's language"""
@property
def full_name(self):
if self.last_name:
return f"{self.first_name} {self.last_name}"
return self.first_name