Skip to content

Commit

Permalink
constants
Browse files Browse the repository at this point in the history
  • Loading branch information
SharpBit committed Dec 8, 2018
1 parent fcf038e commit 6e5aa9c
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## [2.1.6] - 12/8/18
### Added
- Constants extracted from the Brawl Stars App using `Client.get_constants`

## [2.1.5] - 12/5/18
BREAKING CHANGES: Brawl Stars dev team changed "Band" to "Club". This update fixes all of that.
### Changed
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include brawlstats/constants.json
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ Documentation is being hosted on `Read the Docs`_
Misc
~~~~

| If you are currently using this wrapper, feel free to star this repository :)
| If you come across an issue in the wrapper, please `create an issue`_ and I will look into it ASAP.
| If you need help or an API Key, join the API’s `discord server`_.
| If you are currently using this wrapper, feel free to star this repository :)
Examples
~~~~~~~~
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__ = 'v2.1.5'
__version__ = 'v2.1.6'
__title__ = 'brawlstats'
__license__ = 'MIT'
__author__ = 'SharpBit'
Expand Down
1 change: 1 addition & 0 deletions brawlstats/constants.json

Large diffs are not rendered by default.

40 changes: 37 additions & 3 deletions brawlstats/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def get_leaderboard(self, player_or_club: str, count: int=200):
"""
if type(count) != int:
raise ValueError("Make sure 'count' is an int")
if player_or_club.lower() not in ('players', 'clubs') or count > 200 or count < 1:
if player_or_club.lower() not in ('players', 'clubs') or not 0 < count <= 200:
raise ValueError("Please enter 'players' or 'clubs' or make sure 'count' is between 1 and 200.")
url = self.api.leaderboard + '/' + player_or_club + '/' + str(count)
if self.is_async:
Expand All @@ -212,6 +212,34 @@ def get_events(self):

return Events(self, resp, data)

async def _get_constants_async(self, key):
data, resp = await self._aget(self.api.constants)
if key and not data.get(key):
raise KeyError('No such key for Brawl Stars constants "{}"'.format(key))
if key and data.get(key):
return Constants(self, resp, data.get(key))
return Constants(self, resp, data)

def get_constants(self, key=None):
"""Gets Brawl Stars constants extracted from the app.
Parameters
----------
key: Optional[str] = None
Any key to get specific data.
Returns Constants
"""
if self.is_async:
return self._get_constants_async(key)
data, resp = await self._get(self.api.constants)
if key and not data.get(key):
raise KeyError('No such key for Brawl Stars constants "{}"'.format(key))
if key and data.get(key):
return Constants(self, resp, data.get(key))
return Constants(self, resp, data)


class Profile(BaseBox):
"""
Returns a full player object with all of its attributes.
Expand Down Expand Up @@ -298,5 +326,11 @@ class Events(BaseBox):
def __repr__(self):
return '<Events object>'

def __str__(self):
return 'Events object'

class Constants(BaseBox):
"""
Returns some Brawl Stars constants.
"""

def __repr__(self):
return '<Constants object>'
1 change: 1 addition & 0 deletions brawlstats/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ def __init__(self, base_url):
self.club = self.base + '/clubs'
self.leaderboard = self.base + '/leaderboards'
self.events = self.base + '/events'
self.constants = 'https://fourjr.herokuapp.com/bs/constants/'
3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Data Models
.. autoclass:: brawlstats.core.Events
:members:

.. autoclass:: brawlstats.core.Constants
:members:

Profile
-------

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# The short X.Y version
version = '2.1'
# The full version, including alpha/beta/rc tags
release = '2.1.5'
release = '2.1.6'


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='brawlstats',
version='2.1.5',
version='2.1.6',
description='An async Python API wrapper for the unofficial Brawl Stars API',
long_description=long_description,
long_description_content_type='text/x-rst',
Expand Down
11 changes: 11 additions & 0 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ async def test_get_events(self):
events = await self.client.get_events()
self.assertTrue(isinstance(events.current, list))

async def test_get_constants(self):
default = await self.client.get_constants()
self.assertEqual(default.info, 'This data is updated hourly.')
loc = await self.client.get_constants('location')
self.assertTrue(isinstance(loc, list))

async def request():
await self.get_constants(invalid_key)
invalid_key = 'invalid'
self.assertAsyncRaises(KeyError, request)

# Other
async def test_invalid_tag(self):
async def request():
Expand Down
9 changes: 9 additions & 0 deletions tests/test_blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def test_get_events(self):
events = self.client.get_events()
self.assertTrue(isinstance(events.current, list))

def test_get_constants(self):
default = self.client.get_constants()
self.assertEqual(default.info, 'This data is updated hourly.')
loc = self.client.get_constants('location')
self.assertTrue(isinstance(loc, list))
get_constants = self.client.get_constants
invalid_key = 'invalid'
self.assertRaises(KeyError, get_constants, invalid_key)

# Other
def test_invalid_tag(self):
get_profile = self.client.get_profile
Expand Down

0 comments on commit 6e5aa9c

Please sign in to comment.