Skip to content

Commit

Permalink
feat(api): put downloads endpoint for re-attempting download
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Shatford <[email protected]>
  • Loading branch information
jordanshatford committed Sep 13, 2023
1 parent 3977c0c commit 789e32a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=['*'],
)

Expand Down
7 changes: 7 additions & 0 deletions apps/api/app/routers/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
47 changes: 47 additions & 0 deletions apps/api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions apps/web/src/lib/stores/downloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -103,6 +120,7 @@ function createDownloadsStore() {
subscribe,
setupStatusListener,
add,
restart,
remove,
getFile,
reset: () => set({})
Expand Down
5 changes: 1 addition & 4 deletions apps/web/src/routes/downloads/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@
{/if}
{#if [DownloadState.ERROR].includes(row.status.state)}
<IconButton
on:click={async () => {
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"
/>
Expand Down
21 changes: 21 additions & 0 deletions packages/client/src/generated/services/DownloadsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ export class DownloadsService {
});
}

/**
* Put Downloads
* @param requestBody
* @returns VideoWithOptions Successful Response
* @throws ApiError
*/
public static putDownloads(
requestBody: VideoWithOptions,
): CancelablePromise<VideoWithOptions> {
return __request(OpenAPI, {
method: 'PUT',
url: '/downloads',
body: requestBody,
mediaType: 'application/json',
errors: {
403: `Forbidden`,
422: `Validation Error`,
},
});
}

/**
* Post Downloads
* @param requestBody
Expand Down

0 comments on commit 789e32a

Please sign in to comment.