@@ -24,7 +24,7 @@ class Client:
2424 Parameters
2525 ------------
2626 token: str
27- The API Key that you can get from https://discord.me/BrawlAPI
27+ The API Key that you can get from https://brawlapi.cf/dashboard
2828 session: Optional[Session] = None
2929 Use a current session or a make new one. Can be ``aiohttp.ClientSession()`` or ``requests.Session()``
3030 timeout: Optional[int] = 10
@@ -42,6 +42,9 @@ class Client:
4242 Whether or not to give you more info to debug easily.
4343 base_url: Optional[str] = None
4444 Sets a different base URL to make request to. Only use this if you know what you are doing.
45+ prevent_ratelimit: Optional[bool] = False
46+ Whether or not you want to wait for a small amount of time between requests to prevent being ratelimited.
47+ Recommended if you are performing multiple requests in a short period of time.
4548 """
4649
4750 REQUEST_LOG = '{method} {url} recieved {text} has returned {status}'
@@ -54,10 +57,13 @@ def __init__(self, token, session=None, timeout=10, is_async=False, **options):
5457 aiohttp .ClientSession (loop = self .loop , connector = self .connector ) if self .is_async else requests .Session ()
5558 )
5659 self .timeout = timeout
60+ self .lock = asyncio .Lock () if options .get ('prevent_ratelimit' ) is True else None
5761 self .api = API (options .get ('base_url' ))
62+
5863 self .debug = options .get ('debug' , False )
5964 self .cache = TTLCache (900 , 180 ) # 5 requests/sec
60- self .ratelimit = [5 , 5 , 0 ] # per second, remaining, time until reset
65+ self .ratelimit = [3 , 3 , 0 ] # per second, remaining, time until reset
66+
6167 self .headers = {
6268 'Authorization' : token ,
6369 'User-Agent' : 'brawlstats/{0} (Python {1[0]}.{1[1]})' .format (self .api .VERSION , sys .version_info ),
@@ -148,7 +154,13 @@ def _request(self, url):
148154 return data
149155
150156 async def _aget_model (self , url , model , key = None ):
151- data , resp = await self ._arequest (url )
157+ if self .lock is not None :
158+ async with self .lock :
159+ data , resp = await self ._arequest (url )
160+ await asyncio .sleep (1 / self .ratelimit [0 ])
161+ else :
162+ data , resp = await self ._arequest (url )
163+
152164 if model == Constants :
153165 if key and not data .get (key ):
154166 raise KeyError ('No such key for Brawl Stars constants "{}"' .format (key ))
0 commit comments