Разработана конечная точка для получения списка сценариев, решена проблема с CORS

Этот коммит содержится в:
Глеб Иваницкий 2024-08-27 17:30:48 +03:00
родитель b6661d3ebd
Коммит e38883578e

Просмотреть файл

@ -1,7 +1,8 @@
import asyncio import asyncio
from fastapi import FastAPI, Body from fastapi import FastAPI, Response, Body
from fastapi.websockets import WebSocket, WebSocketDisconnect from fastapi.websockets import WebSocket, WebSocketDisconnect
from fastapi.responses import JSONResponse
import config import config
@ -63,20 +64,54 @@ scripts = [
scripts_cm = ConnectionManager() 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', path='/api/scripts',
) )
async def _( async def _(
script_id: int = Body( script_id: int = Body(
alias='id',
validation_alias='id', validation_alias='id',
serialization_alias='id',
), ),
name: str = Body(), name: str = Body(),
time: str = Body(), time: str = Body(),
message_number: str = Body( message_number: str = Body(
alias='messageNumber',
validation_alias='messageNumber', validation_alias='messageNumber',
serialization_alias='messageNumber',
), ),
is_enabled: bool = Body( is_enabled: bool = Body(
alias='isEnabled',
validation_alias='isEnabled', validation_alias='isEnabled',
serialization_alias='isEnabled',
), ),
): ):
i = script_id - 1 i = script_id - 1
@ -84,6 +119,10 @@ async def _(
scripts[i]['time'] = time scripts[i]['time'] = time
scripts[i]['messageNumber'] = message_number scripts[i]['messageNumber'] = message_number
scripts[i]['isEnabled'] = is_enabled scripts[i]['isEnabled'] = is_enabled
return Response(
status_code=201,
headers=cors,
)
@api.websocket( @api.websocket(