mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 02:03:04 +00:00
Migration guide 2.x -> 3.0 (#1143)
* Initial commit for docs cleanup * Update migration guide * More docs * Added changes description * Small fixes
This commit is contained in:
parent
2ecf9cefd7
commit
56f0d9d220
24 changed files with 363 additions and 85 deletions
|
|
@ -1,3 +1,5 @@
|
|||
.. _enums:
|
||||
|
||||
#####
|
||||
Enums
|
||||
#####
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
Use Custom API server
|
||||
=====================
|
||||
|
||||
.. autoclass:: aiogram.client.telegram.TelegramAPIServer
|
||||
:members:
|
||||
|
||||
For example, if you want to use self-hosted API server:
|
||||
|
||||
.. code-block:: python3
|
||||
.. code-block:: python
|
||||
|
||||
session = AiohttpSession(
|
||||
api=TelegramAPIServer.from_base('http://localhost:8082')
|
||||
)
|
||||
bot = Bot(..., session=session)
|
||||
|
||||
.. autoclass:: aiogram.client.telegram.TelegramAPIServer
|
||||
:members:
|
||||
|
|
|
|||
|
|
@ -8,3 +8,4 @@ Client sessions is used for interacting with API server.
|
|||
custom_server
|
||||
base
|
||||
aiohttp
|
||||
middleware
|
||||
|
|
|
|||
75
docs/api/session/middleware.rst
Normal file
75
docs/api/session/middleware.rst
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
##########################
|
||||
Client session middlewares
|
||||
##########################
|
||||
|
||||
In some cases you may want to add some middlewares to the client session to customize the behavior of the client.
|
||||
|
||||
Some useful cases that is:
|
||||
|
||||
- Log the outgoing requests
|
||||
- Customize the request parameters
|
||||
- Handle rate limiting errors and retry the request
|
||||
- others ...
|
||||
|
||||
So, you can do it using client session middlewares.
|
||||
A client session middleware is a function (or callable class) that receives the request and the next middleware to call.
|
||||
The middleware can modify the request and then call the next middleware to continue the request processing.
|
||||
|
||||
How to register client session middleware?
|
||||
==========================================
|
||||
|
||||
Register using register method
|
||||
------------------------------
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
bot.session.middleware(RequestLogging(ignore_methods=[GetUpdates]))
|
||||
|
||||
Register using decorator
|
||||
------------------------
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@bot.session.middleware()
|
||||
async def my_middleware(
|
||||
make_request: NextRequestMiddlewareType[TelegramType],
|
||||
bot: "Bot",
|
||||
method: TelegramMethod[TelegramType],
|
||||
) -> Response[TelegramType]:
|
||||
# do something with request
|
||||
return await make_request(bot, method)
|
||||
|
||||
|
||||
Example
|
||||
=======
|
||||
|
||||
Class based session middleware
|
||||
------------------------------
|
||||
|
||||
.. literalinclude:: ../../../aiogram/client/session/middlewares/request_logging.py
|
||||
:lines: 16-
|
||||
:language: python
|
||||
:linenos:
|
||||
|
||||
.. note::
|
||||
|
||||
this middlewware is already implemented inside aiogram, so, if you want to use it you can
|
||||
just import it :code:`from aiogram.client.session.middlewares.request_logging import RequestLogging`
|
||||
|
||||
|
||||
Function based session middleware
|
||||
---------------------------------
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
async def __call__(
|
||||
self,
|
||||
make_request: NextRequestMiddlewareType[TelegramType],
|
||||
bot: "Bot",
|
||||
method: TelegramMethod[TelegramType],
|
||||
) -> Response[TelegramType]:
|
||||
try:
|
||||
# do something with request
|
||||
return await make_request(bot, method)
|
||||
finally:
|
||||
# do something after request
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
##########
|
||||
ErrorEvent
|
||||
##########
|
||||
|
||||
|
||||
.. automodule:: aiogram.types.error_event
|
||||
:members:
|
||||
:member-order: bysource
|
||||
:undoc-members: True
|
||||
:exclude-members: model_config,model_fields
|
||||
Loading…
Add table
Add a link
Reference in a new issue