SQLite
This commit is contained in:
@@ -1,12 +1,57 @@
|
||||
import json
|
||||
|
||||
import tornado.gen
|
||||
import tornado.web
|
||||
|
||||
from web import asyncTornado
|
||||
from constants import argumentTypes
|
||||
|
||||
from objects import glob
|
||||
|
||||
allowed_args = ["file_hash", "file_version", "timestamp"]
|
||||
ARGS = {
|
||||
("file_hash", "file_version", "timestamp"): argumentTypes.one_required
|
||||
}
|
||||
|
||||
def handle(requestsManager.asyncRequestHandler):
|
||||
return {}
|
||||
SQL_STRUCT = {
|
||||
"main": "SELECT * FROM updates WHERE %s LIMIT 1",
|
||||
"file_hash": "%s = '%s'",
|
||||
"file_version": "%s = %s",
|
||||
"timestamp": "timestamp <= '%s' ORDER BY timestamp DESC"
|
||||
}
|
||||
|
||||
def callback(method, data):
|
||||
return None
|
||||
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.request.arguments[method]
|
||||
|
||||
cur = glob.sql.cursor()
|
||||
|
||||
sql = SQL_STRUCT["main"] % SQL_STRUCT["method"]
|
||||
if method == "timestamp":
|
||||
sql = sql % method_value
|
||||
else:
|
||||
sql = sql % (method, method_value)
|
||||
|
||||
cur.execute(sql)
|
||||
data = cur.fetchone()
|
||||
|
||||
status_code = 200
|
||||
except Exception as e:
|
||||
status_code = 400
|
||||
data["status"] = status_code
|
||||
data["message"] = e
|
||||
finally:
|
||||
cur.close()
|
||||
|
||||
self.write( json.dumps(data) )
|
||||
self.set_header("Content-Type", "application/json")
|
||||
self.set_status(status_code)
|
||||
|
||||
Reference in New Issue
Block a user