Небольшая оптимизация
This commit is contained in:
parent
1fb2fbd654
commit
2cd3d64bb8
@ -5,12 +5,13 @@ from typing import TYPE_CHECKING
|
|||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import aiofiles
|
import aiofiles
|
||||||
import aiohttp
|
|
||||||
|
|
||||||
import puremagic
|
import puremagic
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from aiohttp import ClientSession, ClientConnectionError, FormData
|
||||||
|
|
||||||
from ..exceptions.invalid_token import InvalidToken
|
from ..exceptions.invalid_token import InvalidToken
|
||||||
|
from ..exceptions.max import MaxConnection
|
||||||
|
|
||||||
from ..types.errors import Error
|
from ..types.errors import Error
|
||||||
from ..enums.http_method import HTTPMethod
|
from ..enums.http_method import HTTPMethod
|
||||||
@ -37,7 +38,7 @@ class BaseConnection:
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.bot: 'Bot' = None
|
self.bot: 'Bot' = None
|
||||||
self.session: aiohttp.ClientSession = None
|
self.session: ClientSession = None
|
||||||
|
|
||||||
async def request(
|
async def request(
|
||||||
self,
|
self,
|
||||||
@ -64,7 +65,7 @@ class BaseConnection:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if not self.bot.session:
|
if not self.bot.session:
|
||||||
self.bot.session = aiohttp.ClientSession(self.bot.API_URL)
|
self.bot.session = ClientSession(self.bot.API_URL)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
r = await self.bot.session.request(
|
r = await self.bot.session.request(
|
||||||
@ -72,10 +73,11 @@ class BaseConnection:
|
|||||||
url=path.value if isinstance(path, ApiPath) else path,
|
url=path.value if isinstance(path, ApiPath) else path,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
except aiohttp.ClientConnectorDNSError as e:
|
except ClientConnectionError as e:
|
||||||
return logger_connection.error(f'Ошибка при отправке запроса: {e}')
|
raise MaxConnection(f'Ошибка при отправке запроса: {e}')
|
||||||
|
|
||||||
if r.status == 401:
|
if r.status == 401:
|
||||||
|
await self.bot.session.close()
|
||||||
raise InvalidToken('Неверный токен!')
|
raise InvalidToken('Неверный токен!')
|
||||||
|
|
||||||
if not r.ok:
|
if not r.ok:
|
||||||
@ -122,7 +124,7 @@ class BaseConnection:
|
|||||||
basename = os.path.basename(path)
|
basename = os.path.basename(path)
|
||||||
_, ext = os.path.splitext(basename)
|
_, ext = os.path.splitext(basename)
|
||||||
|
|
||||||
form = aiohttp.FormData()
|
form = FormData()
|
||||||
form.add_field(
|
form.add_field(
|
||||||
name='data',
|
name='data',
|
||||||
value=file_data,
|
value=file_data,
|
||||||
@ -130,7 +132,7 @@ class BaseConnection:
|
|||||||
content_type=f"{type.value}/{ext.lstrip('.')}"
|
content_type=f"{type.value}/{ext.lstrip('.')}"
|
||||||
)
|
)
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with ClientSession() as session:
|
||||||
response = await session.post(
|
response = await session.post(
|
||||||
url=url,
|
url=url,
|
||||||
data=form
|
data=form
|
||||||
@ -168,7 +170,7 @@ class BaseConnection:
|
|||||||
|
|
||||||
basename = f'{uuid4()}{ext}'
|
basename = f'{uuid4()}{ext}'
|
||||||
|
|
||||||
form = aiohttp.FormData()
|
form = FormData()
|
||||||
form.add_field(
|
form.add_field(
|
||||||
name='data',
|
name='data',
|
||||||
value=buffer,
|
value=buffer,
|
||||||
@ -176,7 +178,7 @@ class BaseConnection:
|
|||||||
content_type=mime_type
|
content_type=mime_type
|
||||||
)
|
)
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with ClientSession() as session:
|
||||||
response = await session.post(
|
response = await session.post(
|
||||||
url=url,
|
url=url,
|
||||||
data=form
|
data=form
|
||||||
@ -203,7 +205,7 @@ class BaseConnection:
|
|||||||
'Authorization': f'Bearer {token}'
|
'Authorization': f'Bearer {token}'
|
||||||
}
|
}
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with ClientSession() as session:
|
||||||
async with session.get(url, headers=headers) as response:
|
async with session.get(url, headers=headers) as response:
|
||||||
|
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user