feat(waku api): Add waku send message rest api

This commit is contained in:
Emil Ivanichkov 2024-04-19 14:50:13 +03:00 committed by Emil Ivanichkov
parent 5c3b44449d
commit 289d42a192
5 changed files with 49 additions and 2 deletions

View File

@ -4,13 +4,11 @@ import
presto/[route, segpath, server],
stew/io2,
serialization, json_serialization,
waku/waku_noise/noise_types,
# Local packages
./types,
../../[rest_constants, rest_serialization],
../../../waku,
../../../filepaths,
../../../../../libs/waku_utils/waku_pair
from confutils import OutFile, `$`
@ -88,3 +86,25 @@ proc installWakuApiHandlers*(router: var RestRouter,
return wakuApiError(Http500, saveHandshakeRes.error())
except:
return wakuApiError(Http500, "Internal Server Error")
router.api(MethodPost, "/waku/send"
) do (contentBody: Option[ContentBody]) -> RestApiResponse:
let wakuSendMessageData =
block:
if contentBody.isNone():
return wakuApiError(Http404, EmptyRequestBodyError)
let dres = decodeBody(WakuSendMessageRequestData, contentBody.get())
if dres.isErr():
return wakuApiError(Http400, InvalidWakuSendMessageObjects)
dres.get()
try:
let wakuSendResult = wakuSendMessage(wakuHost,
wakuSendMessageData.message,
wakuSendMessageData.contentTopic)
notice "Waku message sent successfully! Request fulfilled."
return RestApiResponse.response("Message sent successfully",
Http200, "application/json")
except:
return wakuApiError(Http500, "Internal Server Error")

View File

@ -18,6 +18,10 @@ proc wakuExportHandshakePlain*(body: WakuExportHandshakeRequestData): RestPlainR
rest, endpoint: "/waku/export/handshake",
meth: MethodPost.}
proc wakuSendMessagePlain*(body: WakuSendMessageRequestData): RestPlainResponse {.
rest, endpoint: "/waku/send",
meth: MethodPost.}
proc wakuPair*(client: RestClientRef, wakuPairData: WakuPairRequestData) {.async.} =
notice "Initiating Waku Pair request..."
let
@ -45,3 +49,17 @@ proc wakuExportHandshake*(client : RestClientRef,
fatal "Waku Handshake Export request failed", status=resp.status, body=respMsg
else:
raiseUnknownStatusError(resp)
proc wakuSendMessage*(client: RestClientRef,
wakuSendMessageData: WakuSendMessageRequestData) {.async.} =
notice "Initiating Waku Send Message request..."
let
resp = await client.wakuSendMessagePlain(wakuSendMessageData)
respMsg = string.fromBytes(resp.data)
case resp.status:
of 200:
notice "Waku Send Message request successful", body=respMsg
of 400, 401, 403, 404, 500:
notice "Waku Send Message request failed", status=resp.status, body=respMsg
else:
raiseUnknownStatusError(resp)

View File

@ -7,3 +7,7 @@ type WakuPairRequestData* = object
type WakuExportHandshakeRequestData* = object
exportFile*: string
type WakuSendMessageRequestData* = object
message*: string
contentTopic*: string

View File

@ -7,3 +7,5 @@ const
"Invalid Waku export handshake objects found"
WakuHandshakeFileExportError* =
"Waku handshake file export failed"
InvalidWakuSendMessageObjects* =
"Invalid Waku send message objects found"

View File

@ -22,6 +22,7 @@ createJsonFlavor RestJson
RestJson.useDefaultSerializationFor(
WakuPairRequestData,
WakuExportHandshakeRequestData,
WakuSendMessageRequestData,
HandshakeResult,
CipherState,
)
@ -30,6 +31,7 @@ type
DecodeTypes* =
WakuPairRequestData |
WakuExportHandshakeRequestData |
WakuSendMessageRequestData |
HandshakeResult |
CipherState |
MDigest[256]
@ -38,6 +40,7 @@ type
EncodeTypes* =
WakuPairRequestData |
WakuExportHandshakeRequestData |
WakuSendMessageRequestData |
HandshakeResult |
CipherState |
MDigest[256]