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/config_service.cpp Config service sample - component
22
23// clang-format on
24
26public:
27 HttpHandlerJsonBase(
28 const components::ComponentConfig& config,
29 const components::ComponentContext& component_context,
30 bool is_monitor = false
31 );
32
33 std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext& context) const final;
34
35 virtual formats::json::Value HandleRequestJsonThrow(
36 const http::HttpRequest& request,
37 const formats::json::Value& request_json,
38 request::RequestContext& context
39 ) const = 0;
40
41 static yaml_config::Schema GetStaticConfigSchema();
42
43protected:
44 /// @returns A pointer to json request if it was parsed successfully or
45 /// nullptr otherwise.
46 static const formats::json::Value* GetRequestJson(const request::RequestContext& context);
47
48 /// @returns a pointer to json response if it was returned successfully by
49 /// `HandleRequestJsonThrow()` or nullptr otherwise.
50 static const formats::json::Value* GetResponseJson(const request::RequestContext& context);
51
52 void ParseRequestData(const http::HttpRequest& request, request::RequestContext& context) const override;
53
54private:
55 FormattedErrorData GetFormattedExternalErrorBody(const CustomHandlerException& exc) const final;
56};
57
58} // namespace server::handlers
59
60template <>
61inline constexpr bool components::kHasValidate<server::handlers::HttpHandlerJsonBase> = true;
62
63USERVER_NAMESPACE_END