[sales] Perform POST with JSON body instead of URL params
This commit is contained in:
parent
a43e3d6d94
commit
ef80871984
|
@ -30,6 +30,8 @@ import ../blocktype
|
|||
import ../conf
|
||||
import ../contracts
|
||||
|
||||
import ./json
|
||||
|
||||
proc validate(
|
||||
pattern: string,
|
||||
value: string): int
|
||||
|
@ -330,31 +332,23 @@ proc initRestApi*(node: DaggerNodeRef, conf: DaggerConf): RestRouter =
|
|||
"\nAddrs: \n" & addrs &
|
||||
"\nRoot Dir: " & $conf.dataDir)
|
||||
|
||||
router.api(
|
||||
MethodGet,
|
||||
"/api/dagger/v1/sales/availability") do (
|
||||
size: Option[uint64],
|
||||
duration: Option[uint64],
|
||||
minPrice: Option[UInt256]) -> RestApiResponse:
|
||||
router.rawApi(
|
||||
MethodPost,
|
||||
"/api/dagger/v1/sales/availability") do () -> RestApiResponse:
|
||||
## Add available storage to sell
|
||||
##
|
||||
## size - size of available storage in bytes
|
||||
## duration - maximum time the storage should be sold for (in seconds)
|
||||
## minPrice - minimum price to be paid (in amount of tokens)
|
||||
|
||||
without size =? size.?get():
|
||||
return RestApiResponse.error(Http400, "Missing or incorrect size")
|
||||
let body = await request.getBody()
|
||||
|
||||
without duration =? duration.?get():
|
||||
return RestApiResponse.error(Http400, "Missing or incorrect duration")
|
||||
|
||||
without minPrice =? minPrice.?get():
|
||||
return RestApiResponse.error(Http400, "Missing or incorrect minPrice")
|
||||
without availability =? Availability.fromJson(body), error:
|
||||
return RestApiResponse.error(Http400, error.msg)
|
||||
|
||||
without contracts =? node.contracts:
|
||||
return RestApiResponse.error(Http503, "Sales unavailable")
|
||||
|
||||
let availability = Availability.init(size, duration, minPrice)
|
||||
contracts.sales.add(availability)
|
||||
return RestApiResponse.response(availability.id.toHex)
|
||||
|
||||
|
|
|
@ -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)
|
|
@ -3,6 +3,7 @@ import std/os
|
|||
import std/streams
|
||||
import std/strutils
|
||||
import std/httpclient
|
||||
import std/json
|
||||
import pkg/asynctest
|
||||
import pkg/chronos
|
||||
|
||||
|
@ -41,5 +42,11 @@ suite "Integration tests":
|
|||
|
||||
test "node handles new storage availability":
|
||||
let baseurl = "http://localhost:8080/api/dagger/v1"
|
||||
let url = baseurl & "/sales/availability?size=1&duration=1&minPrice=0x2A"
|
||||
check client.get(url).status == "200 OK"
|
||||
let url = baseurl & "/sales/availability"
|
||||
let json = %*{
|
||||
"size": "0x1",
|
||||
"duration": "0x2",
|
||||
"minPrice": "0x3"
|
||||
}
|
||||
let response = client.post(url, $json)
|
||||
check response.status == "200 OK"
|
||||
|
|
Loading…
Reference in New Issue