Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/SharpBit/brawlstats
Browse files Browse the repository at this point in the history
…into development
  • Loading branch information
SharpBit committed Oct 6, 2024
2 parents 5e1ba0a + a0593cc commit 76215a3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Change Log
All notable changes to this project will be documented in this file.

## [4.1.1] - 10/31/21
### Fixed
- Installation dependency issue with aiohttp

## [4.1.0] - 1/4/21
### Added
- `use_cache` parameter for methods to specify whether or not to use cache for a specific API call
### Changed
- Wrapper no longer replaces the letter O with 0 for tags to better represent a valid tag

## [4.0.6] - 10/6/20
### Changed
- Format of the string of `utils.get_datetime`
Expand Down
2 changes: 1 addition & 1 deletion brawlstats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
############


__version__ = 'v4.0.6'
__version__ = 'v4.1.1'
__title__ = 'brawlstats'
__license__ = 'MIT'
__author__ = 'SharpBit'
Expand Down
71 changes: 49 additions & 22 deletions brawlstats/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,14 @@ def _resolve_cache(self, url):
log.debug('GET {} got result from cache.'.format(url))
return data

async def _arequest(self, url):
async def _arequest(self, url, use_cache=True):
"""Async method to request a url."""
# Try and retrieve from cache
cache = self._resolve_cache(url)
if use_cache:
cache = self._resolve_cache(url)
else:
cache = None

if cache is not None:
return cache

Expand All @@ -145,13 +149,16 @@ async def _arequest(self, url):

return data

def _request(self, url):
def _request(self, url, use_cache=True):
"""Sync method to request a url."""
if self.is_async:
return self._arequest(url)
return self._arequest(url, use_cache)

# Try and retrieve from cache
cache = self._resolve_cache(url)
if use_cache:
cache = self._resolve_cache(url)
else:
cache = None
if cache is not None:
return cache

Expand All @@ -166,12 +173,12 @@ def _request(self, url):

return data

async def _aget_model(self, url, model, key=None):
async def _aget_model(self, url, model, use_cache=True, key=None):
"""Method to turn the response data into a Model class for the async client."""
if self.prevent_ratelimit:
# Use self.lock if prevent_ratelimit=True
async with self.lock:
data = await self._arequest(url)
data = await self._arequest(url, use_cache)
await asyncio.sleep(0.1)
else:
data = await self._arequest(url)
Expand All @@ -185,13 +192,13 @@ async def _aget_model(self, url, model, key=None):

return model(self, data)

def _get_model(self, url, model, key=None):
def _get_model(self, url, model, use_cache=True, key=None):
"""Method to turn the response data into a Model class for the sync client."""
if self.is_async:
# Calls the async function
return self._aget_model(url, model=model, key=key)
return self._aget_model(url, model=model, use_cache=use_cache, key=key)

data = self._request(url)
data = self._request(url, use_cache)
if self.prevent_ratelimit:
time.sleep(0.1)

Expand All @@ -205,80 +212,91 @@ def _get_model(self, url, model, key=None):
return model(self, data)

@typecasted
def get_player(self, tag: bstag) -> Player:
def get_player(self, tag: bstag, use_cache=True) -> Player:
"""Gets a player's stats.
Parameters
----------
tag : str
A valid player tag.
Valid characters: 0289PYLQGRJCUV
use_cache : bool, optional
Whether to use the internal 3 minutes cache, by default True
Returns
-------
Player
A player object with all of its attributes.
"""
url = '{}/{}'.format(self.api.PROFILE, tag)
return self._get_model(url, model=Player)
return self._get_model(url, model=Player, use_cache=use_cache)

get_profile = get_player

@typecasted
def get_battle_logs(self, tag: bstag) -> BattleLog:
def get_battle_logs(self, tag: bstag, use_cache=True) -> BattleLog:
"""Gets a player's battle logs.
Parameters
----------
tag : str
A valid player tag.
Valid characters: 0289PYLQGRJCUV
use_cache : bool, optional
Whether to use the internal 3 minutes cache, by default True
Returns
-------
BattleLog
A player battle object with all of its attributes.
"""
url = '{}/{}/battlelog'.format(self.api.PROFILE, tag)
return self._get_model(url, model=BattleLog)
return self._get_model(url, model=BattleLog, use_cache=use_cache)

@typecasted
def get_club(self, tag: bstag) -> Club:
def get_club(self, tag: bstag, use_cache=True) -> Club:
"""Gets a club's stats.
Parameters
----------
tag : str
A valid club tag.
Valid characters: 0289PYLQGRJCUV
use_cache : bool, optional
Whether to use the internal 3 minutes cache, by default True
Returns
-------
Club
A club object with all of its attributes.
"""
url = '{}/{}'.format(self.api.CLUB, tag)
return self._get_model(url, model=Club)
return self._get_model(url, model=Club, use_cache=use_cache)

@typecasted
def get_club_members(self, tag: bstag) -> Members:
def get_club_members(self, tag: bstag, use_cache=True) -> Members:
"""Gets the members of a club.
Parameters
----------
tag : str
A valid club tag.
Valid characters: 0289PYLQGRJCUV
use_cache : bool, optional
Whether to use the internal 3 minutes cache, by default True
Returns
-------
Members
A list of the members in a club.
"""
url = '{}/{}/members'.format(self.api.CLUB, tag)
return self._get_model(url, model=Members)
return self._get_model(url, model=Members, use_cache=use_cache)

def get_rankings(self, *, ranking: str, region: str=None, limit: int=200, brawler: Union[str, int]=None) -> Ranking:
def get_rankings(
self, *, ranking: str, region: str=None, limit: int=200,
brawler: Union[str, int]=None, use_cache=True
) -> Ranking:
"""Gets the top count players/clubs/brawlers.
Parameters
Expand All @@ -291,6 +309,8 @@ def get_rankings(self, *, ranking: str, region: str=None, limit: int=200, brawle
The number of top players or clubs to fetch, by default 200
brawler : Union[str, int], optional
The brawler name or ID, by default None
use_cache : bool, optional
Whether to use the internal 3 minutes cache, by default True
Returns
-------
Expand Down Expand Up @@ -331,15 +351,17 @@ def get_rankings(self, *, ranking: str, region: str=None, limit: int=200, brawle
if ranking == 'brawlers':
url = '{}/{}/{}/{}?limit={}'.format(self.api.RANKINGS, region, ranking, brawler, limit)

return self._get_model(url, model=Ranking)
return self._get_model(url, model=Ranking, use_cache=use_cache)

def get_constants(self, key: str=None) -> Constants:
def get_constants(self, key: str=None, use_cache=True) -> Constants:
"""Gets Brawl Stars constants extracted from the app.
Parameters
----------
key : str, optional
Any key to get specific data, by default None
use_cache : bool, optional
Whether to use the internal 3 minutes cache, by default True
Returns
-------
Expand All @@ -348,9 +370,14 @@ def get_constants(self, key: str=None) -> Constants:
"""
return self._get_model(self.api.CONSTANTS, model=Constants, key=key)

def get_brawlers(self) -> Brawlers:
def get_brawlers(self, use_cache=True) -> Brawlers:
"""Gets available brawlers and information about them.
Parameters
----------
use_cache : bool, optional
Whether to use the internal 3 minutes cache, by default True
Returns
-------
Brawlers
Expand Down
2 changes: 1 addition & 1 deletion brawlstats/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def set_brawlers(self, brawlers):


def bstag(tag):
tag = tag.strip('#').upper().replace('O', '0')
tag = tag.strip('#').upper()
allowed = '0289PYLQGRJCUV'

if len(tag) < 3:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
aiohttp>=3.6.0,<3.7.0
aiohttp>=3.6.0
requests
python-box
cachetools>=3.1.0

0 comments on commit 76215a3

Please sign in to comment.