fix: db revamp, add relations, annotate the db
also allows for request retrieval, and adds decorator for authed access
This commit is contained in:
@@ -2,19 +2,40 @@
|
||||
# - HTTP status code,
|
||||
# - human-readable status message,
|
||||
# - json with appropriate data
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from functools import wraps
|
||||
from lewy_globals import getDb, colors as c
|
||||
import flask, json, time
|
||||
import lewy_db
|
||||
import lewy_globals
|
||||
|
||||
def incrementBadRequests():
|
||||
def require_authentication(func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
token = kwargs["r"].args.get('token')
|
||||
if token == lewy_globals.config['api']['api_key']:
|
||||
try:
|
||||
status, received, data = func(*args, **kwargs)
|
||||
return status, received, data
|
||||
except:
|
||||
raise AssertionError(f"Function \"{func.__name__}\" does not return status, code, and data as it should!")
|
||||
else:
|
||||
return 401, "error", {'error_msg': "Unauthorized"}
|
||||
return wrapper
|
||||
|
||||
def increment_bad_requests():
|
||||
lewy_globals.apiFailedRequests += 1
|
||||
|
||||
def notImplemented(data):
|
||||
def not_implemented(data):
|
||||
# TODO: change list to string -> data, not data[0]
|
||||
return 501, f"not recognised/implemented: {data[0]}", []
|
||||
|
||||
def stub_hello():
|
||||
return 200, 'hello from v1! stats are at /api/v1/stats', []
|
||||
|
||||
def epochToDate(epoch):
|
||||
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(epoch))
|
||||
|
||||
def stats():
|
||||
data_to_send = {
|
||||
"start_time": lewy_globals.starttime,
|
||||
@@ -25,11 +46,18 @@ def stats():
|
||||
"outside_api_requests": lewy_globals.outsideApiHits,
|
||||
"local_api_requests": lewy_globals.apiRequests - lewy_globals.outsideApiHits
|
||||
}
|
||||
return 200, "OK", data_to_send
|
||||
return 200, "ok", data_to_send
|
||||
|
||||
def get_matches():
|
||||
pass
|
||||
|
||||
@require_authentication
|
||||
def debugger_halt(r):
|
||||
print(f"{c.WARNING}[{epochToDate(time.time())}]{c.ENDC} {r.remote_addr} triggered a debugger halt!")
|
||||
breakpoint()
|
||||
return 200, "ok", []
|
||||
|
||||
def lookup(data):
|
||||
def lookup(data, request):
|
||||
if data == []:
|
||||
return stub_hello()
|
||||
match data[0].lower():
|
||||
@@ -39,6 +67,8 @@ def lookup(data):
|
||||
return stub_hello()
|
||||
case 'info':
|
||||
return stub_hello()
|
||||
case 'halt':
|
||||
return debugger_halt(r = request)
|
||||
case _:
|
||||
incrementBadRequests()
|
||||
return notImplemented(data)
|
||||
increment_bad_requests()
|
||||
return not_implemented(data)
|
||||
Reference in New Issue
Block a user