[sales] Perform POST with JSON body instead of URL params

This commit is contained in:
Mark Spanbroek 2022-05-09 15:15:23 +02:00 committed by markspanbroek
parent a43e3d6d94
commit ef80871984
3 changed files with 32 additions and 16 deletions

View File

@ -30,6 +30,8 @@ import ../blocktype
import ../conf import ../conf
import ../contracts import ../contracts
import ./json
proc validate( proc validate(
pattern: string, pattern: string,
value: string): int value: string): int
@ -330,31 +332,23 @@ proc initRestApi*(node: DaggerNodeRef, conf: DaggerConf): RestRouter =
"\nAddrs: \n" & addrs & "\nAddrs: \n" & addrs &
"\nRoot Dir: " & $conf.dataDir) "\nRoot Dir: " & $conf.dataDir)
router.api( router.rawApi(
MethodGet, MethodPost,
"/api/dagger/v1/sales/availability") do ( "/api/dagger/v1/sales/availability") do () -> RestApiResponse:
size: Option[uint64],
duration: Option[uint64],
minPrice: Option[UInt256]) -> RestApiResponse:
## Add available storage to sell ## Add available storage to sell
## ##
## size - size of available storage in bytes ## size - size of available storage in bytes
## 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 size =? size.?get(): let body = await request.getBody()
return RestApiResponse.error(Http400, "Missing or incorrect size")
without duration =? duration.?get(): without availability =? Availability.fromJson(body), error:
return RestApiResponse.error(Http400, "Missing or incorrect duration") return RestApiResponse.error(Http400, error.msg)
without minPrice =? minPrice.?get():
return RestApiResponse.error(Http400, "Missing or incorrect minPrice")
without contracts =? node.contracts: without contracts =? node.contracts:
return RestApiResponse.error(Http503, "Sales unavailable") return RestApiResponse.error(Http503, "Sales unavailable")
let availability = Availability.init(size, duration, minPrice)
contracts.sales.add(availability) contracts.sales.add(availability)
return RestApiResponse.response(availability.id.toHex) return RestApiResponse.response(availability.id.toHex)

15
dagger/rest/json.nim Normal file
View File

@ -0,0 +1,15 @@
import std/json
import std/strutils
import pkg/stew/byteutils
import pkg/questionable/results
import ../sales
func fromHex(T: type SomeInteger, s: string): T =
strutils.fromHex[T](s)
proc fromJson*(_: type Availability, bytes: seq[byte]): ?!Availability =
let json = ?catch parseJson(string.fromBytes(bytes))
let size = ?catch UInt256.fromHex(json["size"].getStr)
let duration = ?catch uint64.fromHex(json["duration"].getStr)
let minPrice = ?catch uint64.fromHex(json["minPrice"].getStr)
success Availability.init(size, duration, minPrice)

View File

@ -3,6 +3,7 @@ import std/os
import std/streams import std/streams
import std/strutils import std/strutils
import std/httpclient import std/httpclient
import std/json
import pkg/asynctest import pkg/asynctest
import pkg/chronos import pkg/chronos
@ -41,5 +42,11 @@ suite "Integration tests":
test "node handles new storage availability": test "node handles new storage availability":
let baseurl = "http://localhost:8080/api/dagger/v1" let baseurl = "http://localhost:8080/api/dagger/v1"
let url = baseurl & "/sales/availability?size=1&duration=1&minPrice=0x2A" let url = baseurl & "/sales/availability"
check client.get(url).status == "200 OK" let json = %*{
"size": "0x1",
"duration": "0x2",
"minPrice": "0x3"
}
let response = client.post(url, $json)
check response.status == "200 OK"