Implement API magic
This commit is contained in:
parent
125ecfd437
commit
7824ca8c33
6
main.py
6
main.py
@ -40,9 +40,10 @@ async def handler(request: aiohttp.web.Request):
|
|||||||
return aiohttp.web.FileResponse('ui.html')
|
return aiohttp.web.FileResponse('ui.html')
|
||||||
|
|
||||||
|
|
||||||
@routes.post(CLIENT_REGEX + '/api')
|
@routes.post(CLIENT_REGEX + '/api/{method}')
|
||||||
async def handler(request: aiohttp.web.Request):
|
async def handler(request: aiohttp.web.Request):
|
||||||
client = get_client(request)
|
client = get_client(request)
|
||||||
|
method = request.match_info.get('method', None)
|
||||||
model = request.app['model']
|
model = request.app['model']
|
||||||
|
|
||||||
data = await request.json()
|
data = await request.json()
|
||||||
@ -50,7 +51,8 @@ async def handler(request: aiohttp.web.Request):
|
|||||||
print(f'{client=} {data=}')
|
print(f'{client=} {data=}')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
assert await model.handle_post(client, data)
|
assert method in model.ApiMethod.dict
|
||||||
|
await model.ApiMethod.dict[method](model, client, **data)
|
||||||
return aiohttp.web.Response(status=200)
|
return aiohttp.web.Response(status=200)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
29
model.py
29
model.py
@ -13,18 +13,17 @@ def generate_random_id(_s=set()):
|
|||||||
|
|
||||||
|
|
||||||
class Model(object):
|
class Model(object):
|
||||||
|
class ApiMethod:
|
||||||
|
dict = dict()
|
||||||
|
|
||||||
global api_methods
|
def __init__(self, fun):
|
||||||
api_methods = {}
|
self.dict[fun.__name__] = fun
|
||||||
|
|
||||||
def api_method():
|
def __contains__(self, item):
|
||||||
global api_methods
|
return item in self.dict
|
||||||
def wrapper(fun):
|
|
||||||
api_methods[fun.__name__] = fun
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
def __init__(self, filename = "tehmodel.json"):
|
def __init__(self, filename="tehmodel.json"):
|
||||||
self.sockets = {} # mapping: client -> socket
|
self.sockets = {} # mapping: client -> socket
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.model = None
|
self.model = None
|
||||||
if os.path.isfile(filename):
|
if os.path.isfile(filename):
|
||||||
@ -43,13 +42,21 @@ class Model(object):
|
|||||||
if not "sessions" in self.model:
|
if not "sessions" in self.model:
|
||||||
self.model["sessions"] = {}
|
self.model["sessions"] = {}
|
||||||
|
|
||||||
@api_method()
|
@ApiMethod
|
||||||
|
async def test_api(self, clientid):
|
||||||
|
print(f'test_api {clientid=}')
|
||||||
|
|
||||||
|
@ApiMethod
|
||||||
|
async def test_yeet(self, clientid):
|
||||||
|
raise Exception('yeet')
|
||||||
|
|
||||||
|
@ApiMethod
|
||||||
async def create_session(self, clientid) -> str:
|
async def create_session(self, clientid) -> str:
|
||||||
sessionname = generate_random_id()
|
sessionname = generate_random_id()
|
||||||
newsession = {"id": sessionname, "owner": clientid, "clients": []}
|
newsession = {"id": sessionname, "owner": clientid, "clients": []}
|
||||||
self.model["sessions"][sessionname] = newsession
|
self.model["sessions"][sessionname] = newsession
|
||||||
|
|
||||||
@api_method()
|
@ApiMethod
|
||||||
async def join_session(self, clientid, sessionid):
|
async def join_session(self, clientid, sessionid):
|
||||||
if sessionid in self.model["sessions"]:
|
if sessionid in self.model["sessions"]:
|
||||||
self.model["sessions"][sessionid].append(clientid)
|
self.model["sessions"][sessionid].append(clientid)
|
||||||
|
Loading…
Reference in New Issue
Block a user