mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-11 07:16:27 +00:00
6d81e3841a
* Allow text/plain content type descriptor for json formatted content body. Refactored duplicated encode/decode functions for rest api * Fix relay endpoint decodings of content bodies to accept text/plain * Added support for content body decoder for checking media type if additional parameters are present * Fix wrong usage of ContentTypeData - appeared only for tests
86 lines
2.3 KiB
Nim
86 lines
2.3 KiB
Nim
when (NimMajor, NimMinor) < (1, 4):
|
|
{.push raises: [Defect].}
|
|
else:
|
|
{.push raises: [].}
|
|
|
|
import
|
|
std/strformat,
|
|
std/sequtils,
|
|
stew/byteutils,
|
|
chronicles,
|
|
json_serialization,
|
|
json_serialization/std/options,
|
|
presto/route,
|
|
presto/common
|
|
|
|
import
|
|
../../../waku_core,
|
|
../../waku/node/peer_manager,
|
|
../../../waku_node,
|
|
../../waku/waku_lightpush,
|
|
../../handlers,
|
|
../serdes,
|
|
../responses,
|
|
../rest_serdes,
|
|
./types
|
|
|
|
export types
|
|
|
|
logScope:
|
|
topics = "waku node rest lightpush api"
|
|
|
|
const futTimeoutForPushRequestProcessing* = 5.seconds
|
|
|
|
const NoPeerNoDiscoError = RestApiResponse.serviceUnavailable(
|
|
"No suitable service peer & no discovery method")
|
|
|
|
const NoPeerNoneFoundError = RestApiResponse.serviceUnavailable(
|
|
"No suitable service peer & none discovered")
|
|
|
|
#### Request handlers
|
|
|
|
const ROUTE_LIGHTPUSH* = "/lightpush/v1/message"
|
|
|
|
proc installLightPushRequestHandler*(
|
|
router: var RestRouter,
|
|
node: WakuNode,
|
|
discHandler: Option[DiscoveryHandler] = none(DiscoveryHandler),
|
|
) =
|
|
|
|
router.api(MethodPost, ROUTE_LIGHTPUSH) do (contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
## Send a request to push a waku message
|
|
debug "post", ROUTE_LIGHTPUSH, contentBody
|
|
|
|
let decodedBody = decodeRequestBody[PushRequest](contentBody)
|
|
|
|
if decodedBody.isErr():
|
|
return decodedBody.error()
|
|
|
|
let req: PushRequest = decodedBody.value()
|
|
|
|
let msg = req.message.toWakuMessage().valueOr:
|
|
return RestApiResponse.badRequest("Invalid message: " & $error)
|
|
|
|
let peer = node.peerManager.selectPeer(WakuLightPushCodec).valueOr:
|
|
let handler = discHandler.valueOr:
|
|
return NoPeerNoDiscoError
|
|
|
|
let peerOp = (await handler()).valueOr:
|
|
return RestApiResponse.internalServerError("No value in peerOp: " & $error)
|
|
|
|
peerOp.valueOr:
|
|
return NoPeerNoneFoundError
|
|
|
|
let subFut = node.lightpushPublish(req.pubsubTopic, msg, peer)
|
|
|
|
if not await subFut.withTimeout(futTimeoutForPushRequestProcessing):
|
|
error "Failed to request a message push due to timeout!"
|
|
return RestApiResponse.serviceUnavailable("Push request timed out")
|
|
|
|
if subFut.value().isErr():
|
|
return RestApiResponse.serviceUnavailable(
|
|
fmt("Failed to request a message push: {subFut.value().error}")
|
|
)
|
|
|
|
return RestApiResponse.ok()
|