Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add session arguments to Connection #56

Merged
merged 1 commit into from
May 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions threema/gateway/_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class Connection(AioRunMixin):
- `blocking`: Whether to use a blocking API, without the need
for an explicit event loop.
- `session`: An optional :class:`aiohttp.ClientSession`.
- `session_kwargs`: Additional key value arguments passed to the
client session on each call to `get` and `post`.
"""
async_functions = {
'__exit__',
Expand Down Expand Up @@ -100,10 +102,11 @@ class Connection(AioRunMixin):
def __init__(
self, identity, secret,
key=None, key_file=None,
blocking=False, session=None,
blocking=False, session=None, session_kwargs=None,
):
super().__init__(blocking=blocking)
self._session = session if session is not None else aiohttp.ClientSession()
self._session_kwargs = session_kwargs if session_kwargs is not None else {}
self._key = None
self._key_file = None
self.id = identity
Expand Down Expand Up @@ -307,12 +310,13 @@ async def _get(self, *args, **kwargs):

Return a :class:`aiohttp.ClientResponse` instance.
"""
kwargs = {**self._session_kwargs, **kwargs}
kwargs.setdefault('params', {})
kwargs['params'].setdefault('from', self.id)
kwargs['params'].setdefault('secret', self.secret)
return await self._session.get(*args, **kwargs)

async def _send(self, url, data):
async def _send(self, url, data, **kwargs):
"""
Send a message.

Expand All @@ -327,13 +331,14 @@ async def _send(self, url, data):
data.setdefault('secret', self.secret)

# Send message
response = await self._session.post(url, data=data)
kwargs = {**self._session_kwargs, **kwargs}
response = await self._session.post(url, data=data, **kwargs)
if response.status == 200:
return await response.text()
else:
await raise_server_error(response, MessageServerError)

async def _upload(self, url, data):
async def _upload(self, url, data, **kwargs):
"""
Upload a blob.

Expand All @@ -349,7 +354,8 @@ async def _upload(self, url, data):
files = {'blob': data}

# Send message
response = await self._session.post(url, params=params, data=files)
kwargs = {**self._session_kwargs, **kwargs}
response = await self._session.post(url, params=params, data=files, **kwargs)
if response.status == 200:
return await response.text()
else:
Expand Down