hecate_plugin_api (hecate_sdk v0.6.2)
View SourceJSON API utilities for plugin Cowboy handlers.
Provides consistent JSON request/response handling for all plugins. Every function that sends a response automatically increments the appropriate metric counter (api_requests for all responses, api_errors for error responses).
Response format follows the hecate convention: Success: {ok: true, ...fields} Error: {ok: false, error: "reason"}
Values of undefined are sanitized to null before JSON encoding, so OTP json produces JSON null instead of the string "undefined".
Summary
Functions
400 Bad Request.
Format error term to binary for JSON encoding.
Get field from map, supporting both atom and binary keys.
Get field from map with default, supporting both atom and binary keys.
Send an error response with {ok: false, error: reason}. Increments the api_errors metric in addition to api_requests.
Send a 200 OK response with {ok: true} merged.
Send OK response with custom status code and {ok: true} merged.
Send a JSON response (alias for json_response/3).
Send a JSON response with status code. Increments the api_requests metric. Sanitizes undefined to null.
405 Method Not Allowed.
404 Not Found.
Read and decode JSON body from request.
Recursively sanitize data for JSON encoding. Converts atom undefined to null so json:encode/1 produces JSON null instead of the string "undefined".
Functions
-spec bad_request(Reason :: term(), Req :: cowboy_req:req()) -> {ok, cowboy_req:req(), []}.
400 Bad Request.
Format error term to binary for JSON encoding.
Get field from map, supporting both atom and binary keys.
Get field from map with default, supporting both atom and binary keys.
-spec json_error(StatusCode :: non_neg_integer(), Reason :: term(), Req :: cowboy_req:req()) -> {ok, cowboy_req:req(), []}.
Send an error response with {ok: false, error: reason}. Increments the api_errors metric in addition to api_requests.
-spec json_ok(Result :: map(), Req :: cowboy_req:req()) -> {ok, cowboy_req:req(), []}.
Send a 200 OK response with {ok: true} merged.
-spec json_ok(StatusCode :: non_neg_integer(), Result :: map(), Req :: cowboy_req:req()) -> {ok, cowboy_req:req(), []}.
Send OK response with custom status code and {ok: true} merged.
-spec json_reply(StatusCode :: non_neg_integer(), Body :: map(), Req :: cowboy_req:req()) -> {ok, cowboy_req:req(), []}.
Send a JSON response (alias for json_response/3).
-spec json_response(StatusCode :: non_neg_integer(), Body :: map(), Req :: cowboy_req:req()) -> {ok, cowboy_req:req(), []}.
Send a JSON response with status code. Increments the api_requests metric. Sanitizes undefined to null.
-spec method_not_allowed(Req :: cowboy_req:req()) -> {ok, cowboy_req:req(), []}.
405 Method Not Allowed.
-spec not_found(Req :: cowboy_req:req()) -> {ok, cowboy_req:req(), []}.
404 Not Found.
-spec read_json_body(Req :: cowboy_req:req()) -> {ok, map(), cowboy_req:req()} | {error, invalid_json, cowboy_req:req()}.
Read and decode JSON body from request.
Recursively sanitize data for JSON encoding. Converts atom undefined to null so json:encode/1 produces JSON null instead of the string "undefined".