diff --git a/MCM_api.py b/MCM_api.py new file mode 100644 index 0000000..a5062e0 --- /dev/null +++ b/MCM_api.py @@ -0,0 +1,35 @@ +from django.conf import settings +from GeoffreyApp.errors import ExternalLookupFailed +import requests + +base_url = getattr(settings, 'MCM_BASE_URL', 'localhost') + +player_url = base_url + '/api/model/player/' + + +def add_dashes_to_uuid(uuid): + return '{}-{}-{}-{}-{}'.format(uuid[:8], uuid[8:12], uuid[12:16], uuid[16:20], uuid[20:]) + + +def get_token(): + api_token = getattr(settings, 'MCM_API_TOKEN') + if api_token is not None: + return api_token + else: + raise AttributeError + + +def get_player(player_uuid): + api_token = get_token() + player_uuid = add_dashes_to_uuid(player_uuid) + + try: + params = {"uuid": player_uuid, "api": api_token} + j = requests.get(player_url, params=params).json() + except: + raise ExternalLookupFailed + + return j + + + diff --git a/api/commands.py b/api/commands.py index 4118c2a..f06f3ac 100644 --- a/api/commands.py +++ b/api/commands.py @@ -1,6 +1,6 @@ from django.db.models import Q, F from GeoffreyApp.models import * -from GeoffreyApp.api.BotErrors import * +from GeoffreyApp.errors import * from GeoffreyApp.minecraft_api import * post_list = [] diff --git a/api/views.py b/api/views.py index 9cd85b2..e19b272 100644 --- a/api/views.py +++ b/api/views.py @@ -4,7 +4,7 @@ from django.conf import settings import inspect import GeoffreyApp.api.commands as commands -from GeoffreyApp.api.BotErrors import * +from GeoffreyApp.errors import * def getRequiredArgs(func): diff --git a/api/BotErrors.py b/errors.py similarity index 95% rename from api/BotErrors.py rename to errors.py index 2f6b53d..0463aa8 100644 --- a/api/BotErrors.py +++ b/errors.py @@ -85,3 +85,6 @@ class EmptryString(DataBaseError): class CommandNotFound(DataBaseError): """Command not found""" + +class ExternalLookupFailed(DataBaseError): + """Entry not found on external database""" diff --git a/minecraft_api.py b/minecraft_api.py index 1d39070..121a6f9 100644 --- a/minecraft_api.py +++ b/minecraft_api.py @@ -2,7 +2,7 @@ from simplejson.errors import JSONDecodeError import requests -from GeoffreyApp.api.BotErrors import UsernameLookupFailed +from GeoffreyApp.errors import UsernameLookupFailed uuid_lookup_url = 'https://api.mojang.com/users/profiles/minecraft/{}' username_lookup_url = 'https://api.mojang.com/user/profiles/{}/names' diff --git a/tests.py b/tests.py index 7ce503c..8310cf5 100644 --- a/tests.py +++ b/tests.py @@ -1,3 +1,15 @@ from django.test import TestCase - +from GeoffreyApp.MCM_api import get_player # Create your tests here. + + +class MCM_api_test(TestCase): + + def test_get_player(self): + uuid = "fe7e84132570458892032b69ff188bc3" + players = get_player(uuid) + + if len(players) != 1: + self.fail() + else: + self.assertEqual(players[0]["id"], 4146)