nwaku/waku/waku_api/rest/responses.nim
NagyZoltanPeter a00f350cd1
feat: Added simple, configurable rate limit for lightpush and store-query (#2390)
* feat: Added simple, configurable rate limit for lightpush and store-query
Adjust lightpush rest response to rate limit, added tests ann some fixes
Add rest store query test for rate limit checks and proper error response
Update apps/wakunode2/external_config.nim
Move chronos/tokenbucket to nwaku codebasee with limited and fixed feature set
Add meterics counter to lightpush rate limits

Co-authored-by: gabrielmer <101006718+gabrielmer@users.noreply.github.com>
2024-04-15 15:28:35 +02:00

49 lines
1.7 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 serviceUnavailable*(
t: typedesc[RestApiResponse], msg: string = ""
): RestApiResponse =
RestApiResponse.error(Http503, 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 tooManyRequests*(t: typedesc[RestApiResponse], msg: string = ""): RestApiResponse =
RestApiResponse.error(Http429, 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)