mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-11 18:01:04 +00:00
Remove deprecated temp session.
This commit is contained in:
parent
651e0ad2f1
commit
80d2b24d7e
2 changed files with 24 additions and 63 deletions
|
|
@ -72,9 +72,6 @@ class BaseBot:
|
||||||
self.session = aiohttp.ClientSession(connector=connector, request_class=request_class,
|
self.session = aiohttp.ClientSession(connector=connector, request_class=request_class,
|
||||||
loop=self.loop, json_serialize=json.dumps)
|
loop=self.loop, json_serialize=json.dumps)
|
||||||
|
|
||||||
# Temp sessions
|
|
||||||
self._temp_sessions = []
|
|
||||||
|
|
||||||
# Data stored in bot instance
|
# Data stored in bot instance
|
||||||
self._data = {}
|
self._data = {}
|
||||||
|
|
||||||
|
|
@ -90,40 +87,6 @@ class BaseBot:
|
||||||
"""
|
"""
|
||||||
if self.session and not self.session.closed:
|
if self.session and not self.session.closed:
|
||||||
await self.session.close()
|
await self.session.close()
|
||||||
for session in self._temp_sessions:
|
|
||||||
if not session.closed:
|
|
||||||
await session.close()
|
|
||||||
|
|
||||||
@deprecated('Manually you may use `aiohttp.ClientSession` instead of temp session')
|
|
||||||
def create_temp_session(self, limit: base.Integer = 1, force_close: base.Boolean = False) -> aiohttp.ClientSession:
|
|
||||||
"""
|
|
||||||
Create temporary session
|
|
||||||
|
|
||||||
:param limit: Limit of connections
|
|
||||||
:type limit: :obj:`int`
|
|
||||||
:param force_close: Set to True to force close and do reconnect after each request (and between redirects).
|
|
||||||
:type force_close: :obj:`bool`
|
|
||||||
:return: New session
|
|
||||||
:rtype: :obj:`aiohttp.TCPConnector`
|
|
||||||
"""
|
|
||||||
session = aiohttp.ClientSession(
|
|
||||||
connector=aiohttp.TCPConnector(limit=limit, force_close=force_close),
|
|
||||||
loop=self.loop, json_serialize=json.dumps)
|
|
||||||
self._temp_sessions.append(session)
|
|
||||||
return session
|
|
||||||
|
|
||||||
@deprecated('Manually you may use `aiohttp.ClientSession` instead of temp session')
|
|
||||||
async def destroy_temp_session(self, session: aiohttp.ClientSession):
|
|
||||||
"""
|
|
||||||
Destroy temporary session
|
|
||||||
|
|
||||||
:param session: target session
|
|
||||||
:type session: :obj:`aiohttp.ClientSession`
|
|
||||||
"""
|
|
||||||
if not session.closed:
|
|
||||||
await session.close()
|
|
||||||
if session in self._temp_sessions:
|
|
||||||
self._temp_sessions.remove(session)
|
|
||||||
|
|
||||||
async def request(self, method: base.String,
|
async def request(self, method: base.String,
|
||||||
data: Optional[Dict] = None,
|
data: Optional[Dict] = None,
|
||||||
|
|
@ -168,12 +131,10 @@ class BaseBot:
|
||||||
if destination is None:
|
if destination is None:
|
||||||
destination = io.BytesIO()
|
destination = io.BytesIO()
|
||||||
|
|
||||||
session = self.create_temp_session()
|
|
||||||
url = api.Methods.file_url(token=self.__token, path=file_path)
|
url = api.Methods.file_url(token=self.__token, path=file_path)
|
||||||
|
|
||||||
dest = destination if isinstance(destination, io.IOBase) else open(destination, 'wb')
|
dest = destination if isinstance(destination, io.IOBase) else open(destination, 'wb')
|
||||||
try:
|
async with self.session.get(url, timeout=timeout, proxy=self.proxy, proxy_auth=self.proxy_auth) as response:
|
||||||
async with session.get(url, timeout=timeout, proxy=self.proxy, proxy_auth=self.proxy_auth) as response:
|
|
||||||
while True:
|
while True:
|
||||||
chunk = await response.content.read(chunk_size)
|
chunk = await response.content.read(chunk_size)
|
||||||
if not chunk:
|
if not chunk:
|
||||||
|
|
@ -183,8 +144,6 @@ class BaseBot:
|
||||||
if seek:
|
if seek:
|
||||||
dest.seek(0)
|
dest.seek(0)
|
||||||
return dest
|
return dest
|
||||||
finally:
|
|
||||||
await self.destroy_temp_session(session)
|
|
||||||
|
|
||||||
async def send_file(self, file_type, method, file, payload) -> Union[Dict, base.Boolean]:
|
async def send_file(self, file_type, method, file, payload) -> Union[Dict, base.Boolean]:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
from aiogram import Bot, types
|
from aiogram import Bot, types
|
||||||
from aiogram.dispatcher import Dispatcher
|
from aiogram.dispatcher import Dispatcher
|
||||||
from aiogram.types import ParseMode
|
from aiogram.types import ParseMode
|
||||||
|
|
@ -28,29 +30,29 @@ bot = Bot(token=API_TOKEN, loop=loop, proxy=PROXY_URL)
|
||||||
dp = Dispatcher(bot)
|
dp = Dispatcher(bot)
|
||||||
|
|
||||||
|
|
||||||
|
async def fetch(url, proxy=None, proxy_auth=None):
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(url, proxy=proxy, proxy_auth=proxy_auth) as response:
|
||||||
|
return await response.text()
|
||||||
|
|
||||||
|
|
||||||
@dp.message_handler(commands=['start'])
|
@dp.message_handler(commands=['start'])
|
||||||
async def cmd_start(message: types.Message):
|
async def cmd_start(message: types.Message):
|
||||||
# Create a temporary session
|
|
||||||
session = bot.create_temp_session()
|
|
||||||
|
|
||||||
content = []
|
content = []
|
||||||
|
|
||||||
# Make request (without proxy)
|
# Make request (without proxy)
|
||||||
async with session.get(GET_IP_URL) as response:
|
ip = await fetch(GET_IP_URL)
|
||||||
content.append(text(':globe_showing_Americas:', bold('IP:'), code(await response.text())))
|
content.append(text(':globe_showing_Americas:', bold('IP:'), code(ip)))
|
||||||
# This line is formatted to '🌎 *IP:* `YOUR IP`'
|
# This line is formatted to '🌎 *IP:* `YOUR IP`'
|
||||||
|
|
||||||
# Make request through proxy
|
# Make request through proxy
|
||||||
async with session.get(GET_IP_URL, proxy=bot.proxy, proxy_auth=bot.proxy_auth) as response:
|
ip = await fetch(GET_IP_URL, bot.proxy, bot.proxy_auth)
|
||||||
content.append(text(':locked_with_key:', bold('IP:'), code(await response.text()), italic('via proxy')))
|
content.append(text(':locked_with_key:', bold('IP:'), code(ip), italic('via proxy')))
|
||||||
# This line is formatted to '🔐 *IP:* `YOUR IP` _via proxy_'
|
# This line is formatted to '🔐 *IP:* `YOUR IP` _via proxy_'
|
||||||
|
|
||||||
# Send content
|
# Send content
|
||||||
await bot.send_message(message.chat.id, emojize(text(*content, sep='\n')), parse_mode=ParseMode.MARKDOWN)
|
await bot.send_message(message.chat.id, emojize(text(*content, sep='\n')), parse_mode=ParseMode.MARKDOWN)
|
||||||
|
|
||||||
# Destroy temp session
|
|
||||||
await bot.destroy_temp_session(session)
|
|
||||||
|
|
||||||
# In this example you can see emoji codes: ":globe_showing_Americas:" and ":locked_with_key:"
|
# In this example you can see emoji codes: ":globe_showing_Americas:" and ":locked_with_key:"
|
||||||
# You can find full emoji cheat sheet at https://www.webpagefx.com/tools/emoji-cheat-sheet/
|
# You can find full emoji cheat sheet at https://www.webpagefx.com/tools/emoji-cheat-sheet/
|
||||||
# For representing emoji codes into real emoji use emoji util (aiogram.utils.emoji)
|
# For representing emoji codes into real emoji use emoji util (aiogram.utils.emoji)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue