[purchasing] retrieve purchase details through REST API

This commit is contained in:
Mark Spanbroek 2022-05-11 10:51:59 +02:00 committed by markspanbroek
parent fb56f60d77
commit b31ef8356b
3 changed files with 54 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import pkg/chronos
import pkg/presto
import pkg/libp2p
import pkg/stew/base10
import pkg/stew/byteutils
import pkg/confutils
import pkg/libp2p/routing_record
@ -94,6 +95,13 @@ proc decodeString(_: type UInt256, value: string): Result[UInt256, cstring] =
except ValueError as e:
err e.msg.cstring
proc decodeString(_: type array[32, byte],
value: string): Result[array[32, byte], cstring] =
try:
ok array[32, byte].fromHex(value)
except ValueError as e:
err e.msg.cstring
proc initRestApi*(node: DaggerNodeRef, conf: DaggerConf): RestRouter =
var router = RestRouter.init(validate)
router.api(
@ -310,4 +318,23 @@ proc initRestApi*(node: DaggerNodeRef, conf: DaggerConf): RestRouter =
let json = %availability
return RestApiResponse.response($json)
router.api(
MethodGet,
"/api/dagger/v1/storage/purchases/{id}") do (
id: array[32, byte]) -> RestApiResponse:
without contracts =? node.contracts:
return RestApiResponse.error(Http503, "Purchasing unavailable")
without id =? id.tryGet.catch, error:
return RestApiResponse.error(Http400, error.msg)
without purchase =? contracts.purchasing.getPurchase(id):
return RestApiResponse.error(Http404)
let json = %purchase
return RestApiResponse.response($json)
return router

View File

@ -3,6 +3,7 @@ import std/strutils
import pkg/stew/byteutils
import pkg/questionable/results
import ../sales
import ../purchasing
type
StorageRequestParams* = object
@ -22,3 +23,19 @@ proc fromJson*(_: type StorageRequestParams,
let duration = ?catch UInt256.fromHex(json["duration"].getStr)
let maxPrice = ?catch UInt256.fromHex(json["maxPrice"].getStr)
success StorageRequestParams(duration: duration, maxPrice: maxPrice)
func `%`*(address: Address): JsonNode =
% $address
func `%`*(stint: StInt|StUInt): JsonNode =
%("0x" & stint.toHex)
func `%`*(arr: openArray[byte]): JsonNode =
%("0x" & arr.toHex)
func `%`*(purchase: Purchase): JsonNode =
%*{
"request": %purchase.request,
"offers": %purchase.offers,
"selected": %purchase.selected
}

View File

@ -68,3 +68,13 @@ suite "Integration tests":
let json = %*{"duration": "0x1", "maxPrice": "0x2"}
let response = client.post(url, $json)
check response.status == "200 OK"
test "node retrieves purchase status":
let cid = client.post(baseurl1 & "/upload", "some file contents").body
let request = %*{"duration": "0x1", "maxPrice": "0x2"}
let id = client.post(baseurl1 & "/storage/request/" & cid, $request).body
let response = client.get(baseurl1 & "/storage/purchases/" & id)
check response.status == "200 OK"
let json = parseJson(response.body)
check json["request"]["ask"]["duration"].getStr == "0x1"
check json["request"]["ask"]["maxPrice"].getStr == "0x2"