Added getFile endpoint

This commit is contained in:
Emily 2018-05-29 13:38:35 +02:00
parent 8752f2a431
commit fc3c0f44c5
2 changed files with 42 additions and 1 deletions

37
handlers/file.py Normal file
View File

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

6
web.py
View File

@ -2,7 +2,7 @@ import json
import MySQLdb import MySQLdb
import MySQLdb.cursors import MySQLdb.cursors
from flask import Flask, make_response, request, render_template, jsonify from flask import Flask, make_response, request, render_template, jsonify
from handlers import update from handlers import update, file
from objects import glob from objects import glob
app = Flask(__name__) app = Flask(__name__)
@ -30,5 +30,9 @@ def api_index():
def api_update(): def api_update():
return update.handle(request) return update.handle(request)
@app.route("/api/getFile", methods=["GET", "POST"])
def api_file():
return file.handle(request)
if __name__ == "__main__": if __name__ == "__main__":
app.run(**config["web"]) app.run(**config["web"])