getList and small improv

This commit is contained in:
Emily 2019-03-11 13:25:39 +01:00
parent bb9ee9da8d
commit 6afbcb69f1
6 changed files with 64 additions and 42 deletions

View File

@ -1,4 +1,4 @@
{ {
"python.pythonPath": "/usr/bin/python3", "python.pythonPath": "C:\\Users\\Emily\\AppData\\Local\\Programs\\Python\\Python36\\python.exe",
"python.linting.pylintEnabled": true "python.linting.pylintEnabled": true
} }

View File

@ -1,31 +0,0 @@
from objects import glob
allowed_args = ["file_hash", "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",
"allowed": allowed_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
return callback(method_name)
def callback(method):
cur = glob.sql.cursor()
cur.execute("SELECT {} FROM updates WHERE filename = 'osu!.exe' ORDER BY file_version".format(
method
))
data = []
for row in cur.fetchall():
data.append(row[method])
return data

View File

@ -1,3 +1,4 @@
import traceback
import json import json
import tornado.gen import tornado.gen
@ -27,7 +28,7 @@ class handler(asyncTornado.asyncRequestHandler):
data = {} data = {}
try: try:
args_filter = asyncTornado.check_arguments(self.request.arguments, ARGS) args_filter = asyncTornado.check_arguments(self.request.arguments, ARGS)
if False in args_filter: if not args_filter:
raise Exception("Missing required arguments") raise Exception("Missing required arguments")
method = args_filter[0] method = args_filter[0]
@ -54,6 +55,7 @@ class handler(asyncTornado.asyncRequestHandler):
data["status"] = status_code data["status"] = status_code
data["message"] = str(e) data["message"] = str(e)
print("Err: %s" % e) print("Err: %s" % e)
traceback.print_exc()
finally: finally:
self.write( json.dumps(data) ) self.write( json.dumps(data) )
self.set_header("Content-Type", "application/json") self.set_header("Content-Type", "application/json")

52
api/v1/getList.py Normal file
View File

@ -0,0 +1,52 @@
import traceback
import json
import tornado.gen
import tornado.web
from web import asyncTornado
from constants import argumentTypes
from objects import glob
ARGS = {
("file_hash", "file_version", "timestamp"): argumentTypes.one_required
}
SQL_STRUCT = {
"main": "SELECT %s FROM updates WHERE filename = 'osu!.exe' ORDER BY file_version"
}
class handler(asyncTornado.asyncRequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def asyncGet(self):
status_code = 400
data = {}
try:
args_filter = asyncTornado.check_arguments(self.request.arguments, ARGS)
if not args_filter:
raise Exception("Missing required arguments")
conn = glob.new_sql()
cur = conn.cursor()
sql = SQL_STRUCT["main"]
cur.execute(sql % ",".join(args_filter))
data = cur.fetchall()
cur.close()
conn.close()
status_code = 200
except Exception as e:
status_code = 400
data["status"] = status_code
data["message"] = str(e)
print("Err: %s" % e)
traceback.print_exc()
finally:
self.write( json.dumps(data) )
self.set_header("Content-Type", "application/json")
self.set_status(status_code)

View File

@ -27,7 +27,7 @@ class handler(asyncTornado.asyncRequestHandler):
data = {} data = {}
try: try:
args_filter = asyncTornado.check_arguments(self.request.arguments, ARGS) args_filter = asyncTornado.check_arguments(self.request.arguments, ARGS)
if False in args_filter: if not args_filter:
raise Exception("Missing required arguments") raise Exception("Missing required arguments")
method = args_filter[0] method = args_filter[0]

View File

@ -69,17 +69,15 @@ def runBackground(data, callback):
glob.pool.apply_async(func, args, kwargs, _callback) glob.pool.apply_async(func, args, kwargs, _callback)
def check_arguments(arguments, arguments_filter): def check_arguments(arguments, arguments_filter):
filter_pass = []
for k, v in arguments_filter.items(): for k, v in arguments_filter.items():
if v == argumentTypes.optional: if v == argumentTypes.optional:
filter_pass.append( arg_filter_and(arguments, k) ) return arg_filter_and(arguments, k)
elif v == argumentTypes.required: elif v == argumentTypes.required:
filter_pass.append( arg_filter_require_all(arguments, k) ) return arg_filter_require_all(arguments, k)
elif v == argumentTypes.one_required: elif v == argumentTypes.one_required:
filter_pass.append( arg_filter_first(arguments, k, False) ) return arg_filter_first(arguments, k, False)
elif v == argumentTypes.only_one: elif v == argumentTypes.only_one:
filter_pass.append( arg_filter_only_one(arguments, k) ) return arg_filter_only_one(arguments, k)
return filter_pass
def arg_filter_and(arguments, filter, can_false = False): def arg_filter_and(arguments, filter, can_false = False):
arg_filter = [] arg_filter = []
@ -104,7 +102,8 @@ def arg_filter_only_one(arguments, required):
return True if len(arg_filter) == 1 else False return True if len(arg_filter) == 1 else False
def arg_filter_first(arguments, filter, optional = True): def arg_filter_first(arguments, filter, optional = True):
arg_filter = []
for i in filter: for i in filter:
if i in arguments: if i in arguments:
return i arg_filter.append(i)
return optional return arg_filter if len(arg_filter) else optional