Created
October 21, 2022 10:35
-
-
Save Glukhoff/8472c25d48ad320d53ee40ab25654a1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Callable, Any, Awaitable, Dict | |
from aiogram import BaseMiddleware | |
from aiogram.types import CallbackQuery | |
from bot.data.config import Config | |
from bot.models.base import create_poll | |
class DataBaseMiddleware(BaseMiddleware): | |
def __init__(self, config: Config) -> None: | |
self.sessionmaker = create_poll( | |
user=config.db.user, | |
password=config.db.password, | |
host=config.db.host, | |
name=config.db.name | |
) | |
async def __call__( | |
self, | |
handler: Callable[[CallbackQuery, Dict[str, Any]], Awaitable[Any]], | |
event: CallbackQuery, | |
data: Dict[str, Any] | |
) -> Any: | |
async with self.sessionmaker() as session: | |
data['db'] = session | |
return await handler(event, data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment