Skip to content

Commit 4b9fca0

Browse files
committed
Add error handling for RateLimitedProvider init
1 parent fcbeb7c commit 4b9fca0

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

xbox/webapi/api/provider/ratelimitedprovider.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from typing import Union, Dict
88
from xbox.webapi.api.provider.baseprovider import BaseProvider
9+
from xbox.webapi.common.exceptions import XboxException
910
from xbox.webapi.common.ratelimits.models import LimitType, ParsedRateLimit, TimePeriod
1011
from xbox.webapi.common.ratelimits import CombinedRateLimit
1112

@@ -23,6 +24,23 @@ def __init__(self, client):
2324
"""
2425
super().__init__(client)
2526

27+
# Check that RATE_LIMITS set defined in the child class
28+
if hasattr(self, "RATE_LIMITS"):
29+
# Note: we cannot check (type(self.RATE_LIMITS) == dict) as the type hints have already defined it as such
30+
if "burst" and "sustain" in self.RATE_LIMITS:
31+
# We have the required keys, attempt to parse.
32+
# (type-checking for the values is performed in __parse_rate_limit_key)
33+
self.__handle_rate_limit_setup()
34+
else:
35+
raise XboxException(
36+
"RATE_LIMITS object missing required keys 'burst', 'sustain'"
37+
)
38+
else:
39+
raise XboxException(
40+
"RateLimitedProvider as parent class but RATE_LIMITS not set!"
41+
)
42+
43+
def __handle_rate_limit_setup(self):
2644
# Retrieve burst and sustain from the dict
2745
burst_key = self.RATE_LIMITS["burst"]
2846
sustain_key = self.RATE_LIMITS["sustain"]
@@ -52,3 +70,7 @@ def __parse_rate_limit_key(
5270
# Since the key-value pairs match we can just pass the dict to the model
5371
return ParsedRateLimit(**key, period=period)
5472
# return ParsedRateLimit(read=key["read"], write=key["write"])
73+
else:
74+
raise XboxException(
75+
"RATE_LIMITS value types not recognised. Must be one of 'int, 'dict'."
76+
)

0 commit comments

Comments
 (0)