userver: server::handlers::HttpHandlerJsonBase Class Reference
Loading...
Searching...
No Matches
server::handlers::HttpHandlerJsonBase Class Referenceabstract

#include <userver/server/handlers/http_handler_json_base.hpp>

Detailed Description

Convenient base for handlers that accept requests with body in JSON format and respond with body in JSON format.

Example usage:

struct ConfigDataWithTimestamp {
std::chrono::system_clock::time_point updated_at;
std::unordered_map<std::string, formats::json::Value> key_values;
};
class ConfigDistributor final : public server::handlers::HttpHandlerJsonBase {
public:
static constexpr std::string_view kName = "handler-config";
using KeyValues = std::unordered_map<std::string, formats::json::Value>;
// Component is valid after construction and is able to accept requests
ConfigDistributor(const components::ComponentConfig& config,
formats::json::Value HandleRequestJsonThrow(
void SetNewValues(KeyValues&& key_values) {
config_values_.Assign(ConfigDataWithTimestamp{
/*.updated_at=*/utils::datetime::Now(),
/*.key_values=*/std::move(key_values),
});
}
private:
};
Examples
samples/config_service/config_service.cpp, samples/testsuite-support/src/metrics.cpp, and samples/testsuite-support/src/metrics.hpp.

Definition at line 25 of file http_handler_json_base.hpp.

+ Inheritance diagram for server::handlers::HttpHandlerJsonBase:
+ Collaboration diagram for server::handlers::HttpHandlerJsonBase:

Public Member Functions

 HttpHandlerJsonBase (const components::ComponentConfig &config, const components::ComponentContext &component_context, bool is_monitor=false)
 
std::string HandleRequestThrow (const http::HttpRequest &request, request::RequestContext &context) const final
 
virtual formats::json::Value HandleRequestJsonThrow (const http::HttpRequest &request, const formats::json::Value &request_json, request::RequestContext &context) const =0
 
- Public Member Functions inherited from server::handlers::HttpHandlerBase
 HttpHandlerBase (const components::ComponentConfig &config, const components::ComponentContext &component_context, bool is_monitor=false)
 
void HandleRequest (request::RequestBase &request, request::RequestContext &context) const override
 
void ReportMalformedRequest (request::RequestBase &request) const final
 
virtual const std::string & HandlerName () const
 
const std::vector< http::HttpMethod > & GetAllowedMethods () const
 
virtual logging::Level GetLogLevelForResponseStatus (http::HttpStatus status) const
 
std::string GetResponseDataForLoggingChecked (const http::HttpRequest &request, request::RequestContext &context, const std::string &response_data) const
 
void HandleCustomHandlerException (const http::HttpRequest &request, const CustomHandlerException &ex) const
 
void HandleUnknownException (const http::HttpRequest &request, const std::exception &ex) const
 
void LogUnknownException (const std::exception &ex) const
 Helper function to log an unknown exception.
 
const std::optional< logging::Level > & GetLogLevel () const
 Returns the default log level for the handler.
 
- Public Member Functions inherited from server::handlers::HandlerBase
 HandlerBase (const components::ComponentConfig &config, const components::ComponentContext &component_context, bool is_monitor=false)
 
bool IsMonitor () const
 Returns whether this is a monitoring handler.
 
const HandlerConfigGetConfig () const
 Returns handler config.
 
- Public Member Functions inherited from components::LoggableComponentBase
 LoggableComponentBase (const ComponentConfig &, const ComponentContext &)
 
 LoggableComponentBase (LoggableComponentBase &&)=delete
 
 LoggableComponentBase (const LoggableComponentBase &)=delete
 
 ~LoggableComponentBase () override=default
 
ComponentHealth GetComponentHealth () const override
 
void OnLoadingCancelled () override
 
void OnAllComponentsLoaded () override
 
void OnAllComponentsAreStopping () override
 

Static Public Member Functions

static yaml_config::Schema GetStaticConfigSchema ()
 
- Static Public Member Functions inherited from server::handlers::HttpHandlerBase
static yaml_config::Schema GetStaticConfigSchema ()
 
- Static Public Member Functions inherited from server::handlers::HandlerBase
static yaml_config::Schema GetStaticConfigSchema ()
 
- Static Public Member Functions inherited from components::LoggableComponentBase
static yaml_config::Schema GetStaticConfigSchema ()
 

Protected Member Functions

void ParseRequestData (const http::HttpRequest &request, request::RequestContext &context) const override
 
- Protected Member Functions inherited from server::handlers::HttpHandlerBase
void ThrowUnsupportedHttpMethod (const http::HttpRequest &request) const
 
virtual void OnRequestCompleteThrow (const http::HttpRequest &, request::RequestContext &) const
 
virtual void HandleStreamRequest (const server::http::HttpRequest &, server::request::RequestContext &, server::http::ResponseBodyStream &) const
 
virtual bool IsStreamed () const
 
virtual bool IsMethodStatisticIncluded () const
 
virtual bool NeedCheckAuth () const
 
virtual std::string GetRequestBodyForLogging (const http::HttpRequest &request, request::RequestContext &context, const std::string &request_body) const
 Override it if you need a custom request body logging.
 
virtual std::string GetResponseDataForLogging (const http::HttpRequest &request, request::RequestContext &context, const std::string &response_data) const
 Override it if you need a custom response data logging.
 
virtual std::string GetMetaType (const http::HttpRequest &) const
 

Static Protected Member Functions

static const formats::json::ValueGetRequestJson (const request::RequestContext &context)
 
static const formats::json::ValueGetResponseJson (const request::RequestContext &context)
 

Additional Inherited Members

- Protected Types inherited from server::handlers::HandlerBase
using HandlerErrorCode = handlers::HandlerErrorCode
 
using InternalMessage = handlers::InternalMessage
 
using ExternalBody = handlers::ExternalBody
 
using ClientError = handlers::ClientError
 
using InternalServerError = handlers::InternalServerError
 

Member Function Documentation

◆ GetRequestJson()

static const formats::json::Value * server::handlers::HttpHandlerJsonBase::GetRequestJson ( const request::RequestContext & context)
staticprotected
Returns
A pointer to json request if it was parsed successfully or nullptr otherwise.

◆ GetResponseJson()

static const formats::json::Value * server::handlers::HttpHandlerJsonBase::GetResponseJson ( const request::RequestContext & context)
staticprotected
Returns
a pointer to json response if it was returned successfully by HandleRequestJsonThrow() or nullptr otherwise.

◆ HandleRequestThrow()

std::string server::handlers::HttpHandlerJsonBase::HandleRequestThrow ( const http::HttpRequest & request,
request::RequestContext & context ) const
finalvirtual

The core method for HTTP request handling. request arg contains HTTP headers, full body, etc. The method should return response body.

Note
It is used only if IsStreamed() returned false.

Reimplemented from server::handlers::HttpHandlerBase.

◆ ParseRequestData()

void server::handlers::HttpHandlerJsonBase::ParseRequestData ( const http::HttpRequest & ,
request::RequestContext &  ) const
overrideprotectedvirtual

For internal use. You don't need to override it. This method is overridden in format-specific base handlers.

Reimplemented from server::handlers::HttpHandlerBase.


The documentation for this class was generated from the following file: