Skip to content

Commit 0585963

Browse files
committed
Make it accept arbitrary kwargs for the session
1 parent 14c4a99 commit 0585963

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

topgg/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class DBLClient(DataContainerMixin):
4949
The default bot_id. You can override this by passing it when calling a method.
5050
session (:class:`aiohttp.ClientSession`)
5151
An `aiohttp session`_ to use for requests to the API.
52+
**kwargs:
53+
Arbitrary kwargs to be passed to :class:`aiohttp.ClientSession` if session was not provided.
5254
"""
5355

5456
__slots__ = ("http", "default_bot_id", "_token", "_is_closed", "_autopost")
@@ -60,6 +62,7 @@ def __init__(
6062
*,
6163
default_bot_id: t.Optional[int] = None,
6264
session: t.Optional[aiohttp.ClientSession] = None,
65+
**kwargs: t.Any,
6366
) -> None:
6467
super().__init__()
6568
self._token = token

topgg/http.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ async def _json_or_text(
5252
class HTTPClient:
5353
"""Represents an HTTP client sending HTTP requests to the Top.gg API.
5454
55-
.. _event loop: https://docs.python.org/3/library/asyncio-eventloops.html
5655
.. _aiohttp session: https://aiohttp.readthedocs.io/en/stable/client_reference.html#client-session
5756
5857
Args:
@@ -62,17 +61,17 @@ class HTTPClient:
6261
Keyword Arguments:
6362
session: `aiohttp session`_
6463
The `aiohttp session`_ used for requests to the API.
65-
loop: `event loop`_
66-
An `event loop`_ used for asynchronous operations.
64+
**kwargs:
65+
Arbitrary kwargs to be passed to :class:`aiohttp.ClientSession`.
6766
"""
6867

69-
def __init__(self, token: str, **kwargs: Any) -> None:
68+
def __init__(
69+
self, token: str, *, session: aiohttp.ClientSession = None, **kwargs: Any
70+
) -> None:
7071
self.BASE = "https://top.gg/api"
7172
self.token = token
72-
self.loop = kwargs.get("loop") or asyncio.get_event_loop()
73-
session = kwargs.get("session")
7473
self._own_session = session is None
75-
self.session = session or aiohttp.ClientSession(loop=self.loop)
74+
self.session = session or aiohttp.ClientSession(**kwargs)
7675
self.global_rate_limiter = AsyncRateLimiter(
7776
max_calls=99, period=1, callback=_rate_limit_handler
7877
)
@@ -140,7 +139,7 @@ async def request(self, method: str, endpoint: str, **kwargs: Any) -> dict:
140139
# if is_global:
141140
# self._global_over.clear()
142141

143-
await asyncio.sleep(retry_after, loop=self.loop)
142+
await asyncio.sleep(retry_after)
144143
_LOGGER.debug("Done sleeping for the ratelimit. Retrying...")
145144

146145
# release the global lock now that the

0 commit comments

Comments
 (0)