mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Create aiohttp session inside async function
This commit is contained in:
parent
2d0fabbfb8
commit
b7a7e20750
4 changed files with 21 additions and 10 deletions
|
|
@ -105,7 +105,7 @@ class BaseBot:
|
|||
|
||||
self.parse_mode = parse_mode
|
||||
|
||||
def get_new_session(self) -> aiohttp.ClientSession:
|
||||
async def get_new_session(self) -> aiohttp.ClientSession:
|
||||
return aiohttp.ClientSession(
|
||||
connector=self._connector_class(**self._connector_init, loop=self._main_loop),
|
||||
loop=self._main_loop,
|
||||
|
|
@ -116,10 +116,17 @@ class BaseBot:
|
|||
def loop(self) -> Optional[asyncio.AbstractEventLoop]:
|
||||
return self._main_loop
|
||||
|
||||
@property
|
||||
def session(self) -> Optional[aiohttp.ClientSession]:
|
||||
async def get_session(self) -> Optional[aiohttp.ClientSession]:
|
||||
if self._session is None or self._session.closed:
|
||||
self._session = self.get_new_session()
|
||||
self._session = await self.get_new_session()
|
||||
return self._session
|
||||
|
||||
@property
|
||||
@deprecated(
|
||||
reason="Client session should be created inside async function, use `await bot.get_session()` instead",
|
||||
stacklevel=3,
|
||||
)
|
||||
async def session(self) -> Optional[aiohttp.ClientSession]:
|
||||
return self._session
|
||||
|
||||
@staticmethod
|
||||
|
|
@ -185,7 +192,8 @@ class BaseBot:
|
|||
"""
|
||||
Close all client sessions
|
||||
"""
|
||||
await self.session.close()
|
||||
if self._session:
|
||||
await self._session.close()
|
||||
|
||||
async def request(self, method: base.String,
|
||||
data: Optional[Dict] = None,
|
||||
|
|
@ -205,7 +213,8 @@ class BaseBot:
|
|||
:rtype: Union[List, Dict]
|
||||
:raise: :obj:`aiogram.exceptions.TelegramApiError`
|
||||
"""
|
||||
return await api.make_request(self.session, self.server, self.__token, method, data, files,
|
||||
|
||||
return await api.make_request(await self.get_session(), self.server, self.__token, method, data, files,
|
||||
proxy=self.proxy, proxy_auth=self.proxy_auth, timeout=self.timeout, **kwargs)
|
||||
|
||||
async def download_file(self, file_path: base.String,
|
||||
|
|
@ -233,7 +242,8 @@ class BaseBot:
|
|||
url = self.get_file_url(file_path)
|
||||
|
||||
dest = destination if isinstance(destination, io.IOBase) else open(destination, 'wb')
|
||||
async with self.session.get(url, timeout=timeout, proxy=self.proxy, proxy_auth=self.proxy_auth) as response:
|
||||
session = await self.get_session()
|
||||
async with session.get(url, timeout=timeout, proxy=self.proxy, proxy_auth=self.proxy_auth) as response:
|
||||
while True:
|
||||
chunk = await response.content.read(chunk_size)
|
||||
if not chunk:
|
||||
|
|
|
|||
|
|
@ -365,7 +365,8 @@ class Executor:
|
|||
self.dispatcher.stop_polling()
|
||||
await self.dispatcher.storage.close()
|
||||
await self.dispatcher.storage.wait_closed()
|
||||
await self.dispatcher.bot.session.close()
|
||||
session = await self.dispatcher.bot.get_session()
|
||||
await session.close()
|
||||
|
||||
async def _startup_polling(self):
|
||||
await self._welcome()
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ async def cmd_start(message: types.Message):
|
|||
# This line is formatted to '🌎 *IP:* `YOUR IP`'
|
||||
|
||||
# Make request through bot's proxy
|
||||
ip = await fetch(GET_IP_URL, bot.session)
|
||||
ip = await fetch(GET_IP_URL, await bot.get_session())
|
||||
content.append(text(':locked_with_key:', bold('IP:'), code(ip), italic('via proxy')))
|
||||
# This line is formatted to '🔐 *IP:* `YOUR IP` _via proxy_'
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ async def bot_fixture():
|
|||
""" Bot fixture """
|
||||
_bot = Bot(TOKEN)
|
||||
yield _bot
|
||||
await _bot.session.close()
|
||||
await (await _bot.get_session()).close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue