Working getUpdate api
This commit is contained in:
parent
e3eaba33ed
commit
0425859878
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -101,4 +101,5 @@ ENV/
|
|||
.mypy_cache/
|
||||
|
||||
# custom
|
||||
wayback.db
|
||||
wayback.db
|
||||
.vscode/
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
|
@ -1,4 +1,4 @@
|
|||
{
|
||||
"python.pythonPath": "C:\\Users\\Emily\\AppData\\Local\\Programs\\Python\\Python36\\python.exe",
|
||||
"python.pythonPath": "/usr/bin/python3",
|
||||
"python.linting.pylintEnabled": true
|
||||
}
|
|
@ -1,34 +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, request.args.get(method_name))
|
||||
|
||||
def callback(method, data):
|
||||
cur = glob.sql.cursor()
|
||||
|
||||
query = "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"
|
||||
if method is "timestamp":
|
||||
query += " ORDER BY a.timestamp DESC"
|
||||
elif method is "file_hash":
|
||||
query = "SELECT * FROM updates WHERE {} = '{}'"
|
||||
|
||||
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()
|
56
api/v1/getUpdate.py
Normal file
56
api/v1/getUpdate.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
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 search.*, max(search.file_version) FROM updates search %s GROUP BY filename;",
|
||||
"file_hash": "WHERE %s <= %s",
|
||||
"file_version": "WHERE %s <= %s",
|
||||
"timestamp": "INNER JOIN ( SELECT file_version, file_version FROM updates WHERE %s = '%s' ) find ON search.file_version <= find.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 False in args_filter:
|
||||
raise Exception("Missing required arguments")
|
||||
|
||||
method = args_filter[0]
|
||||
method_value = self.get_argument(method)
|
||||
|
||||
conn = glob.new_sql()
|
||||
cur = conn.cursor()
|
||||
|
||||
sql = SQL_STRUCT["main"] % SQL_STRUCT[method]
|
||||
|
||||
cur.execute(sql % (method, method_value))
|
||||
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)
|
||||
finally:
|
||||
self.write( json.dumps(data) )
|
||||
self.set_header("Content-Type", "application/json")
|
||||
self.set_status(status_code)
|
Loading…
Reference in New Issue
Block a user