From e38883578ef9e12e499a1237f25218f186068866 Mon Sep 17 00:00:00 2001 From: csasq Date: Tue, 27 Aug 2024 17:30:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D0=B0=20=D0=BA=D0=BE=D0=BD=D0=B5=D1=87=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=82=D0=BE=D1=87=D0=BA=D0=B0=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20=D1=81=D1=86=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=D1=80=D0=B8=D0=B5=D0=B2,=20=D1=80=D0=B5=D1=88?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB=D0=B5=D0=BC?= =?UTF-8?q?=D0=B0=20=D1=81=20CORS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/main.py | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) 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(