2023-03-27 13:47:25 +00:00
|
|
|
import std/httpclient
|
|
|
|
import std/strutils
|
2023-11-21 00:14:06 +00:00
|
|
|
import std/sequtils
|
|
|
|
|
2023-09-01 05:44:41 +00:00
|
|
|
from pkg/libp2p import Cid, `$`, init
|
|
|
|
import pkg/chronicles
|
2023-03-27 13:47:25 +00:00
|
|
|
import pkg/stint
|
|
|
|
import pkg/questionable/results
|
2023-09-01 05:44:41 +00:00
|
|
|
import pkg/codex/rest/json
|
|
|
|
import pkg/codex/purchasing
|
|
|
|
import pkg/codex/errors
|
|
|
|
import pkg/codex/sales/reservations
|
2023-03-27 13:47:25 +00:00
|
|
|
|
2023-12-07 01:16:36 +00:00
|
|
|
export purchasing
|
|
|
|
|
2023-03-27 13:47:25 +00:00
|
|
|
type CodexClient* = ref object
|
|
|
|
http: HttpClient
|
|
|
|
baseurl: string
|
|
|
|
|
|
|
|
proc new*(_: type CodexClient, baseurl: string): CodexClient =
|
|
|
|
CodexClient(http: newHttpClient(), baseurl: baseurl)
|
|
|
|
|
|
|
|
proc info*(client: CodexClient): JsonNode =
|
|
|
|
let url = client.baseurl & "/debug/info"
|
|
|
|
client.http.getContent(url).parseJson()
|
|
|
|
|
|
|
|
proc setLogLevel*(client: CodexClient, level: string) =
|
|
|
|
let url = client.baseurl & "/debug/chronicles/loglevel?level=" & level
|
|
|
|
let headers = newHttpHeaders({"Content-Type": "text/plain"})
|
|
|
|
let response = client.http.request(url, httpMethod=HttpPost, headers=headers)
|
|
|
|
assert response.status == "200 OK"
|
|
|
|
|
2023-09-01 05:44:41 +00:00
|
|
|
proc upload*(client: CodexClient, contents: string): ?!Cid =
|
2023-11-09 08:47:09 +00:00
|
|
|
let response = client.http.post(client.baseurl & "/data", contents)
|
2023-03-27 13:47:25 +00:00
|
|
|
assert response.status == "200 OK"
|
2023-09-01 05:44:41 +00:00
|
|
|
Cid.init(response.body).mapFailure
|
2023-03-27 13:47:25 +00:00
|
|
|
|
2023-11-21 00:14:06 +00:00
|
|
|
proc download*(client: CodexClient, cid: Cid, local = false): ?!string =
|
|
|
|
let
|
|
|
|
response = client.http.get(
|
|
|
|
client.baseurl & "/data/" & $cid &
|
|
|
|
(if local: "" else: "/network"))
|
|
|
|
|
|
|
|
if response.status != "200 OK":
|
|
|
|
return failure(response.status)
|
|
|
|
|
|
|
|
success response.body
|
|
|
|
|
|
|
|
proc list*(client: CodexClient): ?!seq[RestContent] =
|
|
|
|
let url = client.baseurl & "/data"
|
|
|
|
let response = client.http.get(url)
|
|
|
|
|
|
|
|
if response.status != "200 OK":
|
|
|
|
return failure(response.status)
|
|
|
|
|
|
|
|
let json = ? parseJson(response.body).catch
|
|
|
|
seq[RestContent].fromJson(json)
|
|
|
|
|
2023-11-22 11:35:26 +00:00
|
|
|
proc requestStorageRaw*(
|
|
|
|
client: CodexClient,
|
|
|
|
cid: Cid,
|
|
|
|
duration: UInt256,
|
|
|
|
reward: UInt256,
|
|
|
|
proofProbability: UInt256,
|
|
|
|
collateral: UInt256,
|
|
|
|
expiry: UInt256 = 0.u256,
|
|
|
|
nodes: uint = 1,
|
|
|
|
tolerance: uint = 0
|
|
|
|
): Response =
|
|
|
|
|
|
|
|
## Call request storage REST endpoint
|
|
|
|
##
|
|
|
|
let url = client.baseurl & "/storage/request/" & $cid
|
|
|
|
let json = %*{
|
|
|
|
"duration": duration,
|
|
|
|
"reward": reward,
|
|
|
|
"proofProbability": proofProbability,
|
|
|
|
"collateral": collateral,
|
|
|
|
"nodes": nodes,
|
|
|
|
"tolerance": tolerance
|
|
|
|
}
|
|
|
|
|
|
|
|
if expiry != 0:
|
|
|
|
json["expiry"] = %expiry
|
|
|
|
|
|
|
|
return client.http.post(url, $json)
|
|
|
|
|
2023-06-22 15:11:18 +00:00
|
|
|
proc requestStorage*(
|
|
|
|
client: CodexClient,
|
2023-09-01 05:44:41 +00:00
|
|
|
cid: Cid,
|
|
|
|
duration: UInt256,
|
|
|
|
reward: UInt256,
|
|
|
|
proofProbability: UInt256,
|
2023-06-22 15:11:18 +00:00
|
|
|
expiry: UInt256,
|
2023-09-01 05:44:41 +00:00
|
|
|
collateral: UInt256,
|
|
|
|
nodes: uint = 1,
|
|
|
|
tolerance: uint = 0
|
|
|
|
): ?!PurchaseId =
|
2023-06-22 15:11:18 +00:00
|
|
|
## Call request storage REST endpoint
|
2023-09-01 05:44:41 +00:00
|
|
|
##
|
2023-11-22 11:35:26 +00:00
|
|
|
let response = client.requestStorageRaw(cid, duration, reward, proofProbability, collateral, expiry, nodes, tolerance)
|
2023-03-27 13:47:25 +00:00
|
|
|
assert response.status == "200 OK"
|
2023-09-01 05:44:41 +00:00
|
|
|
PurchaseId.fromHex(response.body).catch
|
2023-03-27 13:47:25 +00:00
|
|
|
|
2023-09-01 05:44:41 +00:00
|
|
|
proc getPurchase*(client: CodexClient, purchaseId: PurchaseId): ?!RestPurchase =
|
|
|
|
let url = client.baseurl & "/storage/purchases/" & purchaseId.toHex
|
2023-03-27 13:47:25 +00:00
|
|
|
let body = client.http.getContent(url)
|
2023-09-01 05:44:41 +00:00
|
|
|
let json = ? parseJson(body).catch
|
|
|
|
RestPurchase.fromJson(json)
|
2023-03-27 13:47:25 +00:00
|
|
|
|
2023-12-07 01:16:36 +00:00
|
|
|
proc getSalesAgent*(client: CodexClient, slotId: SlotId): ?!RestSalesAgent =
|
|
|
|
let url = client.baseurl & "/sales/slots/" & slotId.toHex
|
|
|
|
echo "getting sales agent for id, ", slotId.toHex
|
|
|
|
try:
|
|
|
|
let body = client.http.getContent(url)
|
|
|
|
echo "get sales agent body: ", body
|
|
|
|
let json = ? parseJson(body).catch
|
|
|
|
return RestSalesAgent.fromJson(json)
|
|
|
|
except CatchableError as e:
|
|
|
|
echo "[client.getSalesAgent] error getting agent: ", e.msg
|
|
|
|
return failure e.msg
|
|
|
|
|
2023-10-24 10:12:54 +00:00
|
|
|
proc getSlots*(client: CodexClient): ?!seq[Slot] =
|
2023-06-20 12:52:15 +00:00
|
|
|
let url = client.baseurl & "/sales/slots"
|
|
|
|
let body = client.http.getContent(url)
|
2023-10-24 10:12:54 +00:00
|
|
|
let json = ? parseJson(body).catch
|
|
|
|
seq[Slot].fromJson(json)
|
2023-06-20 12:52:15 +00:00
|
|
|
|
2023-06-22 15:11:18 +00:00
|
|
|
proc postAvailability*(
|
|
|
|
client: CodexClient,
|
2023-09-01 05:44:41 +00:00
|
|
|
size, duration, minPrice, maxCollateral: UInt256
|
|
|
|
): ?!Availability =
|
2023-06-22 15:11:18 +00:00
|
|
|
## Post sales availability endpoint
|
2023-09-01 05:44:41 +00:00
|
|
|
##
|
2023-03-27 13:47:25 +00:00
|
|
|
let url = client.baseurl & "/sales/availability"
|
|
|
|
let json = %*{
|
2023-09-01 05:44:41 +00:00
|
|
|
"size": size,
|
|
|
|
"duration": duration,
|
|
|
|
"minPrice": minPrice,
|
|
|
|
"maxCollateral": maxCollateral,
|
2023-03-27 13:47:25 +00:00
|
|
|
}
|
|
|
|
let response = client.http.post(url, $json)
|
2023-12-07 01:16:36 +00:00
|
|
|
doAssert response.status == "200 OK", "expected 200 OK, got " & response.status & ", body: " & response.body
|
2023-09-01 05:44:41 +00:00
|
|
|
Availability.fromJson(response.body.parseJson)
|
2023-03-27 13:47:25 +00:00
|
|
|
|
2023-09-01 05:44:41 +00:00
|
|
|
proc getAvailabilities*(client: CodexClient): ?!seq[Availability] =
|
2023-06-22 15:11:18 +00:00
|
|
|
## Call sales availability REST endpoint
|
2023-03-27 13:47:25 +00:00
|
|
|
let url = client.baseurl & "/sales/availability"
|
|
|
|
let body = client.http.getContent(url)
|
2023-09-01 05:44:41 +00:00
|
|
|
seq[Availability].fromJson(parseJson(body))
|
2023-03-27 13:47:25 +00:00
|
|
|
|
|
|
|
proc close*(client: CodexClient) =
|
|
|
|
client.http.close()
|
|
|
|
|
|
|
|
proc restart*(client: CodexClient) =
|
|
|
|
client.http.close()
|
|
|
|
client.http = newHttpClient()
|
2023-12-07 01:16:36 +00:00
|
|
|
|
|
|
|
proc purchaseStateIs*(client: CodexClient, id: PurchaseId, state: string): bool =
|
|
|
|
client.getPurchase(id).option.?state == some state
|
|
|
|
|
|
|
|
proc saleStateIs*(client: CodexClient, id: SlotId, state: string): bool =
|
|
|
|
client.getSalesAgent(id).option.?state == some state
|
|
|
|
|
|
|
|
proc requestId*(client: CodexClient, id: PurchaseId): ?RequestId =
|
|
|
|
return client.getPurchase(id).option.?requestId
|