blizzardapi2 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ """__init__.py file."""
2
+ from requests.exceptions import * # noqa
3
+
4
+ from .blizzard_api import BlizzardApi # noqa
blizzardapi2/api.py ADDED
@@ -0,0 +1,94 @@
1
+ """api.py file."""
2
+ import requests
3
+
4
+
5
+ class Api:
6
+ """Base API class.
7
+
8
+ Attributes:
9
+ _client_id: A string client id supplied by Blizzard.
10
+ _client_secret: A string client secret supplied by Blizzard.
11
+ _access_token: A string access token that is used to access Blizzard's API.
12
+ _api_url: A string url used to call the API endpoints.
13
+ _api_url_cn: A string url used to call the china API endpoints.
14
+ _oauth_url: A string url used to call the OAuth API endpoints.
15
+ _oauth_url_cn: A string url used to call the china OAuth API endpoints.
16
+ _session: An open requests.Session instance.
17
+ """
18
+
19
+ def __init__(self, client_id, client_secret):
20
+ """Init Api."""
21
+ self._client_id = client_id
22
+ self._client_secret = client_secret
23
+ self._access_token = None
24
+
25
+ self._api_url = "https://{0}.api.blizzard.com{1}"
26
+ self._api_url_cn = "https://gateway.battlenet.com.cn{0}"
27
+
28
+ self._oauth_url = "https://{0}.battle.net{1}"
29
+ self._oauth_url_cn = "https://www.battlenet.com.cn{0}"
30
+
31
+ self._session = requests.Session()
32
+
33
+ def _get_client_token(self, region):
34
+ """Fetch an access token based on client id and client secret credentials.
35
+
36
+ Args:
37
+ region:
38
+ A string containing a region.
39
+ """
40
+ url = self._format_oauth_url("/oauth/token", region)
41
+ query_params = {"grant_type": "client_credentials"}
42
+
43
+ response = self._session.post(
44
+ url,
45
+ params=query_params,
46
+ auth=(self._client_id, self._client_secret),
47
+ )
48
+
49
+ return self._response_handler(response)
50
+
51
+ def _response_handler(self, response):
52
+ """Handle the response."""
53
+ return response.json()
54
+
55
+ def _request_handler(self, url, region, query_params):
56
+ """Handle the request."""
57
+ if self._access_token is None:
58
+ json = self._get_client_token(region)
59
+ self._access_token = json["access_token"]
60
+
61
+ if query_params.get("access_token") is None:
62
+ query_params["access_token"] = self._access_token
63
+
64
+ response = self._session.get(url, params=query_params)
65
+
66
+ return self._response_handler(response)
67
+
68
+ def _format_api_url(self, resource, region):
69
+ """Format the API url into a usable url."""
70
+ if region == "cn":
71
+ url = self._api_url_cn.format(resource)
72
+ else:
73
+ url = self._api_url.format(region, resource)
74
+
75
+ return url
76
+
77
+ def get_resource(self, resource, region, query_params={}):
78
+ """Direction handler for when fetching resources."""
79
+ url = self._format_api_url(resource, region)
80
+ return self._request_handler(url, region, query_params)
81
+
82
+ def _format_oauth_url(self, resource, region):
83
+ """Format the oauth url into a usable url."""
84
+ if region == "cn":
85
+ url = self._oauth_url_cn.format(resource)
86
+ else:
87
+ url = self._oauth_url.format(region, resource)
88
+
89
+ return url
90
+
91
+ def get_oauth_resource(self, resource, region, query_params={}):
92
+ """Direction handler for when fetching oauth resources."""
93
+ url = self._format_oauth_url(resource, region)
94
+ return self._request_handler(url, region, query_params)
@@ -0,0 +1 @@
1
+ """__init__.py file."""
@@ -0,0 +1,15 @@
1
+ """battlenet_api.py file."""
2
+ from .battlenet_oauth_api import BattlenetOauthApi
3
+
4
+
5
+ class BattlenetApi:
6
+ """Battle.net API class.
7
+
8
+ Attributes:
9
+ client_id: A string client id supplied by Blizzard.
10
+ client_secret: A string client secret supplied by Blizzard.
11
+ """
12
+
13
+ def __init__(self, client_id, client_secret):
14
+ """Init BattlenetApi."""
15
+ self.oauth = BattlenetOauthApi(client_id, client_secret)
@@ -0,0 +1,25 @@
1
+ """battlenet_oauth_api.py file."""
2
+ from ..api import Api
3
+
4
+
5
+ class BattlenetOauthApi(Api):
6
+ """All Battle.net OAuth API methods.
7
+
8
+ Attributes:
9
+ client_id: A string client id supplied by Blizzard.
10
+ client_secret: A string client secret supplied by Blizzard.
11
+ """
12
+
13
+ def __init__(self, client_id, client_secret):
14
+ """Init BattlenetOauthApi."""
15
+ super().__init__(client_id, client_secret)
16
+
17
+ # User Authentication
18
+
19
+ def get_user_info(self, region, access_token):
20
+ """Return basic information about the user associated with the current bearer token."""
21
+ resource = "/oauth/userinfo"
22
+ query_params = {
23
+ "access_token": access_token,
24
+ }
25
+ return super().get_oauth_resource(resource, region, query_params)
@@ -0,0 +1,21 @@
1
+ """blizzard_api.py file."""
2
+ from .battlenet.battlenet_api import BattlenetApi
3
+ from .diablo3.diablo3_api import Diablo3Api
4
+ from .hearthstone.hearthstone_api import HearthstoneApi
5
+ from .wow.wow_api import WowApi
6
+
7
+
8
+ class BlizzardApi:
9
+ """Blizzard API class.
10
+
11
+ Attributes:
12
+ client_id: A string client id supplied by Blizzard.
13
+ client_secret: A string client secret supplied by Blizzard.
14
+ """
15
+
16
+ def __init__(self, client_id, client_secret):
17
+ """Init BlizzardApi."""
18
+ self.battlenet = BattlenetApi(client_id, client_secret)
19
+ self.diablo3 = Diablo3Api(client_id, client_secret)
20
+ self.hearthstone = HearthstoneApi(client_id, client_secret)
21
+ self.wow = WowApi(client_id, client_secret)
@@ -0,0 +1 @@
1
+ """__init__.py file."""
@@ -0,0 +1,17 @@
1
+ """diablo3_api.py file."""
2
+ from .diablo3_community_api import Diablo3CommunityApi
3
+ from .diablo3_game_data_api import Diablo3GameDataApi
4
+
5
+
6
+ class Diablo3Api:
7
+ """Diablo3 API class.
8
+
9
+ Attributes:
10
+ client_id: A string client id supplied by Blizzard.
11
+ client_secret: A string client secret supplied by Blizzard.
12
+ """
13
+
14
+ def __init__(self, client_id, client_secret):
15
+ """Init Diablo3Api."""
16
+ self.community = Diablo3CommunityApi(client_id, client_secret)
17
+ self.game_data = Diablo3GameDataApi(client_id, client_secret)
@@ -0,0 +1,113 @@
1
+ """diablo3_community_api.py file."""
2
+ from ..api import Api
3
+
4
+
5
+ class Diablo3CommunityApi(Api):
6
+ """All Diablo 3 Community API methods.
7
+
8
+ Attributes:
9
+ client_id: A string client id supplied by Blizzard.
10
+ client_secret: A string client secret supplied by Blizzard.
11
+ """
12
+
13
+ def __init__(self, client_id, client_secret):
14
+ """Init Diablo3CommunityApi."""
15
+ super().__init__(client_id, client_secret)
16
+
17
+ # D3 Act API
18
+
19
+ def get_act_index(self, region, locale):
20
+ """Return an index of acts."""
21
+ resource = "/d3/data/act"
22
+ query_params = {"locale": locale}
23
+ return super().get_resource(resource, region, query_params)
24
+
25
+ def get_act(self, region, locale, act_id):
26
+ """Return a single act by ID."""
27
+ resource = f"/d3/data/act/{act_id}"
28
+ query_params = {"locale": locale}
29
+ return super().get_resource(resource, region, query_params)
30
+
31
+ # D3 Artisan and Recipe API
32
+
33
+ def get_artisan(self, region, locale, artisan_slug):
34
+ """Return a single artisan by slug."""
35
+ resource = f"/d3/data/artisan/{artisan_slug}"
36
+ query_params = {"locale": locale}
37
+ return super().get_resource(resource, region, query_params)
38
+
39
+ def get_recipe(self, region, locale, artisan_slug, recipe_slug):
40
+ """Return a single recipe by slug for the specified artisan."""
41
+ resource = f"/d3/data/artisan/{artisan_slug}/recipe/{recipe_slug}"
42
+ query_params = {"locale": locale}
43
+ return super().get_resource(resource, region, query_params)
44
+
45
+ # D3 Follower API
46
+
47
+ def get_follower(self, region, locale, follower_slug):
48
+ """Return a single follower by slug."""
49
+ resource = f"/d3/data/follower/{follower_slug}"
50
+ query_params = {"locale": locale}
51
+ return super().get_resource(resource, region, query_params)
52
+
53
+ # D3 Character Class and Skill API
54
+
55
+ def get_character_class(self, region, locale, class_slug):
56
+ """Return a single character class by slug."""
57
+ resource = f"/d3/data/hero/{class_slug}"
58
+ query_params = {"locale": locale}
59
+ return super().get_resource(resource, region, query_params)
60
+
61
+ def get_api_skill(self, region, locale, class_slug, skill_slug):
62
+ """Return a single skill by slug for a specific character class."""
63
+ resource = f"/d3/data/hero/{class_slug}/skill/{skill_slug}"
64
+ query_params = {"locale": locale}
65
+ return super().get_resource(resource, region, query_params)
66
+
67
+ # D3 Item Type API
68
+
69
+ def get_item_type_index(self, region, locale):
70
+ """Return an index of item types."""
71
+ resource = "/d3/data/item-type"
72
+ query_params = {"locale": locale}
73
+ return super().get_resource(resource, region, query_params)
74
+
75
+ def get_item_type(self, region, locale, item_type_slug):
76
+ """Return a single item type by slug."""
77
+ resource = f"/d3/data/item-type/{item_type_slug}"
78
+ query_params = {"locale": locale}
79
+ return super().get_resource(resource, region, query_params)
80
+
81
+ # D3 Item API
82
+
83
+ def get_item(self, region, locale, item_slug_id):
84
+ """Return a single item by item slug and ID."""
85
+ resource = f"/d3/data/item/{item_slug_id}"
86
+ query_params = {"locale": locale}
87
+ return super().get_resource(resource, region, query_params)
88
+
89
+ # D3 Profile API
90
+
91
+ def get_api_account(self, region, locale, account_id):
92
+ """Return the specified account profile."""
93
+ resource = f"/d3/profile/{account_id}/"
94
+ query_params = {"locale": locale}
95
+ return super().get_resource(resource, region, query_params)
96
+
97
+ def get_api_hero(self, region, locale, account_id, hero_id):
98
+ """Return a single hero."""
99
+ resource = f"/d3/profile/{account_id}/hero/{hero_id}"
100
+ query_params = {"locale": locale}
101
+ return super().get_resource(resource, region, query_params)
102
+
103
+ def get_api_detailed_hero_items(self, region, locale, account_id, hero_id):
104
+ """Return a list of items for the specified hero."""
105
+ resource = f"/d3/profile/{account_id}/hero/{hero_id}/items"
106
+ query_params = {"locale": locale}
107
+ return super().get_resource(resource, region, query_params)
108
+
109
+ def get_api_detailed_follower_items(self, region, locale, account_id, hero_id):
110
+ """Return a single item by item slug and ID."""
111
+ resource = f"/d3/profile/{account_id}/hero/{hero_id}/follower-items"
112
+ query_params = {"locale": locale}
113
+ return super().get_resource(resource, region, query_params)
@@ -0,0 +1,47 @@
1
+ """diablo3_game_data_api.py file."""
2
+ from ..api import Api
3
+
4
+
5
+ class Diablo3GameDataApi(Api):
6
+ """All Diablo 3 Game Data API methods.
7
+
8
+ Attributes:
9
+ client_id: A string client id supplied by Blizzard.
10
+ client_secret: A string client secret supplied by Blizzard.
11
+ """
12
+
13
+ def __init__(self, client_id, client_secret):
14
+ """Init Diablo3GameDataApi."""
15
+ super().__init__(client_id, client_secret)
16
+
17
+ # D3
18
+
19
+ def get_season_index(self, region):
20
+ """Return an index of available seasons."""
21
+ resource = "/data/d3/season/"
22
+ return super().get_resource(resource, region)
23
+
24
+ def get_season(self, region, season_id):
25
+ """Return a leaderboard list for the specified season."""
26
+ resource = f"/data/d3/season/{season_id}"
27
+ return super().get_resource(resource, region)
28
+
29
+ def get_season_leaderboard(self, region, season_id, leaderboard_id):
30
+ """Return a the specified leaderboard for the specified season."""
31
+ resource = f"/data/d3/season/{season_id}/leaderboard/{leaderboard_id}"
32
+ return super().get_resource(resource, region)
33
+
34
+ def get_era_index(self, region):
35
+ """Return an index of available eras."""
36
+ resource = "/data/d3/era/"
37
+ return super().get_resource(resource, region)
38
+
39
+ def get_era(self, region, era_id):
40
+ """Return a leaderboard list for a particular era."""
41
+ resource = f"/data/d3/era/{era_id}"
42
+ return super().get_resource(resource, region)
43
+
44
+ def get_era_leaderboard(self, region, era_id, leaderboard_id):
45
+ """Return the specified leaderboard for the specified era."""
46
+ resource = f"/data/d3/era/{era_id}/leaderboard/{leaderboard_id}"
47
+ return super().get_resource(resource, region)
@@ -0,0 +1 @@
1
+ """__init__.py file."""
@@ -0,0 +1,15 @@
1
+ """hearthstone_api.py file."""
2
+ from .hearthstone_game_data_api import HearthstoneGameDataApi
3
+
4
+
5
+ class HearthstoneApi:
6
+ """Hearthstone API class.
7
+
8
+ Attributes:
9
+ client_id: A string client id supplied by Blizzard.
10
+ client_secret: A string client secret supplied by Blizzard.
11
+ """
12
+
13
+ def __init__(self, client_id, client_secret):
14
+ """Init HearthstoneApi."""
15
+ self.game_data = HearthstoneGameDataApi(client_id, client_secret)
@@ -0,0 +1,65 @@
1
+ """hearthstone_game_data_api.py file."""
2
+ from ..api import Api
3
+
4
+
5
+ class HearthstoneGameDataApi(Api):
6
+ """All Hearthstone Game Data API methods.
7
+
8
+ Attributes:
9
+ client_id: A string client id supplied by Blizzard.
10
+ client_secret: A string client secret supplied by Blizzard.
11
+ """
12
+
13
+ def __init__(self, client_id, client_secret):
14
+ """Init HearthstoneGameDataApi."""
15
+ super().__init__(client_id, client_secret)
16
+
17
+ # Cards
18
+
19
+ def search_cards(self, region, locale, **query_params):
20
+ """Return an up-to-date list of all cards matching the search criteria."""
21
+ resource = "/hearthstone/cards"
22
+ query_params.update({"locale": locale})
23
+ return super().get_resource(resource, region, query_params)
24
+
25
+ def get_card(self, region, locale, id_or_slug, game_mode="constructed"):
26
+ """Return the card with an ID or slug that matches the one you specify."""
27
+ resource = f"/hearthstone/cards/{id_or_slug}"
28
+ query_params = {"locale": locale, "game_mode": game_mode}
29
+ return super().get_resource(resource, region, query_params)
30
+
31
+ # Card Backs
32
+
33
+ def search_card_backs(self, region, locale, **query_params):
34
+ """Return an up-to-date list of all card backs matching the search criteria."""
35
+ resource = "/hearthstone/cardbacks"
36
+ query_params.update({"locale": locale})
37
+ return super().get_resource(resource, region, query_params)
38
+
39
+ def get_card_back(self, region, locale, id_or_slug):
40
+ """Return a specific card back by using card back ID or slug."""
41
+ resource = f"/hearthstone/cardbacks/{id_or_slug}"
42
+ query_params = {"locale": locale}
43
+ return super().get_resource(resource, region, query_params)
44
+
45
+ # Decks
46
+
47
+ def get_deck(self, region, locale, **query_params):
48
+ """Find a deck by list of cards or code, including the hero."""
49
+ resource = "/hearthstone/deck"
50
+ query_params = {"locale": locale}
51
+ return super().get_resource(resource, region, query_params)
52
+
53
+ # Metadata
54
+
55
+ def get_metadata(self, region, locale):
56
+ """Return information about the categorization of cards."""
57
+ resource = "/hearthstone/metadata"
58
+ query_params = {"locale": locale}
59
+ return super().get_resource(resource, region, query_params)
60
+
61
+ def get_metadata_type(self, region, locale, type_id):
62
+ """Return information about just one type of metadata."""
63
+ resource = f"/hearthstone/metadata/{type_id}"
64
+ query_params = {"locale": locale}
65
+ return super().get_resource(resource, region, query_params)
@@ -0,0 +1 @@
1
+ """__init__.py file."""
@@ -0,0 +1,17 @@
1
+ """wow_api.py file."""
2
+ from .wow_game_data_api import WowGameDataApi
3
+ from .wow_profile_api import WowProfileApi
4
+
5
+
6
+ class WowApi:
7
+ """Wow API class.
8
+
9
+ Attributes:
10
+ client_id: A string client id supplied by Blizzard.
11
+ client_secret: A string client secret supplied by Blizzard.
12
+ """
13
+
14
+ def __init__(self, client_id, client_secret):
15
+ """Init WowApi."""
16
+ self.game_data = WowGameDataApi(client_id, client_secret)
17
+ self.profile = WowProfileApi(client_id, client_secret)