nwaku/waku/v2/node/rest/responses.nim
Ivansete-status b2acb54d6a
feat(rest-api-store): new rest api to retrieve store waku messages (#1611) (#1630)
* feat: new rest api based on the current store json-rpc api and
following the same structure as the current relay rest api.

* feat: the store api attend GET requests to retrieve historical messages

* feat: unit tests.

* feat: allow return message to rest-client in case error (4XX or 5XX)

* chore: always allow to call the store api endpoints (only rest) without explicit storenode (#1575)

* feat: always mounting the current node as storenode client
2023-04-06 11:43:19 +02:00

49 lines
1.5 KiB
Nim

when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import
std/typetraits,
stew/results,
chronicles,
presto/common
import
./serdes
const MIMETYPE_JSON* = MediaType.init("application/json")
const MIMETYPE_TEXT* = MediaType.init("text/plain")
proc ok*(t: typedesc[RestApiResponse]): RestApiResponse =
RestApiResponse.response("OK", Http200, $MIMETYPE_TEXT)
proc internalServerError*(t: typedesc[RestApiResponse],
msg: string = ""):
RestApiResponse =
RestApiResponse.error(Http500, msg, $MIMETYPE_TEXT)
proc badRequest*(t: typedesc[RestApiResponse],
msg: string = ""):
RestApiResponse =
RestApiResponse.error(Http400, msg, $MIMETYPE_TEXT)
proc notFound*(t: typedesc[RestApiResponse],
msg: string = ""):
RestApiResponse =
RestApiResponse.error(Http404, msg, $MIMETYPE_TEXT)
proc preconditionFailed*(t: typedesc[RestApiResponse],
msg: string = ""):
RestApiResponse =
RestApiResponse.error(Http412, msg, $MIMETYPE_TEXT)
proc jsonResponse*(t: typedesc[RestApiResponse], data: auto, status: HttpCode = Http200): SerdesResult[RestApiResponse] =
let encoded = ?encodeIntoJsonBytes(data)
ok(RestApiResponse.response(encoded, status, $MIMETYPE_JSON))
proc textResponse*(t: typedesc[RestApiResponse], data: string, status: HttpCode = Http200): RestApiResponse =
RestApiResponse.response(data, status, $MIMETYPE_TEXT)