From e491530689168099f27248005f913c99831a0695 Mon Sep 17 00:00:00 2001 From: Sunpy Date: Thu, 7 Mar 2019 14:04:54 +0100 Subject: [PATCH] Rewrote api handle --- api/__init__.py | 7 +++ api/missing.py | 14 ++++++ api/v1/__init__.py | 6 +++ handlers/getfile.py => api/v1/getFile.py | 0 handlers/getlist.py => api/v1/getList.py | 0 handlers/getupdate.py => api/v1/getUpdate.py | 0 main.py | 38 +++++++++++++++++ web.py | 45 -------------------- 8 files changed, 65 insertions(+), 45 deletions(-) create mode 100644 api/__init__.py create mode 100644 api/missing.py create mode 100644 api/v1/__init__.py rename handlers/getfile.py => api/v1/getFile.py (100%) rename handlers/getlist.py => api/v1/getList.py (100%) rename handlers/getupdate.py => api/v1/getUpdate.py (100%) create mode 100644 main.py delete mode 100644 web.py diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..1bf7665 --- /dev/null +++ b/api/__init__.py @@ -0,0 +1,7 @@ +from . import missing +from os import listdir + +versions = {} + +for d in [d for d in listdir(__name__) if d not in ["__init__.py", "__pycache__", "missing.py"]]: + versions[d] = __import__("%s.%s" % (__name__, d), fromlist=[""]) diff --git a/api/missing.py b/api/missing.py new file mode 100644 index 0000000..edb32b1 --- /dev/null +++ b/api/missing.py @@ -0,0 +1,14 @@ +from objects import glob + +allowed_args = ["file_hash", "file_version", "timestamp"] + +def handle(request): + print(request) + + return callback() + +def callback(): + return { + "error": 404, + "message": "Missing api endpoint" + } \ No newline at end of file diff --git a/api/v1/__init__.py b/api/v1/__init__.py new file mode 100644 index 0000000..7156d53 --- /dev/null +++ b/api/v1/__init__.py @@ -0,0 +1,6 @@ +from os import listdir + +handlers = {} + +for d in [d.rstrip(".py") for d in listdir(__name__.replace(".", "/")) if d not in ["__init__.py", "__pycache__"]]: + handlers[d] = __import__("%s.%s" % (__name__, d), fromlist=[""]) \ No newline at end of file diff --git a/handlers/getfile.py b/api/v1/getFile.py similarity index 100% rename from handlers/getfile.py rename to api/v1/getFile.py diff --git a/handlers/getlist.py b/api/v1/getList.py similarity index 100% rename from handlers/getlist.py rename to api/v1/getList.py diff --git a/handlers/getupdate.py b/api/v1/getUpdate.py similarity index 100% rename from handlers/getupdate.py rename to api/v1/getUpdate.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..445082f --- /dev/null +++ b/main.py @@ -0,0 +1,38 @@ +import json +#import MySQLdb +#import MySQLdb.cursors +from flask import Flask, make_response, request, render_template, jsonify +from objects import glob + +import api + +app = Flask(__name__) + +with open("config.json", "r") as f: + config = json.load(f) + +# Setup sql +#glob.sql = MySQLdb.connect(**config["sql"], cursorclass = MySQLdb.cursors.DictCursor) + +@app.route("/") +@app.route("/home") +def home_index(): + return render_template("index.html") + +@app.route("/download") +def download_index(): + return render_template("download.html") + +@app.route("/api") +def api_index(): + return render_template("api.html") + +@app.route("/api//") +def api_call(ver, func): + if ver not in api.versions or func not in api.versions[ver].handlers: + return jsonify(api.missing.handle(request)) + data = api.versions[ver].handlers[func].handle(request) + return jsonify(data) + +if __name__ == "__main__": + app.run(**config["web"]) \ No newline at end of file diff --git a/web.py b/web.py deleted file mode 100644 index ddde1a7..0000000 --- a/web.py +++ /dev/null @@ -1,45 +0,0 @@ -import json -import MySQLdb -import MySQLdb.cursors -from flask import Flask, make_response, request, render_template, jsonify -from handlers import getupdate, getfile, getlist -from objects import glob - -app = Flask(__name__) - -with open("config.json", "r") as f: - config = json.load(f) - -# Setup sql -glob.sql = MySQLdb.connect(**config["sql"], cursorclass = MySQLdb.cursors.DictCursor) - -@app.route("/") -@app.route("/home") -def home_index(): - return render_template("index.html") - -@app.route("/download") -def download_index(): - return render_template("download.html") - -@app.route("/api") -def api_index(): - return render_template("api.html") - -@app.route("/api/getUpdate", methods=["GET", "POST"]) -def api_update(): - data = getupdate.handle(request) - return jsonify(data) - -@app.route("/api/getFile", methods=["GET", "POST"]) -def api_file(): - data = getfile.handle(request) - return jsonify(data) - -@app.route("/api/getList", methods=["GET", "POST"]) -def api_list(): - data = getlist.handle(request) - return jsonify(data) - -if __name__ == "__main__": - app.run(**config["web"]) \ No newline at end of file