Add errors.py, get_player, /player.

This commit is contained in:
Ceda EI 2019-03-13 18:52:44 +05:30
parent 1239cd08cc
commit 2315b05f2c
2 changed files with 33 additions and 9 deletions

3
errors.py Normal file
View File

@ -0,0 +1,3 @@
_400 = {"error": "insufficient parameters"}
_401 = {"error": "invalid token"}
_404 = {"error": "The quest/side quest doesn't exist"}

View File

@ -2,26 +2,47 @@
import questable import questable
import sqlite3 import sqlite3
import errors
from flask import Flask, jsonify, request from flask import Flask, jsonify, request
app = Flask(__name__) app = Flask(__name__)
db = sqlite3.connect("questable.db", check_same_thread=False) db = sqlite3.connect("questable.db", check_same_thread=False)
# /auth. # Returns the player object if valid token
def auth(db): def get_player(db):
success = jsonify({"success": True})
failure = jsonify({"success": False})
try: try:
token = request.args['token'] token = request.args['token']
except (AttributeError): except (AttributeError):
return failure return False
except (KeyError): except (KeyError):
return failure return False
if questable.get_player_from_token(db, token) is False: return questable.get_player_from_token(db, token)
return failure
# /auth.
def auth(db):
if get_player(db) is False:
return jsonify({"success": False})
else: else:
return success return jsonify({"success": True})
app.add_url_rule('/auth', '/auth', lambda: auth(db), methods=['GET']) app.add_url_rule('/auth', '/auth', lambda: auth(db), methods=['GET'])
# /player
def player(db):
player = get_player(db)
if player is False:
return jsonify(errors._401), 401
return jsonify({
"xp": player.get_points(),
"quests_completed": len(player.get_quests()),
"total_quests": len(player.get_quests(None)),
"side_quests_completed": len(player.get_side_quests()),
"total_side_quests": len(player.get_side_quests(None)),
})
app.add_url_rule('/player', '/player', lambda: player(db), methods=['GET'])