Endpoints changed but still not working

This commit is contained in:
Ben Arc
2021-10-12 22:34:43 +01:00
parent aeee469c64
commit 1346ad12f1
9 changed files with 181 additions and 203 deletions
+27 -25
View File
@@ -1,8 +1,8 @@
from http import HTTPStatus
import httpx
from collections import defaultdict
from lnbits.decorators import check_user_exists, validate_uuids
from lnbits.decorators import check_user_exists
import asyncio
from .crud import get_copilot
from functools import wraps
@@ -10,13 +10,15 @@ from functools import wraps
from lnbits.decorators import check_user_exists
from . import copilot_ext, copilot_renderer
from fastapi import FastAPI, Request
from fastapi import FastAPI, Request, WebSocket
from fastapi.params import Depends
from fastapi.templating import Jinja2Templates
from fastapi.param_functions import Query
from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse
from starlette.responses import HTMLResponse, JSONResponse # type: ignore
from lnbits.core.models import User
import base64
templates = Jinja2Templates(directory="templates")
@@ -50,25 +52,25 @@ async def panel(request: Request):
connected_websockets = defaultdict(set)
@copilot_ext.websocket("/ws/<id>/")
async def wss(id):
copilot = await get_copilot(id)
if not copilot:
return "", HTTPStatus.FORBIDDEN
global connected_websockets
send_channel, receive_channel = trio.open_memory_channel(0)
connected_websockets[id].add(send_channel)
try:
while True:
data = await receive_channel.receive()
await websocket.send(data)
finally:
connected_websockets[id].remove(send_channel)
# @copilot_ext.websocket("/ws/{id}/")
# async def websocket_endpoint(websocket: WebSocket, id: str = Query(None)):
# copilot = await get_copilot(id)
# if not copilot:
# return "", HTTPStatus.FORBIDDEN
# await websocket.accept()
# invoice_queue = asyncio.Queue()
# connected_websockets[id].add(invoice_queue)
# try:
# while True:
# data = await websocket.receive_text()
# await websocket.send_text(f"Message text was: {data}")
# finally:
# connected_websockets[id].remove(invoice_queue)
async def updater(copilot_id, data, comment):
copilot = await get_copilot(copilot_id)
if not copilot:
return
for queue in connected_websockets[copilot_id]:
await queue.send(f"{data + '-' + comment}")
# async def updater(copilot_id, data, comment):
# copilot = await get_copilot(copilot_id)
# if not copilot:
# return
# for queue in connected_websockets[copilot_id]:
# await queue.send(f"{data + '-' + comment}")