From e9c55a67d3bb32b54173025d68eab4af4b0f02de Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Sun, 5 May 2024 19:04:18 +0400 Subject: [PATCH] JSON schema v0.0.2 --- backend/main.py | 4 ++-- backend/response.py | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/backend/main.py b/backend/main.py index 7f6879c..2992a1a 100644 --- a/backend/main.py +++ b/backend/main.py @@ -25,7 +25,7 @@ async def handler(socket: SocketT) -> None: match data['action']: case 'list': # list tracks in album - await socket.send(response.ok_playlist( + await socket.send(response.playlist( await ydl_wrap.get_playlist_items( ydls.get_ydl(data['site']), data['url'], @@ -38,7 +38,7 @@ async def handler(socket: SocketT) -> None: data['url'], data.get('items'), ) - await socket.send(response.ok_downloaded(ret)) + await socket.send(response.dl_end(ret)) # TODO: cancellation diff --git a/backend/response.py b/backend/response.py index 47e0c4b..dc9f997 100644 --- a/backend/response.py +++ b/backend/response.py @@ -1,25 +1,29 @@ import json import traceback -OK = '{"ok":true}' - -def ok_playlist(items: list[str]) -> str: +def playlist(items: list[str]) -> str: return json.dumps({ - "ok": True, + "type": "items", "data": items, }) -def ok_downloaded(ret: int) -> str: +def dl_progress(msg: str) -> str: return json.dumps({ - "type": "downloaded", + "type": "dl_progress", + "data": msg, + }) + +def dl_end(ret: int) -> str: + return json.dumps({ + "type": "dl_end", "data": ret, }) def error(ex: Exception) -> str: traceback.print_tb(ex.__traceback__) return json.dumps({ - "ok": False, - "error": { + "type": "error", + "data": { "type": ex.__class__.__qualname__, "message": str(ex), }