diff --git a/api/main.py b/api/main.py index 7eb59e8..763d8d5 100644 --- a/api/main.py +++ b/api/main.py @@ -1,7 +1,8 @@ import asyncio -from fastapi import FastAPI, Body +from fastapi import FastAPI, Response, Body from fastapi.websockets import WebSocket, WebSocketDisconnect +from fastapi.responses import JSONResponse import config @@ -63,20 +64,54 @@ scripts = [ scripts_cm = ConnectionManager() -@api.post( +# TODO(Development-only headers) +cors = { + 'Access-Control-Allow-Methods': '*', + 'Access-Control-Allow-Headers': '*', + 'Access-Control-Allow-Origin': '*', +} + + +@api.options( + path='/api/scripts', +) +async def _(): + return Response( + headers=cors, + ) + + +@api.get( + path='/api/scripts', +) +async def _(): + return JSONResponse( + content=scripts, + status_code=200, + headers=cors, + ) + + +@api.put( path='/api/scripts', ) async def _( script_id: int = Body( + alias='id', validation_alias='id', + serialization_alias='id', ), name: str = Body(), time: str = Body(), message_number: str = Body( + alias='messageNumber', validation_alias='messageNumber', + serialization_alias='messageNumber', ), is_enabled: bool = Body( + alias='isEnabled', validation_alias='isEnabled', + serialization_alias='isEnabled', ), ): i = script_id - 1 @@ -84,6 +119,10 @@ async def _( scripts[i]['time'] = time scripts[i]['messageNumber'] = message_number scripts[i]['isEnabled'] = is_enabled + return Response( + status_code=201, + headers=cors, + ) @api.websocket(