From 703921df322e2c32b4f12786fc48e30989b025ca Mon Sep 17 00:00:00 2001 From: Arnaud Date: Mon, 10 Mar 2025 16:59:24 +0100 Subject: [PATCH] chore(restapi): add headers to support on progress when downloading (#1150) * Add headers to support on progress on download * Replace http session by http client in downloadBytes * Use int instead of int64 for datasetSize * Rename variable to avoid shallowing client --- codex/rest/api.nim | 3 +++ tests/integration/codexclient.nim | 12 ++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/codex/rest/api.nim b/codex/rest/api.nim index 6b8f2ac1..7cb0b43f 100644 --- a/codex/rest/api.nim +++ b/codex/rest/api.nim @@ -114,6 +114,8 @@ proc retrieveCid( else: resp.setHeader("Content-Disposition", "attachment") + resp.setHeader("Content-Length", $manifest.datasetSize.int) + await resp.prepareChunked() while not stream.atEof: @@ -342,6 +344,7 @@ proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRoute resp.setCorsHeaders("GET", corsOrigin) resp.setHeader("Access-Control-Headers", "X-Requested-With") + resp.setHeader("Access-Control-Expose-Headers", "Content-Disposition") await node.retrieveCid(cid.get(), local = false, resp = resp) router.api(MethodGet, "/api/codex/v1/data/{cid}/network/manifest") do( diff --git a/tests/integration/codexclient.nim b/tests/integration/codexclient.nim index 287f465f..f4c3f977 100644 --- a/tests/integration/codexclient.nim +++ b/tests/integration/codexclient.nim @@ -76,15 +76,15 @@ proc downloadNoStream*(client: CodexClient, cid: Cid): ?!string = proc downloadBytes*( client: CodexClient, cid: Cid, local = false ): Future[?!seq[byte]] {.async.} = - let uri = - parseUri(client.baseurl & "/data/" & $cid & (if local: "" else: "/network/stream")) + let uri = client.baseurl & "/data/" & $cid & (if local: "" else: "/network/stream") - let (status, bytes) = await client.session.fetch(uri) + let httpClient = newHttpClient() + let response = httpClient.get(uri) - if status != 200: - return failure("fetch failed with status " & $status) + if response.status != "200 OK": + return failure("fetch failed with status " & $response.status) - success bytes + success response.body.toBytes proc delete*(client: CodexClient, cid: Cid): ?!void = let