Rewrote api handle

This commit is contained in:
Emily 2019-03-07 14:04:54 +01:00
parent 7149885c9d
commit e491530689
8 changed files with 65 additions and 45 deletions

7
api/__init__.py Normal file
View File

@ -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=[""])

14
api/missing.py Normal file
View File

@ -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"
}

6
api/v1/__init__.py Normal file
View File

@ -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=[""])

38
main.py Normal file
View File

@ -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/<ver>/<func>")
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"])

45
web.py
View File

@ -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"])