Skip to content

Commit b20fce0

Browse files
committed
GPI-661: Add docs to http module
1 parent dec59f4 commit b20fce0

File tree

5 files changed

+85
-13
lines changed

5 files changed

+85
-13
lines changed

docs/source/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
]
3939
autodoc_member_order = 'bysource'
4040
autodoc_inherit_docstrings = False
41-
autodoc_mock_imports = ["galaxy.http"]
41+
autodoc_mock_imports = ["aiohttp"]
4242

4343
set_type_checking_flag = True
4444

docs/source/galaxy.http.rst

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
galaxy.http
2+
=================
3+
4+
.. automodule:: galaxy.http
5+
:members:
6+
:special-members: __init__
7+
:undoc-members:
8+
:show-inheritance:

docs/source/index.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ GOG Galaxy Integrations Python API
66
:includehidden:
77

88
Overview <overview>
9-
API <galaxy.api>
9+
galaxy.api
10+
galaxy.http
1011
Platform ID's <platforms>
1112

1213
Index

src/galaxy/http.py

+70-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
"""
2+
This module standarize http traffic and the error handling for further communication with the GOG Galaxy 2.0.
3+
4+
It is recommended to use provided convenient methods for HTTP requests, especially when dealing with authorized sessions.
5+
Examplary simple web service could looks like:
6+
7+
.. code-block:: python
8+
9+
import logging
10+
from galaxy.http import create_client_session, handle_exception
11+
12+
class BackendClient:
13+
AUTH_URL = 'my-integration.com/auth'
14+
HEADERS = {
15+
"My-Custom-Header": "true",
16+
}
17+
def __init__(self):
18+
self._session = create_client_session(headers=self.HEADERS)
19+
20+
async def authenticate(self):
21+
await self._session.request('POST', self.AUTH_URL)
22+
23+
async def close(self):
24+
# to be called on plugin shutdown
25+
await self._session.close()
26+
27+
async def _authorized_request(self, method, url, *args, **kwargs):
28+
with handle_exceptions():
29+
return await self._session.request(method, url, *args, **kwargs)
30+
"""
31+
132
import asyncio
233
import ssl
334
from contextlib import contextmanager
@@ -13,41 +44,74 @@
1344
)
1445

1546

47+
#: Default limit of the simultaneous connections for ssl connector.
1648
DEFAULT_LIMIT = 20
17-
DEFAULT_TIMEOUT = 60 # seconds
49+
#: Default timeout in seconds used for client session.
50+
DEFAULT_TIMEOUT = 60
1851

1952

2053
class HttpClient:
21-
"""Deprecated"""
54+
"""
55+
.. deprecated:: 0.41
56+
Use http module functions instead
57+
"""
2258
def __init__(self, limit=DEFAULT_LIMIT, timeout=aiohttp.ClientTimeout(total=DEFAULT_TIMEOUT), cookie_jar=None):
2359
connector = create_tcp_connector(limit=limit)
2460
self._session = create_client_session(connector=connector, timeout=timeout, cookie_jar=cookie_jar)
2561

2662
async def close(self):
63+
"""Closes connection. Should be called in :meth:`~galaxy.api.plugin.Plugin.shutdown`"""
2764
await self._session.close()
2865

2966
async def request(self, method, url, *args, **kwargs):
3067
with handle_exception():
3168
return await self._session.request(method, url, *args, **kwargs)
3269

3370

34-
def create_tcp_connector(*args, **kwargs):
71+
def create_tcp_connector(*args, **kwargs) -> aiohttp.TCPConnector:
72+
"""
73+
Creates TCP connector with resonable defaults.
74+
For details about available parameters refer to
75+
`aiohttp.TCPConnector <https://docs.aiohttp.org/en/stable/client_reference.html#tcpconnector>`_
76+
"""
3577
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
3678
ssl_context.load_verify_locations(certifi.where())
3779
kwargs.setdefault("ssl", ssl_context)
3880
kwargs.setdefault("limit", DEFAULT_LIMIT)
39-
return aiohttp.TCPConnector(*args, **kwargs)
81+
return aiohttp.TCPConnector(*args, **kwargs) # type: ignore due to https://github.com/python/mypy/issues/4001
82+
83+
84+
def create_client_session(*args, **kwargs) -> aiohttp.ClientSession:
85+
"""
86+
Creates client session with resonable defaults.
87+
For details about available parameters refer to
88+
`aiohttp.ClientSession <https://docs.aiohttp.org/en/stable/client_reference.html>`_
4089
90+
Examplary customization:
4191
42-
def create_client_session(*args, **kwargs):
92+
.. code-block:: python
93+
94+
from galaxy.http import create_client_session, create_tcp_connector
95+
96+
session = create_client_session(
97+
headers={
98+
"Keep-Alive": "true"
99+
},
100+
connector=create_tcp_connector(limit=40),
101+
timeout=100)
102+
"""
43103
kwargs.setdefault("connector", create_tcp_connector())
44104
kwargs.setdefault("timeout", aiohttp.ClientTimeout(total=DEFAULT_TIMEOUT))
45105
kwargs.setdefault("raise_for_status", True)
46-
return aiohttp.ClientSession(*args, **kwargs)
106+
return aiohttp.ClientSession(*args, **kwargs) # type: ignore due to https://github.com/python/mypy/issues/4001
47107

48108

49109
@contextmanager
50110
def handle_exception():
111+
"""
112+
Context manager translating network related exceptions
113+
to custom :mod:`~galaxy.api.errors`.
114+
"""
51115
try:
52116
yield
53117
except asyncio.TimeoutError:
@@ -78,4 +142,3 @@ def handle_exception():
78142
except aiohttp.ClientError:
79143
logging.exception("Caught exception while performing request")
80144
raise UnknownError()
81-

src/galaxy/unittest/mock.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
class AsyncMock(MagicMock):
66
"""
7-
..deprecated:: 0.45
8-
Use: :class:`MagicMock` with meth:`~.async_return_value`.
7+
.. deprecated:: 0.45
8+
Use: :class:`MagicMock` with meth:`~.async_return_value`.
99
"""
1010
async def __call__(self, *args, **kwargs):
1111
return super(AsyncMock, self).__call__(*args, **kwargs)
1212

1313

1414
def coroutine_mock():
1515
"""
16-
..deprecated:: 0.45
17-
Use: :class:`MagicMock` with meth:`~.async_return_value`.
16+
.. deprecated:: 0.45
17+
Use: :class:`MagicMock` with meth:`~.async_return_value`.
1818
"""
1919
coro = MagicMock(name="CoroutineResult")
2020
corofunc = MagicMock(name="CoroutineFunction", side_effect=asyncio.coroutine(coro))

0 commit comments

Comments
 (0)