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>
9constexpr std::string_view kSchema = R"~(
10CREATE TABLE IF NOT EXISTS events_table (
11 id serial NOT NULL,
12 action VARCHAR PRIMARY KEY
13)
14)~";
19 static constexpr std::string_view kName =
"action-client";
21 ActionClient(
const components::ComponentConfig& config,
const components::ComponentContext& context)
23 service_url_(config
["service-url"].As<std
::string
>()),
27 auto CreateHttpRequest(std::string action)
const {
31 static yaml_config::Schema GetStaticConfigSchema() {
33 type: object
34 description: My dependencies schema
35 additionalProperties: false
36 properties:
37 service-url:
38 type: string
39 description: URL of the service to send the actions to
44 const std::string service_url_;
45 clients::http::
Client& http_client_;
52 explicit ActionDep(
const components::ComponentContext& config)
55 auto CreateActionRequest(std::string action)
const {
return component_.CreateHttpRequest(std::move(action)); }
57 static void RegisterOn(
easy::HttpBase& app) {
58 app
.TryAddComponent<ActionClient
>(ActionClient::kName
, "service-url: http://some-service.example/v1/action");
63 ActionClient& component_;
68int main(
int argc,
char* argv[]) {
71 easy::HttpWith<Deps>(argc, argv)
74 .Post("/log", [](
const server::
http::HttpRequest& req,
const Deps& deps) {
75 const auto& action = req
.GetArg("action");
78 "INSERT INTO events_table(action) VALUES($1)",
81 return deps.CreateActionRequest(action)->
body();