Skip to content

Commit 7c7a325

Browse files
committed
Add session_kwargs to Connection
This allows to add arbitrary arguments to each `get` and `post` call. It can be used to add proxy arguments: Connection(session_kwargs={'proxy': <your-proxy>}) See the aiohttp docs for details: https://docs.aiohttp.org/en/stable/client_advanced.html#proxy-support Supersedes and closes #49
1 parent 648e865 commit 7c7a325

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

threema/gateway/_gateway.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class Connection(AioRunMixin):
7171
- `blocking`: Whether to use a blocking API, without the need
7272
for an explicit event loop.
7373
- `session`: An optional :class:`aiohttp.ClientSession`.
74+
- `session_kwargs`: Additional key value arguments passed to the
75+
client session on each call to `get` and `post`.
7476
"""
7577
async_functions = {
7678
'__exit__',
@@ -100,10 +102,11 @@ class Connection(AioRunMixin):
100102
def __init__(
101103
self, identity, secret,
102104
key=None, key_file=None,
103-
blocking=False, session=None,
105+
blocking=False, session=None, session_kwargs=None,
104106
):
105107
super().__init__(blocking=blocking)
106108
self._session = session if session is not None else aiohttp.ClientSession()
109+
self._session_kwargs = session_kwargs if session_kwargs is not None else {}
107110
self._key = None
108111
self._key_file = None
109112
self.id = identity
@@ -307,12 +310,13 @@ async def _get(self, *args, **kwargs):
307310
308311
Return a :class:`aiohttp.ClientResponse` instance.
309312
"""
313+
kwargs = {**self._session_kwargs, **kwargs}
310314
kwargs.setdefault('params', {})
311315
kwargs['params'].setdefault('from', self.id)
312316
kwargs['params'].setdefault('secret', self.secret)
313317
return await self._session.get(*args, **kwargs)
314318

315-
async def _send(self, url, data):
319+
async def _send(self, url, data, **kwargs):
316320
"""
317321
Send a message.
318322
@@ -327,13 +331,14 @@ async def _send(self, url, data):
327331
data.setdefault('secret', self.secret)
328332

329333
# Send message
330-
response = await self._session.post(url, data=data)
334+
kwargs = {**self._session_kwargs, **kwargs}
335+
response = await self._session.post(url, data=data, **kwargs)
331336
if response.status == 200:
332337
return await response.text()
333338
else:
334339
await raise_server_error(response, MessageServerError)
335340

336-
async def _upload(self, url, data):
341+
async def _upload(self, url, data, **kwargs):
337342
"""
338343
Upload a blob.
339344
@@ -349,7 +354,8 @@ async def _upload(self, url, data):
349354
files = {'blob': data}
350355

351356
# Send message
352-
response = await self._session.post(url, params=params, data=files)
357+
kwargs = {**self._session_kwargs, **kwargs}
358+
response = await self._session.post(url, params=params, data=files, **kwargs)
353359
if response.status == 200:
354360
return await response.text()
355361
else:

0 commit comments

Comments
 (0)