Skip to content

Commit 5f836c7

Browse files
committed
Allow optional passing of rate_limit in kwargs
* Checks if the rate limit is exceeded, throws exception if True * TODO: Change this to a *middleware* that also increments the counter after a request
1 parent 8b46dd8 commit 5f836c7

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

xbox/webapi/api/client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from xbox.webapi.api.provider.usersearch import UserSearchProvider
2929
from xbox.webapi.api.provider.userstats import UserStatsProvider
3030
from xbox.webapi.authentication.manager import AuthenticationManager
31+
from xbox.webapi.common.exceptions import RateLimitExceededException
32+
from xbox.webapi.common.ratelimits import RateLimit
3133

3234
log = logging.getLogger("xbox.api")
3335

@@ -55,6 +57,9 @@ async def request(
5557
extra_params = kwargs.pop("extra_params", None)
5658
extra_data = kwargs.pop("extra_data", None)
5759

60+
# Rate limit object
61+
rate_limits: RateLimit = kwargs.pop("rate_limits", None)
62+
5863
if include_auth:
5964
# Ensure tokens valid
6065
await self._auth_mgr.refresh_tokens()
@@ -78,6 +83,11 @@ async def request(
7883
data = data or {}
7984
data.update(extra_data)
8085

86+
if rate_limits:
87+
# Check if rate limits have been exceeded for this endpoint
88+
if rate_limits.is_exceeded():
89+
raise RateLimitExceededException("Rate limit exceeded", rate_limits)
90+
8191
return await self._auth_mgr.session.request(
8292
method, url, **kwargs, headers=headers, params=params, data=data
8393
)

0 commit comments

Comments
 (0)