mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
Handle "player not found" errors correctly
This commit is contained in:
parent
54ebf6b9a7
commit
03cc06d79c
1 changed files with 14 additions and 3 deletions
17
overwatch.py
17
overwatch.py
|
@ -2,6 +2,10 @@ import asyncio
|
||||||
import requests
|
import requests
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
|
|
||||||
|
class NotFoundException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
# Get player data
|
# Get player data
|
||||||
async def get_player_data(platform: str, region: str, battletag: str, **kwargs):
|
async def get_player_data(platform: str, region: str, battletag: str, **kwargs):
|
||||||
print("[Overwatch] Getting player info for: {platform} {region} {battletag}".format(platform=platform,
|
print("[Overwatch] Getting player info for: {platform} {region} {battletag}".format(platform=platform,
|
||||||
|
@ -14,9 +18,16 @@ async def get_player_data(platform: str, region: str, battletag: str, **kwargs):
|
||||||
'https://api.lootbox.eu/{platform}/{region}/{battletag}/profile'.format(**locals()))
|
'https://api.lootbox.eu/{platform}/{region}/{battletag}/profile'.format(**locals()))
|
||||||
# Ensure the request is successful
|
# Ensure the request is successful
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
return r.json()
|
# Parse json and check for the status code inside the response
|
||||||
elif r.status_code == 404:
|
pj = r.json()
|
||||||
raise Exception("Player not found.")
|
if "statusCode" in pj:
|
||||||
|
if pj["statusCode"] == 404:
|
||||||
|
raise NotFoundException("Player not found.")
|
||||||
|
else:
|
||||||
|
raise Exception("Unhandled API response.")
|
||||||
|
else:
|
||||||
|
# Success!
|
||||||
|
return pj
|
||||||
else:
|
else:
|
||||||
raise Exception("Unhandled API response.")
|
raise Exception("Unhandled API response.")
|
||||||
|
|
Loading…
Reference in a new issue