-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathratelimitedprovider.py
More file actions
77 lines (65 loc) · 2.99 KB
/
ratelimitedprovider.py
File metadata and controls
77 lines (65 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
RateLimitedProvider
Subclassed by providers with rate limit support
"""
from typing import Dict, Union
from xbox.webapi.api.provider.baseprovider import BaseProvider
from xbox.webapi.common.exceptions import XboxException
from xbox.webapi.common.ratelimits import CombinedRateLimit
from xbox.webapi.common.ratelimits.models import LimitType, ParsedRateLimit, TimePeriod
class RateLimitedProvider(BaseProvider):
# dict -> Dict (typing.dict) https://stackoverflow.com/a/63460173
RATE_LIMITS: Dict[str, Union[int, Dict[str, int]]]
def __init__(self, client):
"""
Initialize Baseclass
Args:
client (:class:`XboxLiveClient`): Instance of XboxLiveClient
"""
super().__init__(client)
# Check that RATE_LIMITS set defined in the child class
if hasattr(self, "RATE_LIMITS"):
# Note: we cannot check (type(self.RATE_LIMITS) == dict) as the type hints have already defined it as such
if "burst" and "sustain" in self.RATE_LIMITS:
# We have the required keys, attempt to parse.
# (type-checking for the values is performed in __parse_rate_limit_key)
self.__handle_rate_limit_setup()
else:
raise XboxException(
"RATE_LIMITS object missing required keys 'burst', 'sustain'"
)
else:
raise XboxException(
"RateLimitedProvider as parent class but RATE_LIMITS not set!"
)
def __handle_rate_limit_setup(self):
# Retrieve burst and sustain from the dict
burst_key = self.RATE_LIMITS["burst"]
sustain_key = self.RATE_LIMITS["sustain"]
# Parse the rate limit dict values
burst_rate_limits = self.__parse_rate_limit_key(burst_key, TimePeriod.BURST)
sustain_rate_limits = self.__parse_rate_limit_key(
sustain_key, TimePeriod.SUSTAIN
)
# Instanciate CombinedRateLimits for read and write respectively
self.rate_limit_read = CombinedRateLimit(
burst_rate_limits, sustain_rate_limits, type=LimitType.READ
)
self.rate_limit_write = CombinedRateLimit(
burst_rate_limits, sustain_rate_limits, type=LimitType.WRITE
)
def __parse_rate_limit_key(
self, key: Union[int, Dict[str, int]], period: TimePeriod
) -> ParsedRateLimit:
if isinstance(key, int) and not isinstance(key, bool):
# bool is a subclass of int, hence the explicit check
return ParsedRateLimit(read=key, write=key, period=period)
elif isinstance(key, dict):
# TODO: schema here?
# Since the key-value pairs match we can just pass the dict to the model
return ParsedRateLimit(**key, period=period)
# return ParsedRateLimit(read=key["read"], write=key["write"])
else:
raise XboxException(
"RATE_LIMITS value types not recognised. Must be one of 'int, 'dict'."
)