mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-08 17:13:56 +00:00
Update annotations.
This commit is contained in:
parent
7ffeb8ff57
commit
5eeace04f4
4 changed files with 46 additions and 23 deletions
|
|
@ -6,8 +6,8 @@ from http import HTTPStatus
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
from ..utils import json
|
from ..utils import json
|
||||||
from ..utils.exceptions import ValidationError, TelegramAPIError, BadRequest, Unauthorized, NetworkError, RetryAfter, \
|
from ..utils.exceptions import BadRequest, ConflictError, MigrateToChat, NetworkError, RetryAfter, TelegramAPIError, \
|
||||||
MigrateToChat, ConflictError
|
Unauthorized, ValidationError
|
||||||
from ..utils.helper import Helper, HelperMode, Item
|
from ..utils.helper import Helper, HelperMode, Item
|
||||||
|
|
||||||
# Main aiogram logger
|
# Main aiogram logger
|
||||||
|
|
@ -129,13 +129,20 @@ async def request(session, token, method, data=None, files=None, continue_retry=
|
||||||
|
|
||||||
https://core.telegram.org/bots/api#making-requests
|
https://core.telegram.org/bots/api#making-requests
|
||||||
|
|
||||||
:param session: :class:`aiohttp.ClientSession`
|
:param session: HTTP Client session
|
||||||
|
:type session: :obj:`aiohttp.ClientSession`
|
||||||
:param token: BOT token
|
:param token: BOT token
|
||||||
|
:type token: :obj:`str`
|
||||||
:param method: API method
|
:param method: API method
|
||||||
|
:type method: :obj:`str`
|
||||||
:param data: request payload
|
:param data: request payload
|
||||||
|
:type data: :obj:`dict`
|
||||||
:param files: files
|
:param files: files
|
||||||
|
:type files: :obj:`dict`
|
||||||
:param continue_retry:
|
:param continue_retry:
|
||||||
:return: bool or dict
|
:type continue_retry: :obj:`dict`
|
||||||
|
:return: result
|
||||||
|
:rtype :obj:`bool` or :obj:`dict`
|
||||||
"""
|
"""
|
||||||
log.debug("Make request: '{0}' with data: {1} and files {2}".format(
|
log.debug("Make request: '{0}' with data: {1} and files {2}".format(
|
||||||
method, data or {}, files or {}))
|
method, data or {}, files or {}))
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
import io
|
import io
|
||||||
from typing import Union, TypeVar, List, Dict, Optional
|
from typing import Dict, List, Optional, TypeVar, Union
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
|
|
@ -31,13 +31,19 @@ class BaseBot:
|
||||||
Instructions how to get Bot token is found here: https://core.telegram.org/bots#3-how-do-i-create-a-bot
|
Instructions how to get Bot token is found here: https://core.telegram.org/bots#3-how-do-i-create-a-bot
|
||||||
|
|
||||||
:param token: token from @BotFather
|
:param token: token from @BotFather
|
||||||
|
:type token: :obj:`str`
|
||||||
:param loop: event loop
|
:param loop: event loop
|
||||||
|
:type loop: Optional Union :obj:`asyncio.BaseEventLoop`, :obj:`asyncio.AbstractEventLoop`
|
||||||
:param connections_limit: connections limit for aiohttp.ClientSession
|
:param connections_limit: connections limit for aiohttp.ClientSession
|
||||||
|
:type connections_limit: :obj:`int`
|
||||||
:param proxy: HTTP proxy URL
|
:param proxy: HTTP proxy URL
|
||||||
:param proxy_auth: :obj:`aiohttp.BasicAuth`
|
:type proxy: :obj:`str`
|
||||||
|
:param proxy_auth: Authentication information
|
||||||
|
:type proxy_auth: Optional :obj:`aiohttp.BasicAuth`
|
||||||
:param continue_retry: automatic retry sent request when flood control exceeded
|
:param continue_retry: automatic retry sent request when flood control exceeded
|
||||||
|
:type continue_retry: :obj:`bool`
|
||||||
|
:raise: when token is invalid throw an :obj:`aiogram.utils.exceptions.ValidationError`
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.__token = token
|
self.__token = token
|
||||||
self.proxy = proxy
|
self.proxy = proxy
|
||||||
self.proxy_auth = proxy_auth
|
self.proxy_auth = proxy_auth
|
||||||
|
|
@ -56,8 +62,6 @@ class BaseBot:
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
"""
|
"""
|
||||||
When bot object is deleting - need close all sessions
|
When bot object is deleting - need close all sessions
|
||||||
|
|
||||||
:return:
|
|
||||||
"""
|
"""
|
||||||
for session in self._temp_sessions:
|
for session in self._temp_sessions:
|
||||||
if not session.closed:
|
if not session.closed:
|
||||||
|
|
@ -65,15 +69,18 @@ class BaseBot:
|
||||||
if self.session and not self.session.closed:
|
if self.session and not self.session.closed:
|
||||||
self.session.close()
|
self.session.close()
|
||||||
|
|
||||||
def create_temp_session(self) -> aiohttp.ClientSession:
|
def create_temp_session(self, limit: int = 1) -> aiohttp.ClientSession:
|
||||||
"""
|
"""
|
||||||
Create temporary session
|
Create temporary session
|
||||||
|
|
||||||
:return:
|
:param limit: Limit of connections
|
||||||
|
:type limit: :obj:`int`
|
||||||
|
:return: New session
|
||||||
|
:rtype: :obj:`aiohttp.TCPConnector`
|
||||||
"""
|
"""
|
||||||
session = aiohttp.ClientSession(
|
session = aiohttp.ClientSession(
|
||||||
connector=aiohttp.TCPConnector(limit=1, force_close=True),
|
connector=aiohttp.TCPConnector(limit=limit, force_close=True),
|
||||||
loop=self.loop)
|
loop=self.loop, json_serialize=json.dumps)
|
||||||
self._temp_sessions.append(session)
|
self._temp_sessions.append(session)
|
||||||
return session
|
return session
|
||||||
|
|
||||||
|
|
@ -81,8 +88,8 @@ class BaseBot:
|
||||||
"""
|
"""
|
||||||
Destroy temporary session
|
Destroy temporary session
|
||||||
|
|
||||||
:param session:
|
:param session: target session
|
||||||
:return:
|
:type session: :obj:`aiohttp.ClientSession`
|
||||||
"""
|
"""
|
||||||
if not session.closed:
|
if not session.closed:
|
||||||
session.close()
|
session.close()
|
||||||
|
|
@ -98,10 +105,14 @@ class BaseBot:
|
||||||
https://core.telegram.org/bots/api#making-requests
|
https://core.telegram.org/bots/api#making-requests
|
||||||
|
|
||||||
:param method: API method
|
:param method: API method
|
||||||
|
:type method: :obj:`str`
|
||||||
:param data: request parameters
|
:param data: request parameters
|
||||||
|
:type data: :obj:`dict`
|
||||||
:param files: files
|
:param files: files
|
||||||
:return: Union[List, Dict]
|
:type files: :obj:`dict`
|
||||||
:raise: :class:`aiogram.exceptions.TelegramApiError`
|
:return: result
|
||||||
|
:rtype: Union[List, Dict]
|
||||||
|
:raise: :obj:`aiogram.exceptions.TelegramApiError`
|
||||||
"""
|
"""
|
||||||
return await api.request(self.session, self.__token, method, data, files,
|
return await api.request(self.session, self.__token, method, data, files,
|
||||||
proxy=self.proxy, proxy_auth=self.proxy_auth,
|
proxy=self.proxy, proxy_auth=self.proxy_auth,
|
||||||
|
|
@ -118,7 +129,8 @@ class BaseBot:
|
||||||
if You want to automatically create destination (:class:`io.BytesIO`) use default
|
if You want to automatically create destination (:class:`io.BytesIO`) use default
|
||||||
value of destination and handle result of this method.
|
value of destination and handle result of this method.
|
||||||
|
|
||||||
:param file_path: String
|
:param file_path: file path on telegram server (You can get it from :obj:`aiogram.types.File`)
|
||||||
|
:type file_path: :obj:`str`
|
||||||
:param destination: filename or instance of :class:`io.IOBase`. For e. g. :class:`io.BytesIO`
|
:param destination: filename or instance of :class:`io.IOBase`. For e. g. :class:`io.BytesIO`
|
||||||
:param timeout: Integer
|
:param timeout: Integer
|
||||||
:param chunk_size: Integer
|
:param chunk_size: Integer
|
||||||
|
|
@ -129,7 +141,7 @@ class BaseBot:
|
||||||
destination = io.BytesIO()
|
destination = io.BytesIO()
|
||||||
|
|
||||||
session = self.create_temp_session()
|
session = self.create_temp_session()
|
||||||
url = api.FILE_URL.format(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:
|
try:
|
||||||
|
|
@ -153,10 +165,10 @@ class BaseBot:
|
||||||
https://core.telegram.org/bots/api#inputfile
|
https://core.telegram.org/bots/api#inputfile
|
||||||
|
|
||||||
:param file_type: field name
|
:param file_type: field name
|
||||||
:param method: API metod
|
:param method: API method
|
||||||
:param file: String or io.IOBase
|
:param file: String or io.IOBase
|
||||||
:param payload: request payload
|
:param payload: request payload
|
||||||
:return: resonse
|
:return: response
|
||||||
"""
|
"""
|
||||||
if file is None:
|
if file is None:
|
||||||
files = {}
|
files = {}
|
||||||
|
|
@ -434,7 +446,7 @@ class BaseBot:
|
||||||
reply_to_message_id: Optional[Integer] = None,
|
reply_to_message_id: Optional[Integer] = None,
|
||||||
reply_markup: Optional[Union[
|
reply_markup: Optional[Union[
|
||||||
types.InlineKeyboardMarkup, types.ReplyKeyboardMarkup, Dict, String]] = None,
|
types.InlineKeyboardMarkup, types.ReplyKeyboardMarkup, Dict, String]] = None,
|
||||||
filename: Optional[str]=None) -> Dict:
|
filename: Optional[str] = None) -> Dict:
|
||||||
"""
|
"""
|
||||||
Use this method to send general files. On success, the sent Message is returned.
|
Use this method to send general files. On success, the sent Message is returned.
|
||||||
Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.
|
Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.
|
||||||
|
|
|
||||||
|
|
@ -4,3 +4,5 @@ BaseBot
|
||||||
This class is base of bot. In BaseBot implemented all available methods of Telegram Bot API.
|
This class is base of bot. In BaseBot implemented all available methods of Telegram Bot API.
|
||||||
|
|
||||||
.. autoclass:: aiogram.bot.base.BaseBot
|
.. autoclass:: aiogram.bot.base.BaseBot
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
|
||||||
|
|
@ -4,4 +4,6 @@ Bot object
|
||||||
That is extended (and recommended for usage) bot class based on BaseBot class.
|
That is extended (and recommended for usage) bot class based on BaseBot class.
|
||||||
You can use instance of that bot in :obj:`aiogram.dispatcher.Dispatcher`
|
You can use instance of that bot in :obj:`aiogram.dispatcher.Dispatcher`
|
||||||
|
|
||||||
.. autoclass:: aiogram.bot.bot.Bot
|
.. autoclass:: aiogram.bot.bot.Bot
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue