mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
from abc import ABC, abstractmethod
|
|
from typing import TYPE_CHECKING, Protocol
|
|
|
|
from aiogram.methods.base import TelegramType
|
|
|
|
if TYPE_CHECKING:
|
|
from aiogram.client.bot import Bot
|
|
from aiogram.methods import TelegramMethod
|
|
|
|
|
|
class NextRequestMiddlewareType(Protocol[TelegramType]): # pragma: no cover
|
|
async def __call__(
|
|
self,
|
|
bot: Bot,
|
|
method: TelegramMethod[TelegramType],
|
|
) -> TelegramType:
|
|
pass
|
|
|
|
|
|
class RequestMiddlewareType(Protocol): # pragma: no cover
|
|
async def __call__(
|
|
self,
|
|
make_request: NextRequestMiddlewareType[TelegramType],
|
|
bot: Bot,
|
|
method: TelegramMethod[TelegramType],
|
|
) -> TelegramType:
|
|
pass
|
|
|
|
|
|
class BaseRequestMiddleware(ABC):
|
|
"""
|
|
Generic middleware class
|
|
"""
|
|
|
|
@abstractmethod
|
|
async def __call__(
|
|
self,
|
|
make_request: NextRequestMiddlewareType[TelegramType],
|
|
bot: Bot,
|
|
method: TelegramMethod[TelegramType],
|
|
) -> TelegramType:
|
|
"""
|
|
Execute middleware
|
|
|
|
:param make_request: Wrapped make_request in middlewares chain
|
|
:param bot: bot for request making
|
|
:param method: Request method (Subclass of :class:`aiogram.methods.base.TelegramMethod`)
|
|
|
|
:return: Concrete Telegram type (e.g. Message, User, etc.)
|
|
"""
|