77from .classes .FRIEND_INFO_GAMERTAG import FRIEND_INFO_GAMERTAG
88from .classes .PLAYER_SUMMARY import PLAYER_SUMMARY
99from .classes .XUID_PRESENCE import XUID_PRESENCE
10+ from .ErrorHandler import *
1011
1112
1213class 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
0 commit comments