blizzardapi2 2.1.15__tar.gz → 2.1.16__tar.gz

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.
Files changed (56) hide show
  1. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/PKG-INFO +1 -1
  2. blizzardapi2-2.1.16/blizzardapi2/api.py +188 -0
  3. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2.egg-info/PKG-INFO +1 -1
  4. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2.egg-info/SOURCES.txt +0 -1
  5. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/package.json +1 -1
  6. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/pyproject.toml +2 -2
  7. blizzardapi2-2.1.15/blizzardapi2/_version.py +0 -21
  8. blizzardapi2-2.1.15/blizzardapi2/api.py +0 -145
  9. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
  10. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
  11. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/.github/dependabot.yml +0 -0
  12. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/.github/workflows/README.md +0 -0
  13. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/.github/workflows/black-check.yml +0 -0
  14. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/.github/workflows/black.yml +0 -0
  15. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/.github/workflows/python-package.yml +0 -0
  16. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/.gitignore +0 -0
  17. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/.pre-commit-config.yaml +0 -0
  18. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/.release-it.json +0 -0
  19. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/CODE_OF_CONDUCT.md +0 -0
  20. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/CONTRIBUTING.md +0 -0
  21. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/LICENSE +0 -0
  22. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/README.md +0 -0
  23. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/SECURITY.md +0 -0
  24. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/__init__.py +0 -0
  25. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/battlenet/__init__.py +0 -0
  26. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/battlenet/battlenet_api.py +0 -0
  27. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/battlenet/battlenet_oauth_api.py +0 -0
  28. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/blizzard_api.py +0 -0
  29. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/diablo3/__init__.py +0 -0
  30. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/diablo3/diablo3_api.py +0 -0
  31. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/diablo3/diablo3_community_api.py +0 -0
  32. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/diablo3/diablo3_game_data_api.py +0 -0
  33. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/hearthstone/__init__.py +0 -0
  34. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/hearthstone/hearthstone_api.py +0 -0
  35. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/hearthstone/hearthstone_game_data_api.py +0 -0
  36. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/starcraft2/__init__.py +0 -0
  37. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/starcraft2/starcraft2_api.py +0 -0
  38. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/starcraft2/starcraft2_community_api.py +0 -0
  39. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/starcraft2/starcraft2_game_data_api.py +0 -0
  40. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/types.py +0 -0
  41. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/wow/__init__.py +0 -0
  42. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/wow/wow_api.py +0 -0
  43. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/wow/wow_game_data_api.py +0 -0
  44. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2/wow/wow_profile_api.py +0 -0
  45. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2.egg-info/dependency_links.txt +0 -0
  46. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2.egg-info/requires.txt +0 -0
  47. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/blizzardapi2.egg-info/top_level.txt +0 -0
  48. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/docs/battlenet/README.md +0 -0
  49. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/docs/diablo3/README.md +0 -0
  50. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/docs/hearthstone/README.md +0 -0
  51. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/docs/pull_request_template.md +0 -0
  52. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/docs/starcraft2/README.md +0 -0
  53. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/docs/wow/README.md +0 -0
  54. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/poetry.lock +0 -0
  55. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/setup.cfg +0 -0
  56. {blizzardapi2-2.1.15 → blizzardapi2-2.1.16}/uv.lock +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: blizzardapi2
3
- Version: 2.1.15
3
+ Version: 2.1.16
4
4
  Summary: A Python wrapper for Blizzard API
5
5
  Author-email: lostcol0ny <c098os0k@4wrd.cc>
6
6
  License-Expression: MIT
@@ -0,0 +1,188 @@
1
+ """api.py file."""
2
+
3
+ from datetime import UTC, datetime, timedelta
4
+ from typing import Any, Optional
5
+
6
+ import requests
7
+
8
+
9
+ class BaseApi:
10
+ """Base API class for Blizzard API clients.
11
+
12
+ Provides common functionality for authentication, token management,
13
+ and HTTP requests to Blizzard APIs.
14
+
15
+ Attributes:
16
+ _client_id: Blizzard API client ID.
17
+ _client_secret: Blizzard API client secret.
18
+ _access_token: Current access token for API requests.
19
+ _access_token_expiration_datetime: Token expiration timestamp.
20
+ _session: HTTP session for making requests.
21
+ """
22
+
23
+ def __init__(self, client_id: str, client_secret: str) -> None:
24
+ """Initialize the API client.
25
+
26
+ Args:
27
+ client_id: Blizzard API client ID.
28
+ client_secret: Blizzard API client secret.
29
+ """
30
+ self._client_id = client_id
31
+ self._client_secret = client_secret
32
+ self._access_token: Optional[str] = None
33
+ self._access_token_expiration_datetime: Optional[str] = None
34
+ self._session = requests.Session()
35
+
36
+ def _is_token_expired(self) -> bool:
37
+ """Check if the token is expiring within next 5 minutes."""
38
+ # If we don't have an expiration datetime, consider the token expired
39
+ if self._access_token_expiration_datetime is None:
40
+ return True
41
+
42
+ current_time = (
43
+ datetime.now(UTC).isoformat(timespec="milliseconds").replace("+00:00", "Z")
44
+ )
45
+ datetime_format = "%Y-%m-%dT%H:%M:%S.%fZ"
46
+ blizz_expire_obj = datetime.strptime(
47
+ self._access_token_expiration_datetime, datetime_format
48
+ )
49
+ current_datetime_obj = datetime.strptime(current_time, datetime_format)
50
+
51
+ time_difference = abs(blizz_expire_obj - current_datetime_obj)
52
+
53
+ return time_difference <= timedelta(minutes=5)
54
+
55
+ def _get_client_token(self, region: str) -> dict[str, Any]:
56
+ """Fetch an access token based on client id and client secret credentials.
57
+
58
+ Args:
59
+ region:
60
+ A string containing a region.
61
+ """
62
+ url = self._format_oauth_url("/oauth/token", region)
63
+ query_params = {"grant_type": "client_credentials"}
64
+
65
+ response = self._session.post(
66
+ url,
67
+ params=query_params,
68
+ auth=(self._client_id, self._client_secret),
69
+ )
70
+
71
+ json_response = self._response_handler(response)
72
+ self._access_token = json_response["access_token"]
73
+
74
+ return json_response
75
+
76
+ def _ensure_valid_token(self, region: str) -> None:
77
+ """Ensure that the token is valid."""
78
+ if self._access_token is None or self._is_token_expired():
79
+ self._get_client_token(region)
80
+
81
+ def _response_handler(self, response: requests.Response) -> dict[str, Any]:
82
+ """Handle the response."""
83
+ response.raise_for_status()
84
+ return response.json()
85
+
86
+ def _make_request(
87
+ self, url: str, region: str, query_params: Optional[dict[str, Any]] = None
88
+ ) -> dict[str, Any]:
89
+ """Make an authenticated request to the API.
90
+
91
+ Args:
92
+ url: The complete URL to request.
93
+ region: The region for token management.
94
+ query_params: Optional query parameters.
95
+
96
+ Returns:
97
+ The API response as a dictionary.
98
+ """
99
+ self._ensure_valid_token(region)
100
+
101
+ # Prepare query parameters
102
+ if query_params is None:
103
+ query_params = {}
104
+ else:
105
+ query_params = query_params.copy()
106
+
107
+ # Remove access_token from query_params if it exists (for OAuth endpoints)
108
+ query_params.pop("access_token", None)
109
+
110
+ headers = {"Authorization": f"Bearer {self._access_token}"}
111
+
112
+ # Make the request
113
+ response = self._session.get(url, params=query_params, headers=headers)
114
+
115
+ # Update token expiration if provided in response headers
116
+ self._update_token_expiration(response)
117
+
118
+ # Handle token refresh if needed
119
+ if response.status_code == 401 or self._is_token_expired():
120
+ self._get_client_token(region)
121
+ headers["Authorization"] = f"Bearer {self._access_token}"
122
+ response = self._session.get(url, params=query_params, headers=headers)
123
+
124
+ return self._response_handler(response)
125
+
126
+ def _update_token_expiration(self, response: requests.Response) -> None:
127
+ """Update token expiration from response headers.
128
+
129
+ Args:
130
+ response: The HTTP response object.
131
+ """
132
+ blizzard_token_expires = response.headers.get("blizzard-token-expires")
133
+ if blizzard_token_expires is not None:
134
+ self._access_token_expiration_datetime = blizzard_token_expires
135
+
136
+ def _build_url(self, resource: str, region: str, is_oauth: bool = False) -> str:
137
+ """Build the appropriate URL for the given resource and region.
138
+
139
+ Args:
140
+ resource: The API resource path.
141
+ region: The region (us, eu, kr, tw, cn).
142
+ is_oauth: Whether this is an OAuth endpoint.
143
+
144
+ Returns:
145
+ The complete URL for the request.
146
+ """
147
+ if region == "cn":
148
+ if is_oauth:
149
+ return f"https://www.gateway.battlenet.com.cn{resource}"
150
+ else:
151
+ return f"https://gateway.battlenet.com.cn{resource}"
152
+ else:
153
+ if is_oauth:
154
+ return f"https://oauth.battle.net{resource}"
155
+ else:
156
+ return f"https://{region}.api.blizzard.com{resource}"
157
+
158
+ def get_resource(
159
+ self, resource: str, region: str, query_params: Optional[dict[str, Any]] = None
160
+ ) -> dict[str, Any]:
161
+ """Make a request to a regular API resource.
162
+
163
+ Args:
164
+ resource: The API resource path.
165
+ region: The region to query.
166
+ query_params: Optional query parameters.
167
+
168
+ Returns:
169
+ The API response as a dictionary.
170
+ """
171
+ url = self._build_url(resource, region, is_oauth=False)
172
+ return self._make_request(url, region, query_params)
173
+
174
+ def get_oauth_resource(
175
+ self, resource: str, region: str, query_params: Optional[dict[str, Any]] = None
176
+ ) -> dict[str, Any]:
177
+ """Make a request to an OAuth API resource.
178
+
179
+ Args:
180
+ resource: The API resource path.
181
+ region: The region to query.
182
+ query_params: Optional query parameters.
183
+
184
+ Returns:
185
+ The API response as a dictionary.
186
+ """
187
+ url = self._build_url(resource, region, is_oauth=True)
188
+ return self._make_request(url, region, query_params)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: blizzardapi2
3
- Version: 2.1.15
3
+ Version: 2.1.16
4
4
  Summary: A Python wrapper for Blizzard API
5
5
  Author-email: lostcol0ny <c098os0k@4wrd.cc>
6
6
  License-Expression: MIT
@@ -18,7 +18,6 @@ uv.lock
18
18
  .github/workflows/black.yml
19
19
  .github/workflows/python-package.yml
20
20
  blizzardapi2/__init__.py
21
- blizzardapi2/_version.py
22
21
  blizzardapi2/api.py
23
22
  blizzardapi2/blizzard_api.py
24
23
  blizzardapi2/types.py
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blizzardapi2",
3
- "version": "2.1.13",
3
+ "version": "2.1.14",
4
4
  "scripts": {
5
5
  "release": "release-it"
6
6
  },
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "blizzardapi2"
3
- version = "2.1.15"
3
+ version = "2.1.16"
4
4
  description = "A Python wrapper for Blizzard API"
5
5
  authors = [{ name = "lostcol0ny", email = "c098os0k@4wrd.cc" }]
6
6
  license = "MIT"
@@ -79,7 +79,7 @@ exclude = [
79
79
  line-length = 120
80
80
 
81
81
  # Assume Python 3.11
82
- target-version = "2.1.13"
82
+ target-version = "2.1.14"
83
83
 
84
84
  # Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
85
85
  select = ["E4", "E7", "E9", "F"]
@@ -1,21 +0,0 @@
1
- # file generated by setuptools-scm
2
- # don't change, don't track in version control
3
-
4
- __all__ = ["__version__", "__version_tuple__", "version", "version_tuple"]
5
-
6
- TYPE_CHECKING = False
7
- if TYPE_CHECKING:
8
- from typing import Tuple
9
- from typing import Union
10
-
11
- VERSION_TUPLE = Tuple[Union[int, str], ...]
12
- else:
13
- VERSION_TUPLE = object
14
-
15
- version: str
16
- __version__: str
17
- __version_tuple__: VERSION_TUPLE
18
- version_tuple: VERSION_TUPLE
19
-
20
- __version__ = version = '2.1.14.dev3+g80ec3da'
21
- __version_tuple__ = version_tuple = (2, 1, 14, 'dev3', 'g80ec3da')
@@ -1,145 +0,0 @@
1
- """api.py file."""
2
-
3
- from datetime import UTC, datetime, timedelta
4
- from typing import Any
5
-
6
- import requests
7
-
8
-
9
- class BaseApi:
10
- """Base API class.
11
-
12
- Attributes:
13
- _client_id: A string client id supplied by Blizzard.
14
- _client_secret: A string client secret supplied by Blizzard.
15
- _access_token: A string access token that is used to access Blizzard's API.
16
- _api_url: A string url used to call the API endpoints.
17
- _api_url_cn: A string url used to call the china API endpoints.
18
- _oauth_url: A string url used to call the OAuth API endpoints.
19
- _oauth_url_cn: A string url used to call the china OAuth API endpoints.
20
- _session: An open requests.Session instance.
21
- """
22
-
23
- def __init__(self, client_id: str, client_secret: str) -> None:
24
- """Init Api."""
25
- self._client_id: str = client_id
26
- self._client_secret: str = client_secret
27
- self._access_token: str = None
28
- self._access_token_expiration_datetime: str = "2024-08-06T12:38:15.125Z"
29
-
30
- self._api_url = "https://{0}.api.blizzard.com{1}"
31
- self._api_url_cn = "https://gateway.battlenet.com.cn{0}"
32
-
33
- self._oauth_url = "https://oauth.battle.net{1}"
34
- self._oauth_url_cn = "https://www.gateway.battlenet.com.cn{0}"
35
-
36
- self._session = requests.Session()
37
-
38
- def _is_token_expired(self) -> bool:
39
- """Check if the token is expiring within next 5 minutes."""
40
- current_time = (
41
- datetime.now(UTC).isoformat(timespec="milliseconds").replace("+00:00", "Z")
42
- )
43
- datetime_format = "%Y-%m-%dT%H:%M:%S.%fZ"
44
- blizz_expire_obj = datetime.strptime(
45
- self._access_token_expiration_datetime, datetime_format
46
- )
47
- current_datetime_obj = datetime.strptime(current_time, datetime_format)
48
-
49
- time_difference = abs(blizz_expire_obj - current_datetime_obj)
50
-
51
- return time_difference <= timedelta(minutes=5)
52
-
53
- def _get_client_token(self, region: str) -> dict[str, Any]:
54
- """Fetch an access token based on client id and client secret credentials.
55
-
56
- Args:
57
- region:
58
- A string containing a region.
59
- """
60
- url = self._format_oauth_url("/oauth/token", region)
61
- query_params = {"grant_type": "client_credentials"}
62
-
63
- response = self._session.post(
64
- url,
65
- params=query_params,
66
- auth=(self._client_id, self._client_secret),
67
- )
68
-
69
- json_response = self._response_handler(response)
70
- self._access_token = json_response["access_token"]
71
-
72
- return json_response
73
-
74
- def _ensure_valid_token(self, region: str) -> None:
75
- """Ensure that the token is valid."""
76
- if self._access_token is None or self._is_token_expired():
77
- self._get_client_token(region)
78
-
79
- def _response_handler(self, response: requests.Response) -> dict[str, Any]:
80
- """Handle the response."""
81
- response.raise_for_status()
82
- return response.json()
83
-
84
- def _request_handler(
85
- self, url: str, region: str, query_params: dict[str, Any] = None
86
- ) -> dict[str, Any]:
87
- """Handle the request."""
88
- self._ensure_valid_token(region)
89
-
90
- headers = {"Authorization": f"Bearer {self._access_token}"}
91
-
92
- # Initialize query_params if it's None
93
- if query_params is None:
94
- query_params = {}
95
- else:
96
- # Create a copy of query_params to avoid modifying the original
97
- query_params = query_params.copy()
98
-
99
- # Remove access_token from query_params if it exists
100
- query_params.pop("access_token", None)
101
-
102
- response = self._session.get(url, params=query_params, headers=headers)
103
-
104
- self._access_token_expiration_datetime = response.headers.get(
105
- "blizzard-token-expires"
106
- )
107
-
108
- if response.status_code == 401 or self._is_token_expired():
109
- self._get_client_token(region)
110
- headers["Authorization"] = f"Bearer {self._access_token}"
111
- response = self._session.get(url, params=query_params, headers=headers)
112
-
113
- return self._response_handler(response)
114
-
115
- def _format_api_url(self, resource: str, region: str) -> str:
116
- """Format the API url into a usable url."""
117
- if region == "cn":
118
- url = self._api_url_cn.format(resource)
119
- else:
120
- url = self._api_url.format(region, resource)
121
-
122
- return url
123
-
124
- def get_resource(
125
- self, resource: str, region: str, query_params={}
126
- ) -> dict[str, Any]:
127
- """Direction handler for when fetching resources."""
128
- url = self._format_api_url(resource, region)
129
- return self._request_handler(url, region, query_params)
130
-
131
- def _format_oauth_url(self, resource: str, region: str) -> str:
132
- """Format the oauth url into a usable url."""
133
- if region == "cn":
134
- url = self._oauth_url_cn.format(resource)
135
- else:
136
- url = self._oauth_url.format(region, resource)
137
-
138
- return url
139
-
140
- def get_oauth_resource(
141
- self, resource: str, region: str, query_params={}
142
- ) -> dict[str, Any]:
143
- """Direction handler for when fetching oauth resources."""
144
- url = self._format_oauth_url(resource, region)
145
- return self._request_handler(url, region, query_params)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes