mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-08 17:13:56 +00:00
feat(proxy): proxy for aiohttp,base sessions (#284)
* feat(proxy): proxy for aiohttp,base sessions Add support for proxies in aiohttp session with aiohttp_socks library, edit BaseSession class to support proxies for other sessions in future. * fix(annotation): missing underscore before "private" typevar * chore: remove redundant of proxy_url schema for socks version * test: add missing test Add missing test, remove BaseSession.cfg and switch to implementing class' "private" traits, add aiohttp_socks in dependency list as optional and extra. * feat(session): Implement asyncio session for requests [wip] * feat(proxy chain): Chained proxy support in aiohttp session Add ChainProxyConnector support, !pin pydantic to "1.4", add documentation on aiohttp connector. * style(mypy): apply linter changes * tests(mock): remove await for magic mock * fix dangling dependency * refactor(generic): get rid of generic behaviour for base session
This commit is contained in:
parent
2553f5f19e
commit
15bcc0ba9f
8 changed files with 245 additions and 8 deletions
76
docs/api/client/session/aiohttp.md
Normal file
76
docs/api/client/session/aiohttp.md
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
# Aiohttp session
|
||||
|
||||
AiohttpSession represents a wrapper-class around `ClientSession` from [aiohttp]('https://pypi.org/project/aiohttp/')
|
||||
|
||||
Currently `AiohttpSession` is a default session used in `aiogram.Bot`
|
||||
|
||||
## Usage example
|
||||
|
||||
```python
|
||||
from aiogram import Bot
|
||||
from aiogram.api.client.session.aiohttp import AiohttpSession
|
||||
|
||||
session = AiohttpSession()
|
||||
Bot('token', session=session)
|
||||
```
|
||||
|
||||
|
||||
## Proxy requests in AiohttpSession
|
||||
|
||||
In order to use AiohttpSession with proxy connector you have to install [aiohttp-socks]('https://pypi.org/project/aiohttp-socks/')
|
||||
|
||||
Binding session to bot:
|
||||
```python
|
||||
from aiogram import Bot
|
||||
from aiogram.api.client.session.aiohttp import AiohttpSession
|
||||
|
||||
session = AiohttpSession(proxy="protocol://host:port/")
|
||||
Bot(token="bot token", session=session)
|
||||
```
|
||||
|
||||
!!! note "Protocols"
|
||||
Only following protocols are supported: http(tunneling), socks4(a), socks5 as aiohttp_socks documentation claims.
|
||||
|
||||
|
||||
### Authorization
|
||||
|
||||
Proxy authorization credentials can be specified in proxy URL or come as an instance of `aiohttp.BasicAuth` containing
|
||||
login and password.
|
||||
|
||||
Consider examples:
|
||||
```python
|
||||
from aiohttp import BasicAuth
|
||||
from aiogram.api.client.session.aiohttp import AiohttpSession
|
||||
|
||||
auth = BasicAuth(login="user", password="password")
|
||||
session = AiohttpSession(proxy=("protocol://host:port", auth))
|
||||
# or simply include your basic auth credential in URL
|
||||
session = AiohttpSession(proxy="protocol://user:password@host:port")
|
||||
```
|
||||
|
||||
!!! note "Credential priorities"
|
||||
Aiogram prefers `BasicAuth` over username and password in URL, so
|
||||
if proxy URL contains login and password and `BasicAuth` object is passed at the same time
|
||||
aiogram will use login and password from `BasicAuth` instance.
|
||||
|
||||
|
||||
### Proxy chains
|
||||
|
||||
Since [aiohttp-socks]('https://pypi.org/project/aiohttp-socks/') supports proxy chains, you're able to use them in aiogram
|
||||
|
||||
Example of chain proxies:
|
||||
```python
|
||||
from aiohttp import BasicAuth
|
||||
from aiogram.api.client.session.aiohttp import AiohttpSession
|
||||
|
||||
auth = BasicAuth(login="user", password="password")
|
||||
session = AiohttpSession(
|
||||
proxy={"protocol0://host0:port0",
|
||||
"protocol1://user:password@host1:port1",
|
||||
("protocol2://host2:port2", auth),} # can be any iterable if not set
|
||||
)
|
||||
```
|
||||
|
||||
## Location
|
||||
|
||||
- `from aiogram.api.client.session.aiohttp import AiohttpSession`
|
||||
Loading…
Add table
Add a link
Reference in a new issue