Template
mirror of
https://github.com/djimboy/djimbo_template_aio3.git
synced 2026-08-25 06:27:43 +00:00
Update aiogram 3 template
This commit is contained in:
@@ -4,20 +4,21 @@ from typing import Optional
|
||||
import aiohttp
|
||||
|
||||
|
||||
# In handler
|
||||
# Пример использования в обработчике
|
||||
# session = await arSession.get_session()
|
||||
# response = await session.get(...)
|
||||
# response = await session.post(...)
|
||||
|
||||
# Асинхронная сессия для запросов
|
||||
class AsyncRequestSession:
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, timeout: int = 30) -> None:
|
||||
self._session: Optional[aiohttp.ClientSession] = None
|
||||
self._timeout = aiohttp.ClientTimeout(total=timeout)
|
||||
|
||||
# Получение сессии
|
||||
async def get_session(self) -> aiohttp.ClientSession:
|
||||
if self._session is None:
|
||||
new_session = aiohttp.ClientSession()
|
||||
new_session = aiohttp.ClientSession(timeout=self._timeout)
|
||||
self._session = new_session
|
||||
|
||||
return self._session
|
||||
@@ -25,6 +26,7 @@ class AsyncRequestSession:
|
||||
# Закрытие сессии
|
||||
async def close(self) -> None:
|
||||
if self._session is None:
|
||||
return None
|
||||
return
|
||||
|
||||
await self._session.close()
|
||||
self._session = None
|
||||
|
||||
Reference in New Issue
Block a user