CCF
Loading...
Searching...
No Matches
json_handler.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6
7#include <llhttp/llhttp.h>
8#include <variant>
9
10namespace ccf
11{
12 /*
13 * For simple app methods which expect a JSON request these functions do the
14 * common decoding of the input and setting of response fields, to reduce
15 * handler complexity and repetition.
16 *
17 * Rather than:
18 * auto foo = [](auto& ctx) {
19 * nlohmann::json params;
20 * if (<content-type is JSON>)
21 * {
22 * params = nlohmann::json::parse(ctx.rpc_ctx->get_request_body());
23 * }
24 * else
25 * {
26 * ...
27 * }
28 * auto result = fn(params);
29 * if (is_error(result))
30 * {
31 * ctx.rpc_ctx->set_response_status(SOME_ERROR);
32 * ctx.rpc_ctx->set_response_header(content_type, Text);
33 * ctx.rpc_ctx->set_response_body(error_msg(result));
34 * }
35 * ctx.rpc_ctx->set_response_header(content_type, JSON);
36 * ctx.rpc_ctx->set_response_body(result.dump());
37 * };
38 *
39 * it is possible to write the shorter, clearer, return-based lambda:
40 * auto foo = json_adapter([](auto& ctx, nlohmann::json&& params)
41 * {
42 * auto result = fn(params);
43 * if (is_error(result))
44 * {
45 * return make_error(SOME_ERROR, error_msg(result));
46 * }
47 * else
48 * {
49 * return make_success(result);
50 * }
51 * });
52 */
53 namespace jsonhandler
54 {
56 std::variant<ErrorDetails, RedirectDetails, nlohmann::json>;
57
58 nlohmann::json get_json_params(const std::shared_ptr<ccf::RpcContext>& ctx);
59
60 void set_response(
61 JsonAdapterResponse&& res, std::shared_ptr<ccf::RpcContext>& ctx);
62 }
63
66 nlohmann::json&& result_payload);
68 const nlohmann::json& result_payload);
69
71 ccf::http_status status, const std::string& code, const std::string& msg);
72
74
77 endpoints::EndpointContext& ctx, nlohmann::json&& params)>;
80
83 endpoints::ReadOnlyEndpointContext& ctx, nlohmann::json&& params)>;
86
88 endpoints::CommandEndpointContext& ctx, nlohmann::json&& params)>;
90 const CommandHandlerWithJson& f);
91}
std::function< void(CommandEndpointContext &args)> CommandEndpointFunction
Definition endpoint_context.h:52
std::function< void(ReadOnlyEndpointContext &args)> ReadOnlyEndpointFunction
Definition endpoint_context.h:80
std::function< void(EndpointContext &args)> EndpointFunction
Definition endpoint_context.h:63
void set_response(JsonAdapterResponse &&res, std::shared_ptr< ccf::RpcContext > &ctx)
Definition json_handler.cpp:37
std::variant< ErrorDetails, RedirectDetails, nlohmann::json > JsonAdapterResponse
Definition json_handler.h:56
nlohmann::json get_json_params(const std::shared_ptr< ccf::RpcContext > &ctx)
Definition json_handler.cpp:19
Definition app_interface.h:14
std::function< jsonhandler::JsonAdapterResponse(endpoints::ReadOnlyEndpointContext &ctx, nlohmann::json &&params)> ReadOnlyHandlerWithJson
Definition json_handler.h:83
std::function< jsonhandler::JsonAdapterResponse(endpoints::CommandEndpointContext &ctx, nlohmann::json &&params)> CommandHandlerWithJson
Definition json_handler.h:88
jsonhandler::JsonAdapterResponse make_redirect(ccf::http_status status)
Definition json_handler.cpp:123
jsonhandler::JsonAdapterResponse make_success()
Definition json_handler.cpp:99
endpoints::CommandEndpointFunction json_command_adapter(const CommandHandlerWithJson &f)
Definition json_handler.cpp:145
std::function< jsonhandler::JsonAdapterResponse(endpoints::EndpointContext &ctx, nlohmann::json &&params)> HandlerJsonParamsAndForward
Definition json_handler.h:77
llhttp_status http_status
Definition http_status.h:9
endpoints::ReadOnlyEndpointFunction json_read_only_adapter(const ReadOnlyHandlerWithJson &f)
Definition json_handler.cpp:136
endpoints::EndpointFunction json_adapter(const HandlerJsonParamsAndForward &f)
Definition json_handler.cpp:128
jsonhandler::JsonAdapterResponse make_error(ccf::http_status status, const std::string &code, const std::string &msg)
Definition json_handler.cpp:115
Definition endpoint_context.h:24
Definition endpoint_context.h:55
Definition endpoint_context.h:70