mirror of https://github.com/waku-org/nwaku.git
feat(rest): Add HTTP REST API (#727). Amend Debug REST API client
This commit is contained in:
parent
c9076c6550
commit
d2feb7c763
|
@ -1,12 +1,15 @@
|
||||||
|
{.used.}
|
||||||
|
|
||||||
import
|
import
|
||||||
stew/shims/net,
|
stew/shims/net,
|
||||||
chronicles,
|
chronicles,
|
||||||
testutils/unittests,
|
testutils/unittests,
|
||||||
presto,
|
presto,
|
||||||
libp2p/crypto/crypto
|
libp2p/crypto/crypto
|
||||||
import
|
import
|
||||||
../../waku/v2/node/wakunode2,
|
../../waku/v2/node/wakunode2,
|
||||||
../../waku/v2/node/rest/[server, client, debug_api]
|
../../waku/v2/node/rest/[server, client, utils],
|
||||||
|
../../waku/v2/node/rest/debug/debug_api
|
||||||
|
|
||||||
|
|
||||||
proc testWakuNode(): WakuNode =
|
proc testWakuNode(): WakuNode =
|
||||||
|
@ -45,7 +48,9 @@ suite "REST API - Debug":
|
||||||
|
|
||||||
# Then
|
# Then
|
||||||
check:
|
check:
|
||||||
response.listenAddresses == @[$node.switch.peerInfo.addrs[^1] & "/p2p/" & $node.switch.peerInfo.peerId]
|
response.status == 200
|
||||||
|
response.contentType == $MIMETYPE_JSON
|
||||||
|
response.data.listenAddresses == @[$node.switch.peerInfo.addrs[^1] & "/p2p/" & $node.switch.peerInfo.peerId]
|
||||||
|
|
||||||
await restServer.stop()
|
await restServer.stop()
|
||||||
await restServer.closeWait()
|
await restServer.closeWait()
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
|
{.used.}
|
||||||
|
|
||||||
import std/typetraits
|
import std/typetraits
|
||||||
import chronicles,
|
import chronicles,
|
||||||
unittest2,
|
unittest2,
|
||||||
stew/[results, byteutils],
|
stew/[results, byteutils],
|
||||||
json_serialization
|
json_serialization
|
||||||
import
|
import
|
||||||
../../waku/v2/node/rest/[serdes, debug_api]
|
../../waku/v2/node/rest/serdes,
|
||||||
|
../../waku/v2/node/rest/debug/api_types
|
||||||
|
|
||||||
|
|
||||||
suite "Debug API - serialization":
|
suite "Debug API - serialization":
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
|
{.used.}
|
||||||
|
|
||||||
import std/typetraits
|
import std/typetraits
|
||||||
import chronicles,
|
import chronicles,
|
||||||
unittest2,
|
unittest2,
|
||||||
stew/[results, byteutils],
|
stew/[results, byteutils],
|
||||||
json_serialization
|
json_serialization
|
||||||
import
|
import
|
||||||
../../waku/v2/node/rest/[serdes, debug_api]
|
../../waku/v2/node/rest/serdes,
|
||||||
|
../../waku/v2/node/rest/debug/api_types
|
||||||
|
|
||||||
|
|
||||||
# TODO: Decouple this test suite from the `debug_api` module by defining
|
# TODO: Decouple this test suite from the `debug_api` module by defining
|
||||||
|
|
|
@ -1,16 +1,11 @@
|
||||||
{.push raises: [Defect].}
|
{.push raises: [Defect].}
|
||||||
|
|
||||||
import
|
import
|
||||||
stew/byteutils,
|
|
||||||
chronicles,
|
chronicles,
|
||||||
json_serialization,
|
json_serialization,
|
||||||
json_serialization/std/options,
|
json_serialization/std/options
|
||||||
presto/[route, client]
|
import ".."/serdes
|
||||||
import "."/[serdes, utils],
|
import ../../wakunode2
|
||||||
../wakunode2
|
|
||||||
|
|
||||||
logScope: topics = "rest_api_debug"
|
|
||||||
|
|
||||||
|
|
||||||
#### Types
|
#### Types
|
||||||
|
|
||||||
|
@ -20,6 +15,15 @@ type
|
||||||
enrUri*: Option[string]
|
enrUri*: Option[string]
|
||||||
|
|
||||||
|
|
||||||
|
#### Type conversion
|
||||||
|
|
||||||
|
proc toDebugWakuInfo*(nodeInfo: WakuInfo): DebugWakuInfo =
|
||||||
|
DebugWakuInfo(
|
||||||
|
listenAddresses: nodeInfo.listenAddresses,
|
||||||
|
enrUri: some(nodeInfo.enrUri)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
#### Serialization and deserialization
|
#### Serialization and deserialization
|
||||||
|
|
||||||
proc writeValue*(writer: var JsonWriter[RestJson], value: DebugWakuInfo)
|
proc writeValue*(writer: var JsonWriter[RestJson], value: DebugWakuInfo)
|
||||||
|
@ -55,42 +59,4 @@ proc readValue*(reader: var JsonReader[RestJson], value: var DebugWakuInfo)
|
||||||
value = DebugWakuInfo(
|
value = DebugWakuInfo(
|
||||||
listenAddresses: listenAddresses.get,
|
listenAddresses: listenAddresses.get,
|
||||||
enrUri: enrUri
|
enrUri: enrUri
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
#### Server request handlers
|
|
||||||
|
|
||||||
proc toDebugWakuInfo(nodeInfo: WakuInfo): DebugWakuInfo =
|
|
||||||
DebugWakuInfo(
|
|
||||||
listenAddresses: nodeInfo.listenAddresses,
|
|
||||||
enrUri: some(nodeInfo.enrUri)
|
|
||||||
)
|
|
||||||
|
|
||||||
const ROUTE_DEBUG_INFOV1* = "/debug/v1/info"
|
|
||||||
|
|
||||||
proc installDebugInfoV1Handler(router: var RestRouter, node: WakuNode) =
|
|
||||||
router.api(MethodGet, ROUTE_DEBUG_INFOV1) do () -> RestApiResponse:
|
|
||||||
let info = node.info().toDebugWakuInfo()
|
|
||||||
let resp = RestApiResponse.jsonResponse(info, status=Http200)
|
|
||||||
if resp.isErr():
|
|
||||||
debug "An error ocurred while building the json respose", error=resp.error()
|
|
||||||
return RestApiResponse.internalServerError()
|
|
||||||
|
|
||||||
return resp.get()
|
|
||||||
|
|
||||||
proc installDebugApiHandlers*(router: var RestRouter, node: WakuNode) =
|
|
||||||
installDebugInfoV1Handler(router, node)
|
|
||||||
|
|
||||||
|
|
||||||
#### Client
|
|
||||||
|
|
||||||
proc decodeBytes*(t: typedesc[DebugWakuInfo], data: openArray[byte], contentType: string): RestResult[DebugWakuInfo] =
|
|
||||||
if MediaType.init(contentType) != MIMETYPE_JSON:
|
|
||||||
error "Unsupported respose contentType value", contentType = contentType
|
|
||||||
return err("Unsupported response contentType")
|
|
||||||
|
|
||||||
let decoded = ?decodeFromJsonBytes(DebugWakuInfo, data)
|
|
||||||
return ok(decoded)
|
|
||||||
|
|
||||||
# TODO: Check how we can use a constant to set the method endpoint (improve "rest" pragma under nim-presto)
|
|
||||||
proc debugInfoV1*(): DebugWakuInfo {.rest, endpoint: "/debug/v1/info", meth: HttpMethod.MethodGet.}
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
{.push raises: [Defect].}
|
||||||
|
|
||||||
|
import
|
||||||
|
stew/byteutils,
|
||||||
|
chronicles,
|
||||||
|
json_serialization,
|
||||||
|
json_serialization/std/options,
|
||||||
|
presto/[route, client]
|
||||||
|
import "."/api_types
|
||||||
|
import ".."/[serdes, utils]
|
||||||
|
import ../../wakunode2
|
||||||
|
|
||||||
|
logScope: topics = "rest_api_debug"
|
||||||
|
|
||||||
|
|
||||||
|
#### Server request handlers
|
||||||
|
|
||||||
|
const ROUTE_DEBUG_INFOV1* = "/debug/v1/info"
|
||||||
|
|
||||||
|
proc installDebugInfoV1Handler(router: var RestRouter, node: WakuNode) =
|
||||||
|
router.api(MethodGet, ROUTE_DEBUG_INFOV1) do () -> RestApiResponse:
|
||||||
|
let info = node.info().toDebugWakuInfo()
|
||||||
|
let resp = RestApiResponse.jsonResponse(info, status=Http200)
|
||||||
|
if resp.isErr():
|
||||||
|
debug "An error occurred while building the json respose", error=resp.error()
|
||||||
|
return RestApiResponse.internalServerError()
|
||||||
|
|
||||||
|
return resp.get()
|
||||||
|
|
||||||
|
proc installDebugApiHandlers*(router: var RestRouter, node: WakuNode) =
|
||||||
|
installDebugInfoV1Handler(router, node)
|
||||||
|
|
||||||
|
|
||||||
|
#### Client
|
||||||
|
|
||||||
|
proc decodeBytes*(t: typedesc[DebugWakuInfo], data: openArray[byte], contentType: string): RestResult[DebugWakuInfo] =
|
||||||
|
if MediaType.init(contentType) != MIMETYPE_JSON:
|
||||||
|
error "Unsupported respose contentType value", contentType = contentType
|
||||||
|
return err("Unsupported response contentType")
|
||||||
|
|
||||||
|
let decoded = ?decodeFromJsonBytes(DebugWakuInfo, data)
|
||||||
|
return ok(decoded)
|
||||||
|
|
||||||
|
# TODO: Check how we can use a constant to set the method endpoint (improve "rest" pragma under nim-presto)
|
||||||
|
proc debugInfoV1*(): RestResponse[DebugWakuInfo] {.rest, endpoint: "/debug/v1/info", meth: HttpMethod.MethodGet.}
|
|
@ -1,15 +1,23 @@
|
||||||
openapi: 3.1.0
|
openapi: 3.0.3
|
||||||
info:
|
info:
|
||||||
title: Waku REST API - Debug
|
title: Waku V2 node REST API
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
|
contact:
|
||||||
|
name: VAC Team
|
||||||
|
url: https://forum.vac.dev/
|
||||||
|
|
||||||
|
tags:
|
||||||
|
- name: debug
|
||||||
|
description: Debug REST API for WakuV2 node
|
||||||
|
|
||||||
paths:
|
paths:
|
||||||
/debug/v1/info:
|
/debug/v1/info:
|
||||||
get:
|
get:
|
||||||
summary: Get node info
|
summary: Get node info
|
||||||
description: Retrieve information about a Waku v2 node.
|
description: Retrieve information about a Waku v2 node.
|
||||||
|
operationId: getNodeInfo
|
||||||
tags:
|
tags:
|
||||||
- Debug
|
- debug
|
||||||
responses:
|
responses:
|
||||||
'200':
|
'200':
|
||||||
description: Information about a Waku v2 node.
|
description: Information about a Waku v2 node.
|
Loading…
Reference in New Issue