From 84ac092ffbea5210d4454b65bca37c5466f2de8c Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Fri, 26 Jul 2019 18:17:46 -0500 Subject: [PATCH] Small refactor + renamed GeoffreyApiHelper to geoffrey_formatter + moved some utility functions to util --- GeoffreyBot/geoffrey.py | 2 +- GeoffreyBot/geoffrey_api.py | 54 +------------------ ...freyApiHelper.py => geoffrey_formatter.py} | 6 +-- GeoffreyBot/util.py | 50 +++++++++++++++++ 4 files changed, 56 insertions(+), 56 deletions(-) rename GeoffreyBot/{GeoffreyApiHelper.py => geoffrey_formatter.py} (94%) create mode 100644 GeoffreyBot/util.py diff --git a/GeoffreyBot/geoffrey.py b/GeoffreyBot/geoffrey.py index 5d6c88c..1019ad4 100644 --- a/GeoffreyBot/geoffrey.py +++ b/GeoffreyBot/geoffrey.py @@ -8,7 +8,7 @@ import time import traceback import sys from GeoffreyBot.geoffrey_api import HandledError -from GeoffreyBot.GeoffreyApiHelper import format_message +from GeoffreyBot.geoffrey_formatter import format_message logger = logging.getLogger('GeoffreyBot') diff --git a/GeoffreyBot/geoffrey_api.py b/GeoffreyBot/geoffrey_api.py index 67c01fb..c0522e1 100644 --- a/GeoffreyBot/geoffrey_api.py +++ b/GeoffreyBot/geoffrey_api.py @@ -1,57 +1,7 @@ from discord.ext import commands from GeoffreyBot.DiscordHelperFunctions import * -from GeoffreyBot.GeoffreyApiHelper import * - -import requests - -default_error_messages = { - "PlayerNotFound": "You are not in the database, do ?register first!", - "NoLocationsInDatabase": "You have no locations in the database, you need to add some first!", - "DataError": "Slow down their slugger, that's a long word or number you are trying to cram into me, try again with " - "something smaller, please", - "LocationHasTunnelError": "that location already has a tunnel you goober.", - "EntryNameNotUniqueError": "that name has already been used, be more creative dingus kong" -} - - -class HandledError(Exception): - pass - - -class CommandError: - def __init__(self, error, message): - self.error = error - self.message = message - - -async def run_command(ctx, base_url, api_token, request_type, command, errors=None, **kwargs): - URL = base_url + '/GeoffreyApp/api/command/{}/' - - kwargs["api"] = api_token - - if request_type == "GET": - response = requests.get(url=URL.format(command), params=kwargs) - elif request_type == "POST": - response = requests.post(url=URL.format(command), data=kwargs) - else: - raise TypeError - - json = response.json() - - if "error" in json: - error_name = json["error"] - if error_name in errors: - msg = errors[error_name] - elif error_name in default_error_messages: - msg = default_error_messages[error_name] - else: - raise Exception(json['error'], json["error_message"]) - - await ctx.send("{}, {}".format(ctx.message.author.mention, msg)) - - raise HandledError - else: - return json +from GeoffreyBot.geoffrey_formatter import * +from GeoffreyBot.util import run_command, HandledError class GeoffreyCommands(commands.Cog): diff --git a/GeoffreyBot/GeoffreyApiHelper.py b/GeoffreyBot/geoffrey_formatter.py similarity index 94% rename from GeoffreyBot/GeoffreyApiHelper.py rename to GeoffreyBot/geoffrey_formatter.py index 65a3a5d..573fdde 100644 --- a/GeoffreyBot/GeoffreyApiHelper.py +++ b/GeoffreyBot/geoffrey_formatter.py @@ -27,10 +27,10 @@ def formatted_item_listing(item): def formatted_location(location): if location["tunnel"] is not None: return format_message("**{}** @ **{}** **{}** {}", location["name"], formatted_position(location), - location["tunnel"], - formatted_owners(location)) + location["tunnel"], formatted_owners(location)) else: - return format_message("**{}** @ **{}** {}", location["name"], formatted_position(location), formatted_owners(location)) + return format_message("**{}** @ **{}** {}", location["name"], formatted_position(location), + formatted_owners(location)) def formatted_tunnel(tunnel): diff --git a/GeoffreyBot/util.py b/GeoffreyBot/util.py new file mode 100644 index 0000000..9d07478 --- /dev/null +++ b/GeoffreyBot/util.py @@ -0,0 +1,50 @@ +import requests + +default_error_messages = { + "PlayerNotFound": "You are not in the database, do ?register first!", + "NoLocationsInDatabase": "You have no locations in the database, you need to add some first!", + "DataError": "Slow down their slugger, that's a long word or number you are trying to cram into me, try again with " + "something smaller, please", + "LocationHasTunnelError": "that location already has a tunnel you goober.", + "EntryNameNotUniqueError": "that name has already been used, be more creative dingus kong" +} + + +class HandledError(Exception): + pass + + +class CommandError: + def __init__(self, error, message): + self.error = error + self.message = message + + +async def run_command(ctx, base_url, api_token, request_type, command, errors=None, **kwargs): + URL = base_url + '/GeoffreyApp/api/command/{}/' + + kwargs["api"] = api_token + + if request_type == "GET": + response = requests.get(url=URL.format(command), params=kwargs) + elif request_type == "POST": + response = requests.post(url=URL.format(command), data=kwargs) + else: + raise TypeError + + json = response.json() + + if "error" in json: + error_name = json["error"] + if error_name in errors: + msg = errors[error_name] + elif error_name in default_error_messages: + msg = default_error_messages[error_name] + else: + raise Exception(json['error'], json["error_message"]) + + await ctx.send("{}, {}".format(ctx.message.author.mention, msg)) + + raise HandledError + else: + return json \ No newline at end of file