feat(api): add deletion of local datasets to API

This commit is contained in:
gmega 2025-02-06 11:47:12 -03:00
parent c71c8dffed
commit 361d101923
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
3 changed files with 61 additions and 1 deletions

View File

@ -238,6 +238,15 @@ proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRoute
let json = await formatManifestBlocks(node)
return RestApiResponse.response($json, contentType = "application/json")
router.api(MethodOptions, "/api/codex/v1/data/{cid}") do(
cid: Cid, resp: HttpResponseRef
) -> RestApiResponse:
if corsOrigin =? allowedOrigin:
resp.setCorsHeaders("GET,DELETE", corsOrigin)
resp.status = Http204
await resp.sendBody("")
router.api(MethodGet, "/api/codex/v1/data/{cid}") do(
cid: Cid, resp: HttpResponseRef
) -> RestApiResponse:
@ -254,6 +263,27 @@ proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRoute
await node.retrieveCid(cid.get(), local = true, resp = resp)
router.api(MethodDelete, "/api/codex/v1/data/{cid}") do(
cid: Cid, resp: HttpResponseRef
) -> RestApiResponse:
## Deletes either a single block or an entire dataset
## from the local node. Does nothing and returns 200
## if the dataset is not locally available.
##
var headers = buildCorsHeaders("DELETE", allowedOrigin)
if corsOrigin =? allowedOrigin:
resp.setCorsHeaders("DELETE", corsOrigin)
resp.setHeader("Access-Control-Headers", "X-Requested-With")
if cid.isErr:
return RestApiResponse.error(Http400, $cid.error(), headers = headers)
if err =? (await node.delete(cid.get())).errorOption:
return RestApiResponse.error(Http500, err.msg, headers = headers)
return RestApiResponse.response("", headers = headers)
router.api(MethodPost, "/api/codex/v1/data/{cid}/network") do(
cid: Cid, resp: HttpResponseRef
) -> RestApiResponse:

View File

@ -83,6 +83,16 @@ proc downloadBytes*(
success bytes
proc delete*(client: CodexClient, cid: Cid): ?!void =
let
url = client.baseurl & "/data/" & $cid
response = client.http.delete(url)
if response.status != "200 OK":
return failure(response.status)
success()
proc list*(client: CodexClient): ?!RestContentList =
let url = client.baseurl & "/data"
let response = client.http.get(url)
@ -281,3 +291,6 @@ proc downloadRaw*(client: CodexClient, cid: string, local = false): Response =
client.baseurl & "/data/" & cid & (if local: "" else: "/network/stream"),
httpMethod = HttpGet,
)
proc deleteRaw*(client: CodexClient, cid: string): Response =
return client.http.request(client.baseurl & "/data/" & cid, httpMethod = HttpDelete)

View File

@ -1,9 +1,10 @@
import std/httpclient
import std/sequtils
from pkg/libp2p import `==`
from pkg/libp2p import `==`, `$`, Cid
import pkg/codex/units
import ./twonodes
import ../examples
import ../codex/examples
import json
twonodessuite "REST API":
@ -261,3 +262,19 @@ twonodessuite "REST API":
check localResponse.headers.hasKey("Content-Disposition") == true
check localResponse.headers["Content-Disposition"] ==
"attachment; filename=\"example.txt\""
test "should delete a dataset when requested", twoNodesConfig:
let cid = client1.upload("some file contents").get
var response = client1.downloadRaw($cid, local = true)
check response.body == "some file contents"
client1.delete(cid).get
response = client1.downloadRaw($cid, local = true)
check response.status == "404 Not Found"
test "should return 200 when attempting delete of non-existing dataset",
twoNodesConfig:
let response = client1.deleteRaw($(Cid.example()))
check response.status == "200 OK"