Skip to content

Commit

Permalink
new url+rate limit kinda fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SharpBit committed May 21, 2019
1 parent f23d3d7 commit 42ae945
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Change Log
All notable changes to this project will be documented in this file.

## [2.3.8] - 5/21/19
### Changed
- Changed the Base URL back to the new URL.
- Now waits the number of seconds instead of raising a `RateLimitError` when a rate limit will be detected BEFORE it requests.

## [2.3.7] - 5/5/19
### Changed
- Changed the BASE URL to the old API URL. VERSION 2.3.6 WILL NOT WORK DUE TO API TIMEOUT ISSUES. PLEASE UPDATE.
Expand Down
2 changes: 1 addition & 1 deletion brawlstats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
############


__version__ = 'v2.3.7'
__version__ = 'v2.3.8'
__title__ = 'brawlstats'
__license__ = 'MIT'
__author__ = 'SharpBit'
Expand Down
2 changes: 1 addition & 1 deletion brawlstats/constants.json

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions brawlstats/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(self, token, session=None, timeout=10, is_async=False, **options):
aiohttp.ClientSession(loop=self.loop, connector=self.connector) if self.is_async else requests.Session()
)
self.timeout = timeout
self.prevent_ratelimit = options.get('prevent_ratelimit', False)
self.lock = asyncio.Lock() if options.get('prevent_ratelimit') is True else None
self.api = API(options.get('base_url'))

Expand Down Expand Up @@ -123,8 +124,8 @@ async def _arequest(self, url):
cache = self._resolve_cache(url)
if cache is not None:
return cache
if self.ratelimit[1] == 0 and time.time() < self.ratelimit[2] / 1000:
raise RateLimitError(url, 429, self.ratelimit[2] / 1000 - time.time())
if self.ratelimit[1] == 0 and time.time() < self.ratelimit[2]:
raise RateLimitError(url, 429, self.ratelimit[2] - time.time())

try:
async with self.session.get(url, timeout=self.timeout, headers=self.headers) as resp:
Expand All @@ -140,8 +141,8 @@ def _request(self, url):
cache = self._resolve_cache(url)
if cache is not None:
return cache
if self.ratelimit[1] == 0 and time.time() < self.ratelimit[2] / 1000:
raise RateLimitError(url, 429, self.ratelimit[2] / 1000 - time.time())
if self.ratelimit[1] == 0 and time.time() < self.ratelimit[2]:
asyncio.sleep(self.ratelimit[2] - time.time())

try:
with self.session.get(url, timeout=self.timeout, headers=self.headers) as resp:
Expand Down Expand Up @@ -173,7 +174,9 @@ async def _aget_model(self, url, model, key=None):
def _get_model(self, url, model, key=None):
if self.is_async:
return self._aget_model(url, model=model, key=key)
data, resp = self._request(url)
if self.prevent_ratelimit:
data, resp = self._request(url)
time.sleep(1 / self.ratelimit[0])
if model == Constants:
if key and not data.get(key):
raise KeyError('No such key for Brawl Stars constants "{}"'.format(key))
Expand Down
2 changes: 1 addition & 1 deletion brawlstats/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class API:
def __init__(self, base_url):
self.BASE = base_url or 'https://brawlapi.cf/api'
self.BASE = base_url or 'https://api.brawlapi.cf/v1'
self.PROFILE = self.BASE + '/player'
self.CLUB = self.BASE + '/club'
self.LEADERBOARD = self.BASE + '/leaderboards'
Expand Down
3 changes: 2 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ Indices
* :ref:`genindex`
* :ref:`search`

.. _Brawl Stars API: https://brawlapi.cf/api
.. _Brawl Stars API: https://brawlapi.cf/api
.. _discord server: https://discord.me/BrawlAPI
10 changes: 9 additions & 1 deletion tests/test_async.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asynctest
import datetime
import logging
import os

import brawlstats
Expand All @@ -17,7 +18,14 @@ class TestAsyncClient(asynctest.TestCase):
async def setUp(self):
self.player_tag = 'GGJVJLU2'
self.club_tag = 'QCGV8PG'
self.client = brawlstats.Client(TOKEN, is_async=True, timeout=30, prevent_ratelimit=True)
self.client = brawlstats.Client(
TOKEN,
is_async=True,
timeout=30,
prevent_ratelimit=True,
debug=True
)
logging.basicConfig(level=logging.DEBUG)

async def tearDown(self):
await self.client.close()
Expand Down
10 changes: 9 additions & 1 deletion tests/test_blocking.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import logging
import unittest
import os

Expand All @@ -18,7 +19,14 @@ class TestBlockingClient(unittest.TestCase):
def setUp(self):
self.player_tag = 'GGJVJLU2'
self.club_tag = 'QCGV8PG'
self.client = brawlstats.Client(TOKEN, is_async=False, timeout=30, prevent_ratelimit=True)
self.client = brawlstats.Client(
TOKEN,
is_async=False,
timeout=30,
prevent_ratelimit=True,
debug=True
)
logging.basicConfig(level=logging.DEBUG)

def tearDown(self):
self.client.close()
Expand Down

0 comments on commit 42ae945

Please sign in to comment.