2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2022-06-02 09:45:00 +00:00
|
|
|
|
2024-07-09 11:14:28 +00:00
|
|
|
import std/typetraits, results, chronicles, presto/common
|
2024-03-15 23:08:47 +00:00
|
|
|
import ./serdes
|
2022-06-02 09:45:00 +00:00
|
|
|
|
|
|
|
const MIMETYPE_JSON* = MediaType.init("application/json")
|
2022-06-10 11:30:51 +00:00
|
|
|
const MIMETYPE_TEXT* = MediaType.init("text/plain")
|
2022-06-02 09:45:00 +00:00
|
|
|
|
2023-02-13 14:22:24 +00:00
|
|
|
proc ok*(t: typedesc[RestApiResponse]): RestApiResponse =
|
|
|
|
RestApiResponse.response("OK", Http200, $MIMETYPE_TEXT)
|
2022-06-02 09:45:00 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc internalServerError*(
|
|
|
|
t: typedesc[RestApiResponse], msg: string = ""
|
|
|
|
): RestApiResponse =
|
2023-04-06 09:43:19 +00:00
|
|
|
RestApiResponse.error(Http500, msg, $MIMETYPE_TEXT)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc serviceUnavailable*(
|
|
|
|
t: typedesc[RestApiResponse], msg: string = ""
|
|
|
|
): RestApiResponse =
|
2023-09-22 11:46:55 +00:00
|
|
|
RestApiResponse.error(Http503, msg, $MIMETYPE_TEXT)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc badRequest*(t: typedesc[RestApiResponse], msg: string = ""): RestApiResponse =
|
2023-04-06 09:43:19 +00:00
|
|
|
RestApiResponse.error(Http400, msg, $MIMETYPE_TEXT)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc notFound*(t: typedesc[RestApiResponse], msg: string = ""): RestApiResponse =
|
2023-04-06 09:43:19 +00:00
|
|
|
RestApiResponse.error(Http404, msg, $MIMETYPE_TEXT)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc preconditionFailed*(
|
|
|
|
t: typedesc[RestApiResponse], msg: string = ""
|
|
|
|
): RestApiResponse =
|
2023-04-06 09:43:19 +00:00
|
|
|
RestApiResponse.error(Http412, msg, $MIMETYPE_TEXT)
|
2023-02-13 14:22:24 +00:00
|
|
|
|
2024-04-15 13:28:35 +00:00
|
|
|
proc tooManyRequests*(t: typedesc[RestApiResponse], msg: string = ""): RestApiResponse =
|
|
|
|
RestApiResponse.error(Http429, msg, $MIMETYPE_TEXT)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc jsonResponse*(
|
|
|
|
t: typedesc[RestApiResponse], data: auto, status: HttpCode = Http200
|
|
|
|
): SerdesResult[RestApiResponse] =
|
2023-02-13 14:22:24 +00:00
|
|
|
let encoded = ?encodeIntoJsonBytes(data)
|
|
|
|
ok(RestApiResponse.response(encoded, status, $MIMETYPE_JSON))
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc textResponse*(
|
|
|
|
t: typedesc[RestApiResponse], data: string, status: HttpCode = Http200
|
|
|
|
): RestApiResponse =
|
2023-02-13 14:22:24 +00:00
|
|
|
RestApiResponse.response(data, status, $MIMETYPE_TEXT)
|