From fc3c0f44c58ebff28af4caa0f92412defe4b294f Mon Sep 17 00:00:00 2001 From: Sunpy Date: Tue, 29 May 2018 13:38:35 +0200 Subject: [PATCH] Added getFile endpoint --- handlers/file.py | 37 +++++++++++++++++++++++++++++++++++++ web.py | 6 +++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 handlers/file.py diff --git a/handlers/file.py b/handlers/file.py new file mode 100644 index 0000000..605d982 --- /dev/null +++ b/handlers/file.py @@ -0,0 +1,37 @@ +from objects import glob + +allowed_args = ["file_hash", "id", "file_version", "timestamp"] + +def handle(request): + if len([x for x in request.args if x in allowed_args]) == 0: + return {"error": "Missing valid args"} + + for i in range(len(allowed_args)): # Gets the first valid argument and sets it as the method handler + method = request.args.get(allowed_args[i]) + method_name = allowed_args[i] + if method is not None: + break + + output = { + "method": method_name, + "response": callback(method_name, request.args.get(method_name)) + } + return output + +def callback(method, data): + cur = glob.sql.cursor() + + if method is "timestamp": + cur.execute("SELECT * FROM updates WHERE timestamp <= '{}' ORDER BY timestamp DESC LIMIT 1".format( + data + )) + else: + query = "SELECT * FROM updates WHERE {} = {} LIMIT 1" + if method is "file_hash": + query = "SELECT * FROM updates WHERE {} = '{}' LIMIT 1" + cur.execute(query.format( + method, + data + )) + + return cur.fetchone() \ No newline at end of file diff --git a/web.py b/web.py index 1da4abe..6ca5e0c 100644 --- a/web.py +++ b/web.py @@ -2,7 +2,7 @@ import json import MySQLdb import MySQLdb.cursors from flask import Flask, make_response, request, render_template, jsonify -from handlers import update +from handlers import update, file from objects import glob app = Flask(__name__) @@ -30,5 +30,9 @@ def api_index(): def api_update(): return update.handle(request) +@app.route("/api/getFile", methods=["GET", "POST"]) +def api_file(): + return file.handle(request) + if __name__ == "__main__": app.run(**config["web"]) \ No newline at end of file