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