Skip to content

Commit 9e3667d

Browse files
committed
example: Annotate the main example code
1 parent db88814 commit 9e3667d

2 files changed

Lines changed: 77 additions & 26 deletions

File tree

README.md

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,75 +56,103 @@ Example: Search Xbox Live via cmdline tool
5656
API usage
5757

5858
```py
59-
import sys
6059
import asyncio
60+
import sys
61+
6162
from httpx import HTTPStatusError
62-
from xbox.webapi.common.signed_session import SignedSession
63+
6364
from xbox.webapi.api.client import XboxLiveClient
6465
from xbox.webapi.authentication.manager import AuthenticationManager
6566
from xbox.webapi.authentication.models import OAuth2TokenResponse
67+
from xbox.webapi.common.signed_session import SignedSession
6668
from xbox.webapi.scripts import CLIENT_ID, CLIENT_SECRET, TOKENS_FILE
6769

68-
# This uses the default client identification by OpenXbox
69-
# Feel free to use your own here
70+
"""
71+
This uses the global default client identification by OpenXbox
72+
You can supply your own parameters here if you are permitted to create
73+
new Microsoft OAuth Apps and know what you are doing
74+
"""
7075
client_id = CLIENT_ID
7176
client_secret = CLIENT_SECRET
7277
tokens_file = TOKENS_FILE
7378

7479
"""
7580
For doing authentication, see xbox/webapi/scripts/authenticate.py
7681
"""
82+
83+
7784
async def async_main():
85+
# Create a HTTP client session
7886
async with SignedSession() as session:
79-
auth_mgr = AuthenticationManager(
80-
session, client_id, client_secret, ""
81-
)
82-
87+
"""
88+
Initialize with global OAUTH parameters from above
89+
"""
90+
auth_mgr = AuthenticationManager(session, client_id, client_secret, "")
91+
92+
"""
93+
Read in tokens that you received from the `xbox-authenticate`-script previously
94+
See `xbox/webapi/scripts/authenticate.py`
95+
"""
8396
try:
84-
with open(tokens_file, mode="r") as f:
85-
tokens = f.read()
97+
with open(tokens_file) as f:
98+
tokens = f.read()
99+
# Assign gathered tokens
86100
auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens)
87101
except FileNotFoundError:
88-
print(f'File {tokens_file} isn`t found or it doesn`t contain tokens!')
102+
print(f"File {tokens_file} isn`t found or it doesn`t contain tokens!")
89103
exit(-1)
90-
104+
105+
"""
106+
Refresh tokens, just in case
107+
You could also manually check the token lifetimes and just refresh them
108+
if they are close to expiry
109+
"""
91110
try:
92-
await auth_mgr.refresh_tokens()
111+
await auth_mgr.refresh_tokens()
93112
except HTTPStatusError:
94-
print("Could not refresh tokens")
95-
sys.exit(-1)
113+
print("Could not refresh tokens")
114+
sys.exit(-1)
96115

116+
# Save the refreshed/updated tokens
97117
with open(tokens_file, mode="w") as f:
98-
f.write(auth_mgr.oauth.json())
99-
print(f'Refreshed tokens in {tokens_file}!')
118+
f.write(auth_mgr.oauth.json())
119+
print(f"Refreshed tokens in {tokens_file}!")
100120

121+
"""
122+
Construct the Xbox API client from AuthenticationManager instance
123+
"""
101124
xbl_client = XboxLiveClient(auth_mgr)
102125

103-
# Some example API calls
126+
"""
127+
Some example API calls
128+
"""
104129
# Get friendslist
105130
friendslist = await xbl_client.people.get_friends_own()
106-
print('Your friends:')
131+
print("Your friends:")
107132
print(friendslist)
108133
print()
109134

110135
# Get presence status (by list of XUID)
111-
presence = await xbl_client.presence.get_presence_batch(["2533274794093122", "2533274807551369"])
112-
print('Statuses of some random players by XUID:')
136+
presence = await xbl_client.presence.get_presence_batch(
137+
["2533274794093122", "2533274807551369"]
138+
)
139+
print("Statuses of some random players by XUID:")
113140
print(presence)
114141
print()
115142

116143
# Get messages
117144
messages = await xbl_client.message.get_inbox()
118-
print('Your messages:')
145+
print("Your messages:")
119146
print(messages)
120147
print()
121148

122149
# Get profile by GT
123150
profile = await xbl_client.profile.get_profile_by_gamertag("SomeGamertag")
124-
print('Profile under SomeGamertag gamer tag:')
151+
print("Profile under SomeGamertag gamer tag:")
125152
print(profile)
126153
print()
127154

155+
128156
asyncio.run(async_main())
129157
```
130158

readme_example.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
from xbox.webapi.common.signed_session import SignedSession
1010
from xbox.webapi.scripts import CLIENT_ID, CLIENT_SECRET, TOKENS_FILE
1111

12-
# This uses the default client identification by OpenXbox
13-
# Feel free to use your own here
12+
"""
13+
This uses the global default client identification by OpenXbox
14+
You can supply your own parameters here if you are permitted to create
15+
new Microsoft OAuth Apps and know what you are doing
16+
"""
1417
client_id = CLIENT_ID
1518
client_secret = CLIENT_SECRET
1619
tokens_file = TOKENS_FILE
@@ -21,30 +24,50 @@
2124

2225

2326
async def async_main():
27+
# Create a HTTP client session
2428
async with SignedSession() as session:
29+
"""
30+
Initialize with global OAUTH parameters from above
31+
"""
2532
auth_mgr = AuthenticationManager(session, client_id, client_secret, "")
2633

34+
"""
35+
Read in tokens that you received from the `xbox-authenticate`-script previously
36+
See `xbox/webapi/scripts/authenticate.py`
37+
"""
2738
try:
2839
with open(tokens_file) as f:
2940
tokens = f.read()
41+
# Assign gathered tokens
3042
auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens)
3143
except FileNotFoundError:
3244
print(f"File {tokens_file} isn`t found or it doesn`t contain tokens!")
3345
exit(-1)
3446

47+
"""
48+
Refresh tokens, just in case
49+
You could also manually check the token lifetimes and just refresh them
50+
if they are close to expiry
51+
"""
3552
try:
3653
await auth_mgr.refresh_tokens()
3754
except HTTPStatusError:
3855
print("Could not refresh tokens")
3956
sys.exit(-1)
4057

58+
# Save the refreshed/updated tokens
4159
with open(tokens_file, mode="w") as f:
4260
f.write(auth_mgr.oauth.json())
4361
print(f"Refreshed tokens in {tokens_file}!")
4462

63+
"""
64+
Construct the Xbox API client from AuthenticationManager instance
65+
"""
4566
xbl_client = XboxLiveClient(auth_mgr)
4667

47-
# Some example API calls
68+
"""
69+
Some example API calls
70+
"""
4871
# Get friendslist
4972
friendslist = await xbl_client.people.get_friends_own()
5073
print("Your friends:")

0 commit comments

Comments
 (0)