Added getUpdate endpoint

This commit is contained in:
Emily 2018-05-29 13:28:42 +02:00
parent 3e9a7d05d7
commit 8752f2a431
3 changed files with 25 additions and 7 deletions

View File

@ -1,3 +1,5 @@
from objects import glob
allowed_args = ["file_hash", "file_version", "timestamp"] allowed_args = ["file_hash", "file_version", "timestamp"]
def handle(request): def handle(request):
@ -11,7 +13,22 @@ def handle(request):
break break
output = { output = {
"search_method": method_name "method": method_name,
"response": callback(method_name, request.args.get(method_name))
} }
output["endpoint"] = request.endpoint return output
return output
def callback(method, data):
cur = glob.sql.cursor()
if method is "file_hash":
cur.execute("SELECT * FROM updates WHERE file_hash = '{}'".format(
data
))
else:
cur.execute("SELECT a.* FROM updates a INNER JOIN ( SELECT MAX(file_version) file_version, filename FROM updates WHERE {} < {} GROUP BY filename) b ON a.file_version = b.file_version;".format(
method,
data
))
return cur.fetchall()

1
objects/glob.py Normal file
View File

@ -0,0 +1 @@
sql = None

8
web.py
View File

@ -3,12 +3,16 @@ 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
from objects import glob
app = Flask(__name__) app = Flask(__name__)
with open("config.json", "r") as f: with open("config.json", "r") as f:
config = json.load(f) config = json.load(f)
# Setup sql
glob.sql = MySQLdb.connect(**config["sql"], cursorclass = MySQLdb.cursors.DictCursor)
@app.route("/") @app.route("/")
@app.route("/home") @app.route("/home")
def home_index(): def home_index():
@ -26,9 +30,5 @@ def api_index():
def api_update(): def api_update():
return update.handle(request) return update.handle(request)
@app.route("/api")
def api_index():
return render_template("api.html")
if __name__ == "__main__": if __name__ == "__main__":
app.run(**config["web"]) app.run(**config["web"])