feat(rest): Add HTTP REST API (#727). Amend Debug REST API client

This commit is contained in:
Lorenzo Delgado 2022-06-15 15:02:02 +02:00 committed by Lorenzo Delgado
parent c9076c6550
commit d2feb7c763
6 changed files with 85 additions and 55 deletions

View File

@ -1,3 +1,5 @@
{.used.}
import
stew/shims/net,
chronicles,
@ -6,7 +8,8 @@ import
libp2p/crypto/crypto
import
../../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 =
@ -45,7 +48,9 @@ suite "REST API - Debug":
# Then
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.closeWait()

View File

@ -1,10 +1,13 @@
{.used.}
import std/typetraits
import chronicles,
unittest2,
stew/[results, byteutils],
json_serialization
import
../../waku/v2/node/rest/[serdes, debug_api]
../../waku/v2/node/rest/serdes,
../../waku/v2/node/rest/debug/api_types
suite "Debug API - serialization":

View File

@ -1,10 +1,13 @@
{.used.}
import std/typetraits
import chronicles,
unittest2,
stew/[results, byteutils],
json_serialization
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

View File

@ -1,16 +1,11 @@
{.push raises: [Defect].}
import
stew/byteutils,
chronicles,
json_serialization,
json_serialization/std/options,
presto/[route, client]
import "."/[serdes, utils],
../wakunode2
logScope: topics = "rest_api_debug"
json_serialization/std/options
import ".."/serdes
import ../../wakunode2
#### Types
@ -20,6 +15,15 @@ type
enrUri*: Option[string]
#### Type conversion
proc toDebugWakuInfo*(nodeInfo: WakuInfo): DebugWakuInfo =
DebugWakuInfo(
listenAddresses: nodeInfo.listenAddresses,
enrUri: some(nodeInfo.enrUri)
)
#### Serialization and deserialization
proc writeValue*(writer: var JsonWriter[RestJson], value: DebugWakuInfo)
@ -56,41 +60,3 @@ proc readValue*(reader: var JsonReader[RestJson], value: var DebugWakuInfo)
listenAddresses: listenAddresses.get,
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.}

View File

@ -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.}

View File

@ -1,15 +1,23 @@
openapi: 3.1.0
openapi: 3.0.3
info:
title: Waku REST API - Debug
title: Waku V2 node REST API
version: 1.0.0
contact:
name: VAC Team
url: https://forum.vac.dev/
tags:
- name: debug
description: Debug REST API for WakuV2 node
paths:
/debug/v1/info:
get:
summary: Get node info
description: Retrieve information about a Waku v2 node.
operationId: getNodeInfo
tags:
- Debug
- debug
responses:
'200':
description: Information about a Waku v2 node.