mirror of
https://github.com/waku-org/nwaku.git
synced 2025-02-06 12:04:50 +00:00
46 lines
1.5 KiB
Nim
46 lines
1.5 KiB
Nim
{.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.}
|