1#include <userver/utest/using_namespace_userver.hpp>
3#include <userver/easy.hpp>
4#include <userver/yaml_config/merge_schemas.hpp>
6#include <userver/clients/http/component.hpp>
7#include <userver/components/component_context.hpp>
12 static constexpr std::string_view kName =
"action-client";
14 ActionClient(
const components::ComponentConfig& config,
const components::ComponentContext& context)
16 service_url_(config
["service-url"].As<std
::string
>()),
20 auto CreateHttpRequest(std::string action)
const {
24 static yaml_config::Schema GetStaticConfigSchema() {
26 type: object
27 description: My dependencies schema
28 additionalProperties: false
29 properties:
30 service-url:
31 type: string
32 description: URL of the service to send the actions to
37 const std::string service_url_;
38 clients::http::
Client& http_client_;
45 explicit ActionDep(
const components::ComponentContext& config)
48 auto CreateActionRequest(std::string action)
const {
return component_.CreateHttpRequest(std::move(action)); }
50 static void RegisterOn(
easy::HttpBase& app) {
51 app
.TryAddComponent<ActionClient
>(ActionClient::kName
, "service-url: http://some-service.example/v1/action");
56 ActionClient& component_;
61int main(
int argc,
char* argv[]) {
64 easy::HttpWith<Deps>(argc, argv)
66 .Post("/log", [](
const server::
http::HttpRequest& req,
const Deps& deps) {
67 const auto& action = req
.GetArg("action");
70 "INSERT INTO events_table(action) VALUES($1)",
73 return deps.CreateActionRequest(action)->
body();