Skip to content

Commit 754c936

Browse files
committed
Rewritten get_presence() and added Error Handler
1 parent d8906e1 commit 754c936

File tree

6 files changed

+67
-17
lines changed

6 files changed

+67
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ print(account_info.Gamertag)
2727

2828
# Get user presence
2929
presence = xpa.get_presence(xuid="xuid")
30-
print(presence.state)
30+
print(presence.devices)
3131
```
3232

3333
Full documentation can be found [here](https://github.com/Rarmash/Xbox-Python-API/tree/master/docs).

docs/XPA.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ The `XPA` class provides methods for accessing Xbox Live API endpoints to retrie
7777
- `xuid` (str): XUID of the specified user.
7878
- **Returns:** An instance of `XUID_PRESENCE` with the following attributes:
7979
- `state` (str): User's state.
80-
- `last_seen_device_type` (str): User's last seen device type.
81-
- `last_seen_title_id` (str): User's last seen title ID.
82-
- `last_seen_title_name` (str): User's last seen title name.
83-
- `last_seen_timestamp` (str): User's last seen timestamp.
80+
- `devices` (list): List of devices the user is present on with titles information.
8481

8582
### `get_user_achievements(xuid: str) -> list`
8683
- **Description:** Get user achievements using their XUID.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def read(f_name):
99

1010
setup(
1111
name="xbox-python-api",
12-
version="0.1.3",
12+
version="0.2",
1313
url="https://github.com/Rarmash/Xbox-Python-API",
1414
author="Rarmash",
1515
description="Xbox API library",

xpa/ErrorHandler.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class XboxApiError(Exception):
2+
status_code = None
3+
4+
def __init__(self, message):
5+
self.message = message
6+
super().__init__(message)
7+
8+
9+
class XboxApiBadRequestError(XboxApiError):
10+
status_code = 400
11+
12+
13+
class XboxApiAuthError(XboxApiError):
14+
status_code = 401
15+
16+
17+
class XboxApiForbiddenError(XboxApiError):
18+
status_code = 403
19+
20+
21+
class XboxApiNotFoundError(XboxApiError):
22+
status_code = 404
23+
24+
25+
class XboxApiRateLimitError(XboxApiError):
26+
status_code = 429
27+
28+
29+
class XboxApiRequestError(XboxApiError):
30+
status_code = 500
31+
32+
33+
class XboxApiServerError(XboxApiError):
34+
status_code = 503

xpa/XPA.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
from .classes.FRIEND_INFO_GAMERTAG import FRIEND_INFO_GAMERTAG
88
from .classes.PLAYER_SUMMARY import PLAYER_SUMMARY
99
from .classes.XUID_PRESENCE import XUID_PRESENCE
10+
from .ErrorHandler import *
1011

1112

1213
class XPA:
1314
"""
1415
Xbox API class
1516
"""
17+
1618
def __init__(self, api_key):
1719
"""
1820
Initialize the XPA class
@@ -37,7 +39,27 @@ def _make_request(self, endpoint):
3739
requests.models.Response: response from the request.
3840
"""
3941
headers = {'x-authorization': self.api_key}
40-
return requests.get(endpoint, headers=headers)
42+
try:
43+
response = requests.get(endpoint, headers=headers)
44+
response.raise_for_status()
45+
except requests.exceptions.HTTPError as http_err:
46+
if response.status_code == 400:
47+
raise XboxApiBadRequestError("Bad request") from http_err
48+
elif response.status_code == 401:
49+
raise XboxApiAuthError("Unauthorized") from http_err
50+
elif response.status_code == 403:
51+
raise XboxApiForbiddenError("Forbidden") from http_err
52+
elif response.status_code == 404:
53+
raise XboxApiNotFoundError("Not found") from http_err
54+
elif response.status_code == 429:
55+
raise XboxApiRateLimitError("Rate limit exceeded") from http_err
56+
elif response.status_code == 500:
57+
raise XboxApiBadRequestError("Internal server error") from http_err
58+
elif response.status_code == 503:
59+
raise XboxApiServerError("Service unavailable") from http_err
60+
except Exception as err:
61+
raise XboxApiError("An error occurred") from err
62+
return response
4163

4264
def _find_setting_by_id(self, settings, setting_id):
4365
"""
@@ -145,7 +167,10 @@ def get_account_info_gamertag(self, gamertag: str) -> ACCOUNT_INFO_GAMERTAG:
145167
"""
146168
endpoint = self.url.search_gamertag_url(gamertag)
147169
response = self._make_request(endpoint).json()
148-
user_data = response['people'][0]
170+
try:
171+
user_data = response['people'][0]
172+
except IndexError:
173+
raise XboxApiNotFoundError("User not found")
149174
account_info = ACCOUNT_INFO_GAMERTAG(
150175
xuid=user_data["xuid"],
151176
displayName=user_data["displayName"],
@@ -211,10 +236,7 @@ def get_presence(self, xuid: str) -> XUID_PRESENCE:
211236
user_data = response[0]
212237
presence = XUID_PRESENCE(
213238
state=user_data["state"],
214-
last_seen_device_type=user_data["lastSeen"]["deviceType"],
215-
last_seen_title_id=user_data["lastSeen"]["titleId"],
216-
last_seen_title_name=user_data["lastSeen"]["titleName"],
217-
last_seen_timestamp=user_data["lastSeen"]["timestamp"]
239+
devices=user_data["devices"]
218240
)
219241
return presence
220242

xpa/classes/XUID_PRESENCE.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
class XUID_PRESENCE:
2-
def __init__(self, state, last_seen_device_type, last_seen_title_id, last_seen_title_name, last_seen_timestamp):
2+
def __init__(self, state, devices):
33
self.state = state
4-
self.last_seen_device_type = last_seen_device_type
5-
self.last_seen_title_id = last_seen_title_id
6-
self.last_seen_title_name = last_seen_title_name
7-
self.last_seen_timestamp = last_seen_timestamp
4+
self.devices = devices

0 commit comments

Comments
 (0)