Skip to content

Commit

Permalink
Add request_api method
Browse files Browse the repository at this point in the history
This will properly handle the absence of the endpoint, returning an
empty dictionary instead of raising an error.
  • Loading branch information
tocuto committed Jul 13, 2022
1 parent 11d9236 commit f664480
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions aiotfm/utils/get_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,38 @@ def __init__(self, **keys):
self.kwargs = keys


async def request_api(url):
"""|coro|
Sends a GET request to the specified URL, expecting a JSON response.
If the client is unable to receive a proper JSON response,
an empty dictionary is returned.
:param url: :class:`str` The URL to send the request to.
"""
headers = {"User-Agent": f"Mozilla/5.0 aiotfm/{__version__}"}

try:
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as resp:
return await resp.json()
except aiohttp.ClientError:
return {}


async def get_ip():
"""|coro|
Fetch the game IP and ports, useful for bots with the official role.
"""
url = 'https://cheese.formice.com/api/tfm/ip'
headers = {"User-Agent": f"Mozilla/5.0 aiotfm/{__version__}"}

async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as resp:
data = await resp.json()
data = await request_api(url)

success = data.pop('success', False)
error = data.pop('error', '').capitalize()
description = data.pop('description', 'No description were provided.')
if not len(data):
# Empty dictionary, request failed, let's use default server IP
success = True
else:
success = data.pop('success', False)
error = data.pop('error', '').capitalize()
description = data.pop('description', 'No description was provided.')

if not success:
if error == 'Maintenance':
Expand All @@ -53,15 +71,11 @@ async def get_keys(tfm_id, token):
:param token: :class:`str` your api token.
"""
url = f'https://api.tocuto.tk/tfm/get/keys/{tfm_id}/{token}'
headers = {"User-Agent": f"Mozilla/5.0 aiotfm/{__version__}"}

async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as resp:
data = await resp.json()
data = await request_api(url)

success = data.pop('success', False)
error = data.pop('error', '').capitalize()
description = data.pop('description', 'No description were provided.')
description = data.pop('description', 'No description was provided.')

if not success:
if error == 'Maintenance':
Expand Down

0 comments on commit f664480

Please sign in to comment.