[sales] REST endpoint that lists available storage
This commit is contained in:
parent
1fbb91ddc6
commit
11581fd12c
|
@ -332,6 +332,17 @@ proc initRestApi*(node: DaggerNodeRef, conf: DaggerConf): RestRouter =
|
|||
"\nAddrs: \n" & addrs &
|
||||
"\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(
|
||||
MethodPost,
|
||||
"/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)
|
||||
## 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()
|
||||
|
||||
without availability =? Availability.fromJson(body), error:
|
||||
return RestApiResponse.error(Http400, error.msg)
|
||||
|
||||
without contracts =? node.contracts:
|
||||
return RestApiResponse.error(Http503, "Sales unavailable")
|
||||
|
||||
contracts.sales.add(availability)
|
||||
return RestApiResponse.response(availability.id.toHex)
|
||||
|
||||
let json = %availability
|
||||
return RestApiResponse.response($json)
|
||||
|
||||
return router
|
||||
|
|
|
@ -12,6 +12,7 @@ suite "Integration tests":
|
|||
let workingDir = currentSourcePath() / ".." / ".."
|
||||
|
||||
var node1, node2: Process
|
||||
var baseurl1, baseurl2: string
|
||||
var client: HttpClient
|
||||
|
||||
proc startNode(args: openArray[string]): Process =
|
||||
|
@ -28,6 +29,8 @@ suite "Integration tests":
|
|||
setup:
|
||||
node1 = startNode ["--api-port=8080", "--udp-port=8090"]
|
||||
node2 = startNode ["--api-port=8081", "--udp-port=8091"]
|
||||
baseurl1 = "http://localhost:8080/api/dagger/v1"
|
||||
baseurl2 = "http://localhost:8081/api/dagger/v1"
|
||||
client = newHttpClient()
|
||||
|
||||
teardown:
|
||||
|
@ -36,17 +39,19 @@ suite "Integration tests":
|
|||
node2.stop()
|
||||
|
||||
test "nodes can print their peer information":
|
||||
let info1 = client.get("http://localhost:8080/api/dagger/v1/info").body
|
||||
let info2 = client.get("http://localhost:8081/api/dagger/v1/info").body
|
||||
let info1 = client.get(baseurl1 & "/info").body
|
||||
let info2 = client.get(baseurl2 & "/info").body
|
||||
check info1 != info2
|
||||
|
||||
test "node handles new storage availability":
|
||||
let baseurl = "http://localhost:8080/api/dagger/v1"
|
||||
let url = baseurl & "/sales/availability"
|
||||
let json = %*{
|
||||
"size": "0x1",
|
||||
"duration": "0x2",
|
||||
"minPrice": "0x3"
|
||||
}
|
||||
let response = client.post(url, $json)
|
||||
let url = baseurl1 & "/sales/availability"
|
||||
let json = %*{"size": "0x1", "duration": "0x2", "minPrice": "0x3"}
|
||||
check client.post(url, $json).status == "200 OK"
|
||||
|
||||
test "node lists storage that is for sale":
|
||||
let url = baseurl1 & "/sales/availability"
|
||||
let json = %*{"size": "0x1", "duration": "0x2", "minPrice": "0x3"}
|
||||
let availability = parseJson(client.post(url, $json).body)
|
||||
let response = client.get(url)
|
||||
check response.status == "200 OK"
|
||||
check parseJson(response.body) == %*[availability]
|
||||
|
|
Loading…
Reference in New Issue