userver: userver/server/handlers/http_handler_json_base.hpp Source File
Loading...
Searching...
No Matches
http_handler_json_base.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/http_handler_json_base.hpp
4/// @brief @copybrief server::handlers::HttpHandlerJsonBase
5
6#include <userver/server/handlers/http_handler_base.hpp>
7
8USERVER_NAMESPACE_BEGIN
9
10namespace server::handlers {
11
12// clang-format off
13
14/// @ingroup userver_components userver_http_handlers userver_base_classes
15///
16/// @brief Convenient base for handlers that accept requests with body in
17/// JSON format and respond with body in JSON format.
18///
19/// ## Example usage:
20///
21/// @snippet samples/config_service/main.cpp Config service sample - component
22
23// clang-format on
24
26public:
27 using Value = formats::json::Value;
28 using HttpRequest = server::http::HttpRequest;
29 using RequestContext = server::request::RequestContext;
30
31 HttpHandlerJsonBase(
32 const components::ComponentConfig& config,
33 const components::ComponentContext& component_context,
34 bool is_monitor = false
35 );
36
37 std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext& context) const final;
38
39 virtual Value HandleRequestJsonThrow(const HttpRequest& request, const Value& request_json, RequestContext& context)
40 const = 0;
41
42 static yaml_config::Schema GetStaticConfigSchema();
43
44protected:
45 /// @returns A pointer to json request if it was parsed successfully or
46 /// nullptr otherwise.
47 static const formats::json::Value* GetRequestJson(const request::RequestContext& context);
48
49 /// @returns a pointer to json response if it was returned successfully by
50 /// `HandleRequestJsonThrow()` or nullptr otherwise.
51 static const formats::json::Value* GetResponseJson(const request::RequestContext& context);
52
53 void ParseRequestData(const http::HttpRequest& request, request::RequestContext& context) const override;
54
55private:
56 FormattedErrorData GetFormattedExternalErrorBody(const CustomHandlerException& exc) const final;
57};
58
59} // namespace server::handlers
60
61template <>
62inline constexpr bool components::kHasValidate<server::handlers::HttpHandlerJsonBase> = true;
63
64USERVER_NAMESPACE_END