mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Adding on_make_request and on_post_make_request methods to middlewares and CancelRequest exception
This commit is contained in:
parent
aec67b18af
commit
5cba5c05c4
2 changed files with 20 additions and 5 deletions
|
|
@ -11,6 +11,7 @@ import aiohttp
|
|||
import certifi
|
||||
from aiohttp.helpers import sentinel
|
||||
|
||||
from utils.exceptions import CancelRequest
|
||||
from . import api
|
||||
from ..types import ParseMode, base
|
||||
from ..utils import json
|
||||
|
|
@ -174,7 +175,7 @@ class BaseBot:
|
|||
|
||||
async def request(self, method: base.String,
|
||||
data: Optional[Dict] = None,
|
||||
files: Optional[Dict] = None, **kwargs) -> Union[List, Dict, base.Boolean]:
|
||||
files: Optional[Dict] = None, **kwargs) -> Optional[Union[List, Dict, base.Boolean]]:
|
||||
"""
|
||||
Make an request to Telegram Bot API
|
||||
|
||||
|
|
@ -190,8 +191,18 @@ class BaseBot:
|
|||
:rtype: Union[List, Dict]
|
||||
:raise: :obj:`aiogram.exceptions.TelegramApiError`
|
||||
"""
|
||||
return await api.make_request(self.session, self.__token, method, data, files,
|
||||
proxy=self.proxy, proxy_auth=self.proxy_auth, timeout=self.timeout, **kwargs)
|
||||
from aiogram import Dispatcher
|
||||
dp = Dispatcher.get_current()
|
||||
try:
|
||||
await dp.middleware.trigger('make_request', [self.__token, method, data, files])
|
||||
except CancelRequest:
|
||||
return None
|
||||
else:
|
||||
response = await api.make_request(self.session, self.__token, method, data, files,
|
||||
proxy=self.proxy, proxy_auth=self.proxy_auth, timeout=self.timeout,
|
||||
**kwargs)
|
||||
await dp.middleware.trigger('post_make_request', [self.__token, method, data, files, response])
|
||||
return response
|
||||
|
||||
async def download_file(self, file_path: base.String,
|
||||
destination: Optional[base.InputFile] = None,
|
||||
|
|
|
|||
|
|
@ -153,6 +153,10 @@ class ValidationError(TelegramAPIError):
|
|||
pass
|
||||
|
||||
|
||||
class CancelRequest(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class BadRequest(TelegramAPIError, _MatchErrorMixin):
|
||||
__group = True
|
||||
|
||||
|
|
@ -551,5 +555,5 @@ class Throttled(TelegramAPIError):
|
|||
|
||||
def __str__(self):
|
||||
return f"Rate limit exceeded! (Limit: {self.rate} s, " \
|
||||
f"exceeded: {self.exceeded_count}, " \
|
||||
f"time delta: {round(self.delta, 3)} s)"
|
||||
f"exceeded: {self.exceeded_count}, " \
|
||||
f"time delta: {round(self.delta, 3)} s)"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue