From 789e32a27552796b153ec6462643029ec74589be Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Wed, 13 Sep 2023 15:51:03 +1000 Subject: [PATCH] feat(api): put downloads endpoint for re-attempting download Signed-off-by: Jordan Shatford --- apps/api/app/main.py | 2 +- apps/api/app/routers/downloads.py | 7 +++ apps/api/openapi.json | 47 +++++++++++++++++++ apps/web/src/lib/stores/downloads.ts | 18 +++++++ apps/web/src/routes/downloads/+page.svelte | 5 +- .../generated/services/DownloadsService.ts | 21 +++++++++ 6 files changed, 95 insertions(+), 5 deletions(-) diff --git a/apps/api/app/main.py b/apps/api/app/main.py index 3cc81668..64f73fb9 100644 --- a/apps/api/app/main.py +++ b/apps/api/app/main.py @@ -54,7 +54,7 @@ def generate_custom_unique_id(route: routing.APIRoute): CORSMiddleware, allow_origins=[allowed_origin], allow_credentials=True, - allow_methods=['POST', 'GET', 'DELETE'], + allow_methods=['POST', 'PUT', 'GET', 'DELETE'], allow_headers=['*'], ) diff --git a/apps/api/app/routers/downloads.py b/apps/api/app/routers/downloads.py index ca3cf0b5..d508b83e 100644 --- a/apps/api/app/routers/downloads.py +++ b/apps/api/app/routers/downloads.py @@ -39,6 +39,13 @@ def post_downloads( return video +@router.put('') +def put_downloads(video: VideoWithOptions, session: DependsSession) -> VideoWithOptions: + session.download_manager.remove(video.id) + session.download_manager.add(video) + return video + + async def status_stream(request: Request, session: Session): try: while True: diff --git a/apps/api/openapi.json b/apps/api/openapi.json index 635172ba..3cc31825 100644 --- a/apps/api/openapi.json +++ b/apps/api/openapi.json @@ -154,6 +154,53 @@ } ] }, + "put": { + "tags": [ + "downloads" + ], + "summary": "Put Downloads", + "operationId": "put_downloads", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/VideoWithOptions" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/VideoWithOptions" + } + } + } + }, + "403": { + "description": "Forbidden" + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + }, + "security": [ + { + "HTTPBearer": [] + } + ] + }, "post": { "tags": [ "downloads" diff --git a/apps/web/src/lib/stores/downloads.ts b/apps/web/src/lib/stores/downloads.ts index e425cd47..dd54a72f 100644 --- a/apps/web/src/lib/stores/downloads.ts +++ b/apps/web/src/lib/stores/downloads.ts @@ -58,6 +58,23 @@ function createDownloadsStore() { } } + async function restart(id: string) { + if (!(id in downloads)) return; + + const info = downloads[id]; + + updateDownload(info.id, info); + try { + const result = await DownloadsService.putDownloads(info); + toast.info(`Restarted download of '${info.title}'.`); + updateDownload(result.id, result); + } catch (err) { + toast.error(`Failed to restart '${info.title}' download.`); + console.error('Failed to restart download ', err); + updateDownload(info.id, { status: { state: DownloadState.ERROR } }); + } + } + async function remove(id: string) { if (!(id in downloads)) return; @@ -103,6 +120,7 @@ function createDownloadsStore() { subscribe, setupStatusListener, add, + restart, remove, getFile, reset: () => set({}) diff --git a/apps/web/src/routes/downloads/+page.svelte b/apps/web/src/routes/downloads/+page.svelte index 7974299b..9892b081 100644 --- a/apps/web/src/routes/downloads/+page.svelte +++ b/apps/web/src/routes/downloads/+page.svelte @@ -79,10 +79,7 @@ {/if} {#if [DownloadState.ERROR].includes(row.status.state)} { - await downloads.remove(row.id); - await downloads.add(row); - }} + on:click={async () => await downloads.restart(row.id)} src={RotateIcon} class="h-10 w-10 hover:text-indigo-800 dark:hover:text-indigo-600" /> diff --git a/packages/client/src/generated/services/DownloadsService.ts b/packages/client/src/generated/services/DownloadsService.ts index 73c9aaa8..0cc5aff9 100644 --- a/packages/client/src/generated/services/DownloadsService.ts +++ b/packages/client/src/generated/services/DownloadsService.ts @@ -27,6 +27,27 @@ export class DownloadsService { }); } + /** + * Put Downloads + * @param requestBody + * @returns VideoWithOptions Successful Response + * @throws ApiError + */ + public static putDownloads( + requestBody: VideoWithOptions, + ): CancelablePromise { + return __request(OpenAPI, { + method: 'PUT', + url: '/downloads', + body: requestBody, + mediaType: 'application/json', + errors: { + 403: `Forbidden`, + 422: `Validation Error`, + }, + }); + } + /** * Post Downloads * @param requestBody