Added interface for the MCM Model api

doc_update
Joey Hines 2018-12-17 10:02:43 -06:00
parent 7dc93e4439
commit b8d54ccdcf
6 changed files with 54 additions and 4 deletions

35
MCM_api.py 100644
View File

@ -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

View File

@ -1,6 +1,6 @@
from django.db.models import Q, F from django.db.models import Q, F
from GeoffreyApp.models import * from GeoffreyApp.models import *
from GeoffreyApp.api.BotErrors import * from GeoffreyApp.errors import *
from GeoffreyApp.minecraft_api import * from GeoffreyApp.minecraft_api import *
post_list = [] post_list = []

View File

@ -4,7 +4,7 @@ from django.conf import settings
import inspect import inspect
import GeoffreyApp.api.commands as commands import GeoffreyApp.api.commands as commands
from GeoffreyApp.api.BotErrors import * from GeoffreyApp.errors import *
def getRequiredArgs(func): def getRequiredArgs(func):

View File

@ -85,3 +85,6 @@ class EmptryString(DataBaseError):
class CommandNotFound(DataBaseError): class CommandNotFound(DataBaseError):
"""Command not found""" """Command not found"""
class ExternalLookupFailed(DataBaseError):
"""Entry not found on external database"""

View File

@ -2,7 +2,7 @@ from simplejson.errors import JSONDecodeError
import requests import requests
from GeoffreyApp.api.BotErrors import UsernameLookupFailed from GeoffreyApp.errors import UsernameLookupFailed
uuid_lookup_url = 'https://api.mojang.com/users/profiles/minecraft/{}' uuid_lookup_url = 'https://api.mojang.com/users/profiles/minecraft/{}'
username_lookup_url = 'https://api.mojang.com/user/profiles/{}/names' username_lookup_url = 'https://api.mojang.com/user/profiles/{}/names'

View File

@ -1,3 +1,15 @@
from django.test import TestCase from django.test import TestCase
from GeoffreyApp.MCM_api import get_player
# Create your tests here. # 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)