[sales] REST endpoint that lists available storage

This commit is contained in:
Mark Spanbroek 2022-05-09 16:51:08 +02:00 committed by markspanbroek
parent 1fbb91ddc6
commit 11581fd12c
2 changed files with 32 additions and 14 deletions

View File

@ -332,6 +332,17 @@ proc initRestApi*(node: DaggerNodeRef, conf: DaggerConf): RestRouter =
"\nAddrs: \n" & addrs & "\nAddrs: \n" & addrs &
"\nRoot Dir: " & $conf.dataDir) "\nRoot Dir: " & $conf.dataDir)
router.api(
MethodGet,
"/api/dagger/v1/sales/availability") do () -> RestApiResponse:
## Returns storage that is for sale
without contracts =? node.contracts:
return RestApiResponse.error(Http503, "Sales unavailable")
let json = %contracts.sales.available
return RestApiResponse.response($json)
router.rawApi( router.rawApi(
MethodPost, MethodPost,
"/api/dagger/v1/sales/availability") do () -> RestApiResponse: "/api/dagger/v1/sales/availability") do () -> RestApiResponse:
@ -341,15 +352,17 @@ proc initRestApi*(node: DaggerNodeRef, conf: DaggerConf): RestRouter =
## duration - maximum time the storage should be sold for (in seconds) ## duration - maximum time the storage should be sold for (in seconds)
## minPrice - minimum price to be paid (in amount of tokens) ## minPrice - minimum price to be paid (in amount of tokens)
without contracts =? node.contracts:
return RestApiResponse.error(Http503, "Sales unavailable")
let body = await request.getBody() let body = await request.getBody()
without availability =? Availability.fromJson(body), error: without availability =? Availability.fromJson(body), error:
return RestApiResponse.error(Http400, error.msg) return RestApiResponse.error(Http400, error.msg)
without contracts =? node.contracts:
return RestApiResponse.error(Http503, "Sales unavailable")
contracts.sales.add(availability) contracts.sales.add(availability)
return RestApiResponse.response(availability.id.toHex)
let json = %availability
return RestApiResponse.response($json)
return router return router

View File

@ -12,6 +12,7 @@ suite "Integration tests":
let workingDir = currentSourcePath() / ".." / ".." let workingDir = currentSourcePath() / ".." / ".."
var node1, node2: Process var node1, node2: Process
var baseurl1, baseurl2: string
var client: HttpClient var client: HttpClient
proc startNode(args: openArray[string]): Process = proc startNode(args: openArray[string]): Process =
@ -28,6 +29,8 @@ suite "Integration tests":
setup: setup:
node1 = startNode ["--api-port=8080", "--udp-port=8090"] node1 = startNode ["--api-port=8080", "--udp-port=8090"]
node2 = startNode ["--api-port=8081", "--udp-port=8091"] node2 = startNode ["--api-port=8081", "--udp-port=8091"]
baseurl1 = "http://localhost:8080/api/dagger/v1"
baseurl2 = "http://localhost:8081/api/dagger/v1"
client = newHttpClient() client = newHttpClient()
teardown: teardown:
@ -36,17 +39,19 @@ suite "Integration tests":
node2.stop() node2.stop()
test "nodes can print their peer information": test "nodes can print their peer information":
let info1 = client.get("http://localhost:8080/api/dagger/v1/info").body let info1 = client.get(baseurl1 & "/info").body
let info2 = client.get("http://localhost:8081/api/dagger/v1/info").body let info2 = client.get(baseurl2 & "/info").body
check info1 != info2 check info1 != info2
test "node handles new storage availability": test "node handles new storage availability":
let baseurl = "http://localhost:8080/api/dagger/v1" let url = baseurl1 & "/sales/availability"
let url = baseurl & "/sales/availability" let json = %*{"size": "0x1", "duration": "0x2", "minPrice": "0x3"}
let json = %*{ check client.post(url, $json).status == "200 OK"
"size": "0x1",
"duration": "0x2", test "node lists storage that is for sale":
"minPrice": "0x3" let url = baseurl1 & "/sales/availability"
} let json = %*{"size": "0x1", "duration": "0x2", "minPrice": "0x3"}
let response = client.post(url, $json) let availability = parseJson(client.post(url, $json).body)
let response = client.get(url)
check response.status == "200 OK" check response.status == "200 OK"
check parseJson(response.body) == %*[availability]