blizzardapi2 2.1.15__tar.gz → 2.1.17__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.
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/.github/workflows/python-package.yml +5 -32
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/.gitignore +3 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/.pre-commit-config.yaml +0 -6
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/PKG-INFO +37 -49
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/README.md +34 -46
- blizzardapi2-2.1.17/blizzardapi2/api.py +195 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/blizzard_api.py +0 -33
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/hearthstone/hearthstone_api.py +0 -81
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/wow/wow_game_data_api.py +206 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2.egg-info/PKG-INFO +37 -49
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2.egg-info/SOURCES.txt +0 -2
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2.egg-info/requires.txt +2 -2
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/package.json +1 -1
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/pyproject.toml +4 -4
- blizzardapi2-2.1.15/.github/workflows/black.yml +0 -43
- blizzardapi2-2.1.15/blizzardapi2/_version.py +0 -21
- blizzardapi2-2.1.15/blizzardapi2/api.py +0 -145
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/.github/dependabot.yml +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/.github/workflows/README.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/.github/workflows/black-check.yml +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/.release-it.json +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/CODE_OF_CONDUCT.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/CONTRIBUTING.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/LICENSE +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/SECURITY.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/__init__.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/battlenet/__init__.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/battlenet/battlenet_api.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/battlenet/battlenet_oauth_api.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/diablo3/__init__.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/diablo3/diablo3_api.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/diablo3/diablo3_community_api.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/diablo3/diablo3_game_data_api.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/hearthstone/__init__.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/hearthstone/hearthstone_game_data_api.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/starcraft2/__init__.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/starcraft2/starcraft2_api.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/starcraft2/starcraft2_community_api.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/starcraft2/starcraft2_game_data_api.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/types.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/wow/__init__.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/wow/wow_api.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2/wow/wow_profile_api.py +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2.egg-info/dependency_links.txt +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/blizzardapi2.egg-info/top_level.txt +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/docs/battlenet/README.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/docs/diablo3/README.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/docs/hearthstone/README.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/docs/pull_request_template.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/docs/starcraft2/README.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/docs/wow/README.md +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/poetry.lock +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/setup.cfg +0 -0
- {blizzardapi2-2.1.15 → blizzardapi2-2.1.17}/uv.lock +0 -0
|
@@ -14,38 +14,7 @@ permissions:
|
|
|
14
14
|
id-token: write
|
|
15
15
|
|
|
16
16
|
jobs:
|
|
17
|
-
checkout:
|
|
18
|
-
runs-on: ubuntu-latest
|
|
19
|
-
strategy:
|
|
20
|
-
fail-fast: false
|
|
21
|
-
matrix:
|
|
22
|
-
python-version: ["3.11", "3.12", "3.13"]
|
|
23
|
-
|
|
24
|
-
steps:
|
|
25
|
-
- uses: actions/checkout@v4
|
|
26
|
-
with:
|
|
27
|
-
fetch-depth: 0
|
|
28
|
-
|
|
29
|
-
- name: Set up Python ${{ matrix.python-version }}
|
|
30
|
-
uses: actions/setup-python@v5
|
|
31
|
-
with:
|
|
32
|
-
python-version: ${{ matrix.python-version }}
|
|
33
|
-
|
|
34
|
-
- name: Install uv
|
|
35
|
-
run: |
|
|
36
|
-
curl -LsSf https://astral.sh/uv/install.sh | sh
|
|
37
|
-
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
|
|
38
|
-
|
|
39
|
-
- name: Create virtual environment
|
|
40
|
-
run: |
|
|
41
|
-
uv venv
|
|
42
|
-
|
|
43
|
-
- name: Install dependencies
|
|
44
|
-
run: |
|
|
45
|
-
uv pip install -e ".[dev]"
|
|
46
|
-
|
|
47
17
|
publish:
|
|
48
|
-
needs: checkout
|
|
49
18
|
if: github.ref == 'refs/heads/main'
|
|
50
19
|
runs-on: ubuntu-latest
|
|
51
20
|
steps:
|
|
@@ -80,7 +49,11 @@ jobs:
|
|
|
80
49
|
python -m pip install --upgrade pip
|
|
81
50
|
pip install build twine setuptools_scm
|
|
82
51
|
python -m build
|
|
83
|
-
twine upload dist/* --username __token__ --password ${{ secrets.PYPI_API_TOKEN }}
|
|
52
|
+
twine upload dist/* --skip-existing --username __token__ --password ${{ secrets.PYPI_API_TOKEN }} || {
|
|
53
|
+
echo "Upload failed - version may already exist or filename was previously used"
|
|
54
|
+
echo "This is expected behavior and won't block the release"
|
|
55
|
+
exit 0
|
|
56
|
+
}
|
|
84
57
|
env:
|
|
85
58
|
PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
|
|
86
59
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: blizzardapi2
|
|
3
|
-
Version: 2.1.
|
|
3
|
+
Version: 2.1.17
|
|
4
4
|
Summary: A Python wrapper for Blizzard API
|
|
5
5
|
Author-email: lostcol0ny <c098os0k@4wrd.cc>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -14,13 +14,13 @@ Description-Content-Type: text/markdown
|
|
|
14
14
|
License-File: LICENSE
|
|
15
15
|
Requires-Dist: requests<3.0.0,>=2.32.3
|
|
16
16
|
Requires-Dist: black<26.0.0,>=24.4.2
|
|
17
|
-
Requires-Dist: pytest<
|
|
17
|
+
Requires-Dist: pytest<10.0.0,>=8.2.2
|
|
18
18
|
Requires-Dist: pytest-mock<4.0.0,>=3.14.0
|
|
19
19
|
Requires-Dist: ruff<1.0.0,>=0.3.0
|
|
20
20
|
Requires-Dist: pydantic>=2.11.6
|
|
21
21
|
Requires-Dist: aiohttp>=3.12.12
|
|
22
22
|
Provides-Extra: dev
|
|
23
|
-
Requires-Dist: pytest<
|
|
23
|
+
Requires-Dist: pytest<10.0.0,>=8.2.2; extra == "dev"
|
|
24
24
|
Requires-Dist: pytest-mock<4.0.0,>=3.14.0; extra == "dev"
|
|
25
25
|
Requires-Dist: pytest-asyncio<2.0.0,>=0.23.5; extra == "dev"
|
|
26
26
|
Requires-Dist: black<26.0.0,>=24.4.2; extra == "dev"
|
|
@@ -53,10 +53,10 @@ Current supported features include:
|
|
|
53
53
|
Modern features:
|
|
54
54
|
|
|
55
55
|
- Full type hints support
|
|
56
|
-
-
|
|
56
|
+
- Automatic token management with intelligent refresh
|
|
57
57
|
- Enum-based region and locale validation
|
|
58
|
-
-
|
|
59
|
-
-
|
|
58
|
+
- Clean, maintainable codebase
|
|
59
|
+
- Comprehensive error handling
|
|
60
60
|
|
|
61
61
|
To gain access to Blizzard's API please register [here](https://develop.battle.net/access/) to obtain a client id and client secret.
|
|
62
62
|
|
|
@@ -79,72 +79,58 @@ Python (3.11+)
|
|
|
79
79
|
|
|
80
80
|
```python
|
|
81
81
|
from blizzardapi2 import BlizzardApi
|
|
82
|
-
from blizzardapi2.types import Locale, Region
|
|
83
82
|
|
|
84
83
|
api_client = BlizzardApi("client_id", "client_secret")
|
|
85
84
|
|
|
86
|
-
#
|
|
85
|
+
# Public API endpoint (uses automatic client credentials token)
|
|
87
86
|
categories_index = api_client.wow.game_data.get_achievement_categories_index(
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
"us", # region
|
|
88
|
+
"en_US" # locale
|
|
90
89
|
)
|
|
91
90
|
|
|
92
|
-
# Protected API endpoint
|
|
91
|
+
# Protected API endpoint (requires user OAuth token)
|
|
93
92
|
summary = api_client.wow.profile.get_account_profile_summary(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
"
|
|
93
|
+
"us",
|
|
94
|
+
"en_US",
|
|
95
|
+
"user_access_token" # OAuth token from authorization code flow
|
|
97
96
|
)
|
|
98
97
|
|
|
99
|
-
#
|
|
98
|
+
# WoW Classic endpoint
|
|
100
99
|
connected_realms_index = api_client.wow.game_data.get_connected_realms_index(
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
"us",
|
|
101
|
+
"en_US",
|
|
103
102
|
is_classic=True
|
|
104
103
|
)
|
|
105
104
|
```
|
|
106
105
|
|
|
107
|
-
**
|
|
106
|
+
**Token Management**
|
|
107
|
+
|
|
108
|
+
The library automatically manages client credentials tokens for public endpoints:
|
|
108
109
|
|
|
109
110
|
```python
|
|
110
|
-
import asyncio
|
|
111
111
|
from blizzardapi2 import BlizzardApi
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
# Multiple concurrent requests
|
|
125
|
-
tasks = [
|
|
126
|
-
api_client.wow.profile.get_character_profile_summary(
|
|
127
|
-
Region.US,
|
|
128
|
-
Locale.EN_US,
|
|
129
|
-
"realm-slug",
|
|
130
|
-
"character-name"
|
|
131
|
-
),
|
|
132
|
-
api_client.wow.profile.get_character_achievements_summary(
|
|
133
|
-
Region.US,
|
|
134
|
-
Locale.EN_US,
|
|
135
|
-
"realm-slug",
|
|
136
|
-
"character-name"
|
|
137
|
-
)
|
|
138
|
-
]
|
|
139
|
-
results = await asyncio.gather(*tasks)
|
|
140
|
-
|
|
141
|
-
asyncio.run(main())
|
|
112
|
+
|
|
113
|
+
api_client = BlizzardApi("client_id", "client_secret")
|
|
114
|
+
|
|
115
|
+
# First call: library automatically fetches and caches a client credentials token
|
|
116
|
+
realms = api_client.wow.game_data.get_realms_index("us", "en_US")
|
|
117
|
+
|
|
118
|
+
# Subsequent calls: library reuses the cached token
|
|
119
|
+
achievements = api_client.wow.game_data.get_achievements_index("us", "en_US")
|
|
120
|
+
|
|
121
|
+
# When token expires: library automatically refreshes it
|
|
122
|
+
# You don't need to manage tokens manually!
|
|
142
123
|
```
|
|
143
124
|
|
|
144
125
|
# Access token vs Client ID/Client Secret
|
|
145
126
|
|
|
146
127
|
You can pass in a `client_id` and `client_secret` and use almost any endpoint except for a few that require an `access_token` obtained via OAuth authorization code flow. You can find more information at https://develop.battle.net/documentation/guides/using-oauth/authorization-code-flow.
|
|
147
128
|
|
|
129
|
+
**Important:** The library handles all tokens securely by passing them in Authorization headers, never in URLs. When you provide a user OAuth token to methods like `get_account_profile_summary()`, the library automatically:
|
|
130
|
+
1. Extracts the token from parameters
|
|
131
|
+
2. Sends it via `Authorization: Bearer <token>` header
|
|
132
|
+
3. Keeps tokens out of URL query strings
|
|
133
|
+
|
|
148
134
|
Here is the list of endpoints, specified by Blizzard, that require an OAuth token:
|
|
149
135
|
|
|
150
136
|
```
|
|
@@ -156,6 +142,8 @@ GET /profile/user/wow/collections/pets
|
|
|
156
142
|
GET /profile/user/wow/collections/mounts
|
|
157
143
|
```
|
|
158
144
|
|
|
145
|
+
**Note:** For these protected endpoints, you must implement the OAuth authorization code flow in your application to obtain a user access token. The library does not handle the OAuth flow itself - it only accepts and uses the token you provide.
|
|
146
|
+
|
|
159
147
|
# Documentation
|
|
160
148
|
|
|
161
149
|
For detailed documentation on each game's API, see the following README files:
|
|
@@ -24,10 +24,10 @@ Current supported features include:
|
|
|
24
24
|
Modern features:
|
|
25
25
|
|
|
26
26
|
- Full type hints support
|
|
27
|
-
-
|
|
27
|
+
- Automatic token management with intelligent refresh
|
|
28
28
|
- Enum-based region and locale validation
|
|
29
|
-
-
|
|
30
|
-
-
|
|
29
|
+
- Clean, maintainable codebase
|
|
30
|
+
- Comprehensive error handling
|
|
31
31
|
|
|
32
32
|
To gain access to Blizzard's API please register [here](https://develop.battle.net/access/) to obtain a client id and client secret.
|
|
33
33
|
|
|
@@ -50,72 +50,58 @@ Python (3.11+)
|
|
|
50
50
|
|
|
51
51
|
```python
|
|
52
52
|
from blizzardapi2 import BlizzardApi
|
|
53
|
-
from blizzardapi2.types import Locale, Region
|
|
54
53
|
|
|
55
54
|
api_client = BlizzardApi("client_id", "client_secret")
|
|
56
55
|
|
|
57
|
-
#
|
|
56
|
+
# Public API endpoint (uses automatic client credentials token)
|
|
58
57
|
categories_index = api_client.wow.game_data.get_achievement_categories_index(
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
"us", # region
|
|
59
|
+
"en_US" # locale
|
|
61
60
|
)
|
|
62
61
|
|
|
63
|
-
# Protected API endpoint
|
|
62
|
+
# Protected API endpoint (requires user OAuth token)
|
|
64
63
|
summary = api_client.wow.profile.get_account_profile_summary(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
"
|
|
64
|
+
"us",
|
|
65
|
+
"en_US",
|
|
66
|
+
"user_access_token" # OAuth token from authorization code flow
|
|
68
67
|
)
|
|
69
68
|
|
|
70
|
-
#
|
|
69
|
+
# WoW Classic endpoint
|
|
71
70
|
connected_realms_index = api_client.wow.game_data.get_connected_realms_index(
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
"us",
|
|
72
|
+
"en_US",
|
|
74
73
|
is_classic=True
|
|
75
74
|
)
|
|
76
75
|
```
|
|
77
76
|
|
|
78
|
-
**
|
|
77
|
+
**Token Management**
|
|
78
|
+
|
|
79
|
+
The library automatically manages client credentials tokens for public endpoints:
|
|
79
80
|
|
|
80
81
|
```python
|
|
81
|
-
import asyncio
|
|
82
82
|
from blizzardapi2 import BlizzardApi
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
# Multiple concurrent requests
|
|
96
|
-
tasks = [
|
|
97
|
-
api_client.wow.profile.get_character_profile_summary(
|
|
98
|
-
Region.US,
|
|
99
|
-
Locale.EN_US,
|
|
100
|
-
"realm-slug",
|
|
101
|
-
"character-name"
|
|
102
|
-
),
|
|
103
|
-
api_client.wow.profile.get_character_achievements_summary(
|
|
104
|
-
Region.US,
|
|
105
|
-
Locale.EN_US,
|
|
106
|
-
"realm-slug",
|
|
107
|
-
"character-name"
|
|
108
|
-
)
|
|
109
|
-
]
|
|
110
|
-
results = await asyncio.gather(*tasks)
|
|
111
|
-
|
|
112
|
-
asyncio.run(main())
|
|
83
|
+
|
|
84
|
+
api_client = BlizzardApi("client_id", "client_secret")
|
|
85
|
+
|
|
86
|
+
# First call: library automatically fetches and caches a client credentials token
|
|
87
|
+
realms = api_client.wow.game_data.get_realms_index("us", "en_US")
|
|
88
|
+
|
|
89
|
+
# Subsequent calls: library reuses the cached token
|
|
90
|
+
achievements = api_client.wow.game_data.get_achievements_index("us", "en_US")
|
|
91
|
+
|
|
92
|
+
# When token expires: library automatically refreshes it
|
|
93
|
+
# You don't need to manage tokens manually!
|
|
113
94
|
```
|
|
114
95
|
|
|
115
96
|
# Access token vs Client ID/Client Secret
|
|
116
97
|
|
|
117
98
|
You can pass in a `client_id` and `client_secret` and use almost any endpoint except for a few that require an `access_token` obtained via OAuth authorization code flow. You can find more information at https://develop.battle.net/documentation/guides/using-oauth/authorization-code-flow.
|
|
118
99
|
|
|
100
|
+
**Important:** The library handles all tokens securely by passing them in Authorization headers, never in URLs. When you provide a user OAuth token to methods like `get_account_profile_summary()`, the library automatically:
|
|
101
|
+
1. Extracts the token from parameters
|
|
102
|
+
2. Sends it via `Authorization: Bearer <token>` header
|
|
103
|
+
3. Keeps tokens out of URL query strings
|
|
104
|
+
|
|
119
105
|
Here is the list of endpoints, specified by Blizzard, that require an OAuth token:
|
|
120
106
|
|
|
121
107
|
```
|
|
@@ -127,6 +113,8 @@ GET /profile/user/wow/collections/pets
|
|
|
127
113
|
GET /profile/user/wow/collections/mounts
|
|
128
114
|
```
|
|
129
115
|
|
|
116
|
+
**Note:** For these protected endpoints, you must implement the OAuth authorization code flow in your application to obtain a user access token. The library does not handle the OAuth flow itself - it only accepts and uses the token you provide.
|
|
117
|
+
|
|
130
118
|
# Documentation
|
|
131
119
|
|
|
132
120
|
For detailed documentation on each game's API, see the following README files:
|
|
@@ -0,0 +1,195 @@
|
|
|
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
|
+
_token_expires_at: Token expiration timestamp as datetime object.
|
|
20
|
+
_session: HTTP session for making requests.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
# Region-specific URL mappings
|
|
24
|
+
OAUTH_URLS = {
|
|
25
|
+
"cn": "https://www.gateway.battlenet.com.cn",
|
|
26
|
+
"default": "https://oauth.battle.net",
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
API_URLS = {
|
|
30
|
+
"cn": "https://gateway.battlenet.com.cn",
|
|
31
|
+
"default": "https://{region}.api.blizzard.com",
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
TOKEN_REFRESH_BUFFER = timedelta(minutes=5)
|
|
35
|
+
|
|
36
|
+
def __init__(self, client_id: str, client_secret: str) -> None:
|
|
37
|
+
"""Initialize the API client.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
client_id: Blizzard API client ID.
|
|
41
|
+
client_secret: Blizzard API client secret.
|
|
42
|
+
"""
|
|
43
|
+
self._client_id = client_id
|
|
44
|
+
self._client_secret = client_secret
|
|
45
|
+
self._access_token: Optional[str] = None
|
|
46
|
+
self._token_expires_at: Optional[datetime] = None
|
|
47
|
+
self._session = requests.Session()
|
|
48
|
+
|
|
49
|
+
def _is_token_expired(self) -> bool:
|
|
50
|
+
"""Check if the token is expiring within the refresh buffer window."""
|
|
51
|
+
if self._token_expires_at is None:
|
|
52
|
+
return True
|
|
53
|
+
return datetime.now(UTC) >= self._token_expires_at - self.TOKEN_REFRESH_BUFFER
|
|
54
|
+
|
|
55
|
+
def _get_client_token(self, region: str) -> dict[str, Any]:
|
|
56
|
+
"""Fetch an access token using client credentials flow.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
region: The region for URL construction.
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
The token response from the API.
|
|
63
|
+
"""
|
|
64
|
+
url = self._build_oauth_url("/oauth/token", region)
|
|
65
|
+
response = self._session.post(
|
|
66
|
+
url,
|
|
67
|
+
params={"grant_type": "client_credentials"},
|
|
68
|
+
auth=(self._client_id, self._client_secret),
|
|
69
|
+
)
|
|
70
|
+
response.raise_for_status()
|
|
71
|
+
token_data = response.json()
|
|
72
|
+
|
|
73
|
+
# Store token and calculate expiration
|
|
74
|
+
self._access_token = token_data["access_token"]
|
|
75
|
+
expires_in = token_data.get("expires_in", 86400) # Default 24 hours
|
|
76
|
+
self._token_expires_at = datetime.now(UTC) + timedelta(seconds=expires_in)
|
|
77
|
+
|
|
78
|
+
return token_data
|
|
79
|
+
|
|
80
|
+
def _ensure_valid_token(self, region: str) -> None:
|
|
81
|
+
"""Ensure we have a valid client credentials token."""
|
|
82
|
+
if self._access_token is None or self._is_token_expired():
|
|
83
|
+
self._get_client_token(region)
|
|
84
|
+
|
|
85
|
+
def _build_oauth_url(self, resource: str, region: str) -> str:
|
|
86
|
+
"""Build URL for OAuth endpoints.
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
resource: The API resource path.
|
|
90
|
+
region: The region (us, eu, kr, tw, cn).
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
The complete OAuth URL.
|
|
94
|
+
"""
|
|
95
|
+
base_url = self.OAUTH_URLS.get(region, self.OAUTH_URLS["default"])
|
|
96
|
+
return f"{base_url}{resource}"
|
|
97
|
+
|
|
98
|
+
def _build_api_url(self, resource: str, region: str) -> str:
|
|
99
|
+
"""Build URL for regular API endpoints.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
resource: The API resource path.
|
|
103
|
+
region: The region (us, eu, kr, tw, cn).
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
The complete API URL.
|
|
107
|
+
"""
|
|
108
|
+
if region == "cn":
|
|
109
|
+
base_url = self.API_URLS["cn"]
|
|
110
|
+
else:
|
|
111
|
+
base_url = self.API_URLS["default"].format(region=region)
|
|
112
|
+
return f"{base_url}{resource}"
|
|
113
|
+
|
|
114
|
+
def _make_request(
|
|
115
|
+
self,
|
|
116
|
+
url: str,
|
|
117
|
+
region: str,
|
|
118
|
+
query_params: Optional[dict[str, Any]] = None,
|
|
119
|
+
) -> dict[str, Any]:
|
|
120
|
+
"""Make an authenticated request to the API.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
url: The complete URL to request.
|
|
124
|
+
region: The region for token management.
|
|
125
|
+
query_params: Optional query parameters.
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
The API response as a dictionary.
|
|
129
|
+
"""
|
|
130
|
+
# Prepare query parameters
|
|
131
|
+
params = (query_params or {}).copy()
|
|
132
|
+
|
|
133
|
+
# Check if user provided their own access token
|
|
134
|
+
user_token = params.pop("access_token", None)
|
|
135
|
+
|
|
136
|
+
# Determine which token to use
|
|
137
|
+
if user_token:
|
|
138
|
+
# Use user-provided token (for OAuth user endpoints)
|
|
139
|
+
token = user_token
|
|
140
|
+
else:
|
|
141
|
+
# Ensure client credentials token is valid
|
|
142
|
+
self._ensure_valid_token(region)
|
|
143
|
+
token = self._access_token
|
|
144
|
+
|
|
145
|
+
# Make the request
|
|
146
|
+
response = self._session.get(
|
|
147
|
+
url,
|
|
148
|
+
params=params,
|
|
149
|
+
headers={"Authorization": f"Bearer {token}"},
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
# Handle 401 errors for client credentials (not user tokens)
|
|
153
|
+
if response.status_code == 401 and not user_token:
|
|
154
|
+
# Token might have expired, refresh and retry
|
|
155
|
+
self._get_client_token(region)
|
|
156
|
+
response = self._session.get(
|
|
157
|
+
url,
|
|
158
|
+
params=params,
|
|
159
|
+
headers={"Authorization": f"Bearer {self._access_token}"},
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
response.raise_for_status()
|
|
163
|
+
return response.json()
|
|
164
|
+
|
|
165
|
+
def get_resource(
|
|
166
|
+
self, resource: str, region: str, query_params: Optional[dict[str, Any]] = None
|
|
167
|
+
) -> dict[str, Any]:
|
|
168
|
+
"""Make a request to a regular API resource.
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
resource: The API resource path.
|
|
172
|
+
region: The region to query.
|
|
173
|
+
query_params: Optional query parameters.
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
The API response as a dictionary.
|
|
177
|
+
"""
|
|
178
|
+
url = self._build_api_url(resource, region)
|
|
179
|
+
return self._make_request(url, region, query_params)
|
|
180
|
+
|
|
181
|
+
def get_oauth_resource(
|
|
182
|
+
self, resource: str, region: str, query_params: Optional[dict[str, Any]] = None
|
|
183
|
+
) -> dict[str, Any]:
|
|
184
|
+
"""Make a request to an OAuth API resource.
|
|
185
|
+
|
|
186
|
+
Args:
|
|
187
|
+
resource: The API resource path.
|
|
188
|
+
region: The region to query.
|
|
189
|
+
query_params: Optional query parameters.
|
|
190
|
+
|
|
191
|
+
Returns:
|
|
192
|
+
The API response as a dictionary.
|
|
193
|
+
"""
|
|
194
|
+
url = self._build_oauth_url(resource, region)
|
|
195
|
+
return self._make_request(url, region, query_params)
|
|
@@ -45,39 +45,6 @@ class BlizzardApi(BaseApi):
|
|
|
45
45
|
self.starcraft2 = Starcraft2Api(client_id, client_secret)
|
|
46
46
|
self.battlenet = BattlenetApi(client_id, client_secret)
|
|
47
47
|
|
|
48
|
-
async def __aenter__(self) -> "BlizzardApi":
|
|
49
|
-
"""Async context manager entry.
|
|
50
|
-
|
|
51
|
-
Initializes async sessions for all game API clients.
|
|
52
|
-
|
|
53
|
-
Returns:
|
|
54
|
-
BlizzardApi: The initialized API client.
|
|
55
|
-
"""
|
|
56
|
-
# Initialize async sessions for all game clients
|
|
57
|
-
await self.battlenet.__aenter__()
|
|
58
|
-
await self.diablo3.__aenter__()
|
|
59
|
-
await self.hearthstone.__aenter__()
|
|
60
|
-
await self.wow.__aenter__()
|
|
61
|
-
await self.starcraft2.__aenter__()
|
|
62
|
-
return self
|
|
63
|
-
|
|
64
|
-
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
|
|
65
|
-
"""Async context manager exit.
|
|
66
|
-
|
|
67
|
-
Closes async sessions for all game API clients.
|
|
68
|
-
|
|
69
|
-
Args:
|
|
70
|
-
exc_type: The type of exception that was raised, if any.
|
|
71
|
-
exc_val: The exception instance that was raised, if any.
|
|
72
|
-
exc_tb: The traceback for the exception, if any.
|
|
73
|
-
"""
|
|
74
|
-
# Close async sessions for all game clients
|
|
75
|
-
await self.battlenet.__aexit__(exc_type, exc_val, exc_tb)
|
|
76
|
-
await self.diablo3.__aexit__(exc_type, exc_val, exc_tb)
|
|
77
|
-
await self.hearthstone.__aexit__(exc_type, exc_val, exc_tb)
|
|
78
|
-
await self.wow.__aexit__(exc_type, exc_val, exc_tb)
|
|
79
|
-
await self.starcraft2.__aexit__(exc_type, exc_val, exc_tb)
|
|
80
|
-
|
|
81
48
|
@property
|
|
82
49
|
def client_id(self) -> str:
|
|
83
50
|
"""Get the client ID.
|