Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from trevorphillips/hearthstone
Browse files Browse the repository at this point in the history
adding hearthstone
  • Loading branch information
Trevor Phillips authored Nov 12, 2020
2 parents 1a2ec97 + ed378c1 commit 7113953
Show file tree
Hide file tree
Showing 29 changed files with 514 additions and 621 deletions.
17 changes: 11 additions & 6 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
- name: Lint with bandit
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
bandit blizzardapi
- name: Lint with mypy
run: |
mypy blizzardapi
- name: Lint with pydocstyle
run: |
pydocstyle blizzardapi
- name: Lint with pylint
run: |
pylint blizzardapi --exit-zero
- name: Test with pytest
run: |
pytest
Expand Down
21 changes: 16 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
.PHONY: devinstall clean lint

devinstall:
pip install -r requirements.txt

clean:
rm -rf dist
find . -type d -name __pycache__ -delete
find . -type f -name '*.py[co]' -delete
rm -rf .coverage
rm -rf .pytest_cache
rm -rf build
rm -rf dist
rm -rf docs
rm -rf htmlcov
rm -rf .mypy_cache
rm -rf python_blizzardapi.egg-info
rm -rf .coverage
rm -rf docs
rm -rf .pytest_cache
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete

lint:
black . -l 200 && black . -l 79
bandit blizzardapi
mypy blizzardapi
pydocstyle blizzardapi
pylint blizzardapi --exit-zero
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ python-blizzardapi is a client library for Blizzard's APIs.

Current supported features include:
- Battle.net User
- WoW Profile
- WoW Game Data
- WoW Classic Game Data
- Wow Profile
- Wow Game Data
- Wow Classic Game Data
- Diablo 3 Community
- Diablo 3 Game Data
- Hearthstone Game Data

To gain access to Blizzard's API please register [here](https://develop.battle.net/access/) to obtain a client id and client secret.

Expand Down Expand Up @@ -40,7 +41,7 @@ categories_index = api_client.wow.game_data.get_achievement_categories_index("us
# Protected API endpoint
summary = api_client.wow.profile.get_account_profile_summary("us", "en_US", "access_token")

# WoW Classic endpoint
# Wow Classic endpoint
connected_realms_index = api_client.wow.game_data.get_connected_realms_index("us", "en_US", is_classic=True)
```

Expand Down
4 changes: 3 additions & 1 deletion blizzardapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
"""__init__.py file."""
from requests.exceptions import * # noqa

from .blizzard_api import BlizzardApi # noqa
from .exceptions import BlizzardApiRequestException # noqa
48 changes: 33 additions & 15 deletions blizzardapi/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
"""api.py file."""
import requests
from requests.exceptions import RequestException

from .exceptions import BlizzardApiRequestException


class Api:
"""Base API class.
Attributes:
_client_id: A string client id supplied by Blizzard.
_client_secret: A string client secret supplied by Blizzard.
_access_token: A string access token that is used to access Blizzard's API.
_api_url: A string url used to call the API endpoints.
_api_url_cn: A string url used to call the china API endpoints.
_oauth_url: A string url used to call the OAuth API endpoints.
_oauth_url_cn: A string url used to call the china OAuth API endpoints.
_session: An open requests.Session instance.
"""

def __init__(self, client_id, client_secret):
"""Init Api."""
self._client_id = client_id
self._client_secret = client_secret
self._access_token = None
Expand All @@ -19,39 +31,42 @@ def __init__(self, client_id, client_secret):
self._session = requests.Session()

def _get_client_token(self, region):
"""Fetch an access token based on client id and client secret credentials.
Args:
region:
A string containing a region.
"""
url = self._format_oauth_url("/oauth/token", region)
query_params = {"grant_type": "client_credentials"}

try:
response = self._session.post(
url,
params=query_params,
auth=(self._client_id, self._client_secret),
)
except RequestException as e:
raise BlizzardApiRequestException(str(e))
response = self._session.post(
url,
params=query_params,
auth=(self._client_id, self._client_secret),
)

return self._response_handler(response)

def _response_handler(self, response):
"""Handle the response."""
return response.json()

def _request_handler(self, url, region, query_params):
"""Handle the request."""
if self._access_token is None:
json = self._get_client_token(region)
self._access_token = json["access_token"]

if query_params.get("access_token") is None:
query_params["access_token"] = self._access_token

try:
response = self._session.get(url, params=query_params)
except RequestException as e:
raise BlizzardApiRequestException(str(e))
response = self._session.get(url, params=query_params)

return self._response_handler(response)

def _format_api_url(self, resource, region):
"""Format the API url into a usable url."""
if region == "cn":
url = self._api_url_cn.format(resource)
else:
Expand All @@ -60,10 +75,12 @@ def _format_api_url(self, resource, region):
return url

def get_resource(self, resource, region, query_params={}):
"""Direction handler for when fetching resources."""
url = self._format_api_url(resource, region)
return self._request_handler(url, region, query_params)

def _format_oauth_url(self, resource, region):
"""Format the oauth url into a usable url."""
if region == "cn":
url = self._oauth_url_cn.format(resource)
else:
Expand All @@ -72,5 +89,6 @@ def _format_oauth_url(self, resource, region):
return url

def get_oauth_resource(self, resource, region, query_params={}):
"""Direction handler for when fetching oauth resources."""
url = self._format_oauth_url(resource, region)
return self._request_handler(url, region, query_params)
1 change: 1 addition & 0 deletions blizzardapi/battlenet/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""__init__.py file."""
9 changes: 9 additions & 0 deletions blizzardapi/battlenet/battlenet_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
"""battlenet_api.py file."""
from .battlenet_oauth_api import BattlenetOauthApi


class BattlenetApi:
"""Battle.net API class.
Attributes:
client_id: A string client id supplied by Blizzard.
client_secret: A string client secret supplied by Blizzard.
"""

def __init__(self, client_id, client_secret):
"""Init BattlenetApi."""
self.oauth = BattlenetOauthApi(client_id, client_secret)
14 changes: 9 additions & 5 deletions blizzardapi/battlenet/battlenet_oauth_api.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
"""battlenet_oauth_api.py file."""
from ..api import Api


class BattlenetOauthApi(Api):
"""All OAuth API methods"""
"""All Battle.net OAuth API methods.
Attributes:
client_id: A string client id supplied by Blizzard.
client_secret: A string client secret supplied by Blizzard.
"""

def __init__(self, client_id, client_secret):
"""Init BattlenetOauthApi."""
super().__init__(client_id, client_secret)

# User Authentication

def get_user_info(self, region, access_token):
"""
User Authentication - Returns basic information about the user
associated with the current bearer token.
"""
"""Return basic information about the user associated with the current bearer token."""
resource = "/oauth/userinfo"
query_params = {
"access_token": access_token,
Expand Down
11 changes: 11 additions & 0 deletions blizzardapi/blizzard_api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
"""blizzard_api.py file."""
from .battlenet.battlenet_api import BattlenetApi
from .diablo3.diablo3_api import Diablo3Api
from .hearthstone.hearthstone_api import HearthstoneApi
from .wow.wow_api import WowApi


class BlizzardApi:
"""Blizzard API class.
Attributes:
client_id: A string client id supplied by Blizzard.
client_secret: A string client secret supplied by Blizzard.
"""

def __init__(self, client_id, client_secret):
"""Init BlizzardApi."""
self.battlenet = BattlenetApi(client_id, client_secret)
self.diablo3 = Diablo3Api(client_id, client_secret)
self.hearthstone = HearthstoneApi(client_id, client_secret)
self.wow = WowApi(client_id, client_secret)
1 change: 1 addition & 0 deletions blizzardapi/diablo3/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""__init__.py file."""
9 changes: 9 additions & 0 deletions blizzardapi/diablo3/diablo3_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
"""diablo3_api.py file."""
from .diablo3_community_api import Diablo3CommunityApi
from .diablo3_game_data_api import Diablo3GameDataApi


class Diablo3Api:
"""Diablo3 API class.
Attributes:
client_id: A string client id supplied by Blizzard.
client_secret: A string client secret supplied by Blizzard.
"""

def __init__(self, client_id, client_secret):
"""Init Diablo3Api."""
self.community = Diablo3CommunityApi(client_id, client_secret)
self.game_data = Diablo3GameDataApi(client_id, client_secret)
Loading

0 comments on commit 7113953

Please sign in to comment.