Skip to content

Commit cd77cbd

Browse files
committed
feat chaotic: fixes for clients
commit_hash:30a607a5023518ae57d881a396c51897f18bf023
1 parent c86d1ed commit cd77cbd

38 files changed

Lines changed: 374 additions & 504 deletions

.mapping.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@
8989
"chaotic-openapi/include/userver/chaotic/openapi/client/exceptions.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/client/exceptions.hpp",
9090
"chaotic-openapi/include/userver/chaotic/openapi/client/middleware.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/client/middleware.hpp",
9191
"chaotic-openapi/include/userver/chaotic/openapi/client/middleware_factory.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/client/middleware_factory.hpp",
92-
"chaotic-openapi/include/userver/chaotic/openapi/client/middleware_registry.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/client/middleware_registry.hpp",
9392
"chaotic-openapi/include/userver/chaotic/openapi/form.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/form.hpp",
93+
"chaotic-openapi/include/userver/chaotic/openapi/middlewares/component_list.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/middlewares/component_list.hpp",
9494
"chaotic-openapi/include/userver/chaotic/openapi/middlewares/follow_redirects_middleware.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/middlewares/follow_redirects_middleware.hpp",
9595
"chaotic-openapi/include/userver/chaotic/openapi/middlewares/logging_middleware.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/middlewares/logging_middleware.hpp",
9696
"chaotic-openapi/include/userver/chaotic/openapi/middlewares/manager.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/middlewares/manager.hpp",
9797
"chaotic-openapi/include/userver/chaotic/openapi/middlewares/proxy_middleware.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/middlewares/proxy_middleware.hpp",
98+
"chaotic-openapi/include/userver/chaotic/openapi/middlewares/qos_middleware.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/middlewares/qos_middleware.hpp",
9899
"chaotic-openapi/include/userver/chaotic/openapi/middlewares/ssl_middleware.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/middlewares/ssl_middleware.hpp",
99-
"chaotic-openapi/include/userver/chaotic/openapi/middlewares/timeout_retry_middleware.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/middlewares/timeout_retry_middleware.hpp",
100100
"chaotic-openapi/include/userver/chaotic/openapi/parameters.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/parameters.hpp",
101101
"chaotic-openapi/include/userver/chaotic/openapi/parameters_read.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/parameters_read.hpp",
102102
"chaotic-openapi/include/userver/chaotic/openapi/parameters_write.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/parameters_write.hpp",
@@ -116,14 +116,14 @@
116116
"chaotic-openapi/src/chaotic/openapi/client/exceptions.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/client/exceptions.cpp",
117117
"chaotic-openapi/src/chaotic/openapi/client/middleware.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/client/middleware.cpp",
118118
"chaotic-openapi/src/chaotic/openapi/client/middleware_factory.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/client/middleware_factory.cpp",
119-
"chaotic-openapi/src/chaotic/openapi/client/middleware_registry.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/client/middleware_registry.cpp",
120119
"chaotic-openapi/src/chaotic/openapi/form.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/form.cpp",
120+
"chaotic-openapi/src/chaotic/openapi/middlewares/component_list.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/middlewares/component_list.cpp",
121121
"chaotic-openapi/src/chaotic/openapi/middlewares/follow_redirects_middleware.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/middlewares/follow_redirects_middleware.cpp",
122122
"chaotic-openapi/src/chaotic/openapi/middlewares/logging_middleware.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/middlewares/logging_middleware.cpp",
123123
"chaotic-openapi/src/chaotic/openapi/middlewares/manager.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/middlewares/manager.cpp",
124124
"chaotic-openapi/src/chaotic/openapi/middlewares/proxy_middleware.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/middlewares/proxy_middleware.cpp",
125+
"chaotic-openapi/src/chaotic/openapi/middlewares/qos_middleware.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/middlewares/qos_middleware.cpp",
125126
"chaotic-openapi/src/chaotic/openapi/middlewares/ssl_middleware.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/middlewares/ssl_middleware.cpp",
126-
"chaotic-openapi/src/chaotic/openapi/middlewares/timeout_retry_middleware.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/middlewares/timeout_retry_middleware.cpp",
127127
"chaotic-openapi/src/chaotic/openapi/middlewares_test.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/middlewares_test.cpp",
128128
"chaotic-openapi/src/chaotic/openapi/parameters.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/parameters.cpp",
129129
"chaotic-openapi/src/chaotic/openapi/parameters_read.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/parameters_read.cpp",
@@ -3781,6 +3781,7 @@
37813781
"scripts/docs/en/userver/caches.md":"taxi/uservices/userver/scripts/docs/en/userver/caches.md",
37823782
"scripts/docs/en/userver/chaos_testing.md":"taxi/uservices/userver/scripts/docs/en/userver/chaos_testing.md",
37833783
"scripts/docs/en/userver/chaotic.md":"taxi/uservices/userver/scripts/docs/en/userver/chaotic.md",
3784+
"scripts/docs/en/userver/chaotic_clients.md":"taxi/uservices/userver/scripts/docs/en/userver/chaotic_clients.md",
37843785
"scripts/docs/en/userver/codegen_overview.md":"taxi/uservices/userver/scripts/docs/en/userver/codegen_overview.md",
37853786
"scripts/docs/en/userver/component_system.md":"taxi/uservices/userver/scripts/docs/en/userver/component_system.md",
37863787
"scripts/docs/en/userver/congestion_control.md":"taxi/uservices/userver/scripts/docs/en/userver/congestion_control.md",

chaotic-openapi/chaotic_openapi/back/cpp_client/templates/client_impl.cpp.jinja

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include <client/{{ name }}/client_impl.hpp>
22

3-
#include <userver/chaotic/openapi/client/middleware_registry.hpp>
4-
#include <userver/chaotic/openapi/middlewares/follow_redirects_middleware.hpp>
5-
#include <userver/chaotic/openapi/middlewares/timeout_retry_middleware.hpp>
63
#include <userver/components/component_context.hpp>
74
#include <userver/components/component_base.hpp>
85
#include <userver/yaml_config/merge_schemas.hpp>
@@ -15,52 +12,6 @@ ClientImpl::ClientImpl(const USERVER_NAMESPACE::chaotic::openapi::client::Config
1512
{
1613
}
1714

18-
USERVER_NAMESPACE::yaml_config::Schema ClientImpl::GetStaticConfigSchema() {
19-
std::string base_schema = R"(
20-
type: object
21-
description: OpenAPI HTTP client with middlewares
22-
additionalProperties: false
23-
properties:
24-
base-url:
25-
type: string
26-
description: Base URL for the API
27-
timeout-ms:
28-
type: integer
29-
description: Request timeout in milliseconds
30-
minimum: 1
31-
attempts:
32-
type: integer
33-
description: Maximum number of retry attempts
34-
minimum: 1
35-
middlewares:
36-
type: object
37-
description: Middleware configurations
38-
additionalProperties: false
39-
properties:
40-
)";
41-
42-
const auto& registry = USERVER_NAMESPACE::chaotic::openapi::client::MiddlewareRegistry::Instance();
43-
const auto& factories = registry.GetFactories();
44-
45-
std::string middlewares_yaml = "";
46-
for (const auto& [name, factory] : factories) {
47-
auto middleware_schema = factory->GetStaticConfigSchemaStr();
48-
middlewares_yaml += " " + name + ":\n";
49-
50-
std::string indented_yaml = "";
51-
std::string line;
52-
std::istringstream schema_stream(middleware_schema);
53-
while (std::getline(schema_stream, line)) {
54-
indented_yaml += " " + line + "\n";
55-
}
56-
middlewares_yaml += indented_yaml;
57-
}
58-
59-
std::string combined_schema = base_schema + middlewares_yaml;
60-
61-
return USERVER_NAMESPACE::yaml_config::MergeSchemas<USERVER_NAMESPACE::components::LoggableComponentBase>(combined_schema);
62-
}
63-
6415
{% for op in operations %}
6516
{% if op.client_generate %}
6617
{{ op.cpp_namespace() }}::Response ClientImpl::{{ op.cpp_method_name() }}(
@@ -70,33 +21,12 @@ properties:
7021
const USERVER_NAMESPACE::chaotic::openapi::client::CommandControl& command_control
7122
) {
7223
auto r = http_client_.CreateRequest();
73-
r.url(config_.base_url + "{{ op.path }}");
24+
ApplyConfig(r, command_control, config_);
7425

7526
{% if not op.empty_request() -%}
76-
SerializeRequest(request, r);
27+
SerializeRequest(request, config_.base_url, r);
7728
{% endif %}
7829

79-
if (command_control) {
80-
auto it = middlewares_.find("timeout_retry");
81-
if (it != middlewares_.end() && (command_control.timeout.count() > 0 || command_control.attempts > 0)) {
82-
auto timeout_retry = std::dynamic_pointer_cast<USERVER_NAMESPACE::chaotic::openapi::TimeoutRetryMiddleware>(it->second);
83-
if (timeout_retry) {
84-
timeout_retry->ApplyCommandControl(
85-
command_control.timeout.count() > 0 ? command_control.timeout : config_.timeout,
86-
command_control.attempts > 0 ? command_control.attempts : config_.attempts
87-
);
88-
}
89-
}
90-
91-
it = middlewares_.find("follow_redirects");
92-
if (it != middlewares_.end() && command_control.follow_redirects) {
93-
auto follow_redirects = std::dynamic_pointer_cast<USERVER_NAMESPACE::chaotic::openapi::FollowRedirectsMiddleware>(it->second);
94-
if (follow_redirects) {
95-
follow_redirects->ApplyFollowRedirects(*command_control.follow_redirects);
96-
}
97-
}
98-
}
99-
10030
middleware_manager_.ProcessRequest(r);
10131

10232
std::shared_ptr<USERVER_NAMESPACE::clients::http::Response> response;
Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <client/{{ name }}/component.hpp>
22

33
#include <userver/chaotic/openapi/client/config.hpp>
4-
#include <userver/chaotic/openapi/client/middleware_registry.hpp>
4+
#include <userver/chaotic/openapi/client/middleware_factory.hpp>
55
#include <userver/components/component_context.hpp>
66
#include <userver/clients/http/component.hpp>
7+
#include <userver/yaml_config/merge_schemas.hpp>
78

89
namespace {{ namespace }} {
910

@@ -16,14 +17,11 @@ Component::Component(const USERVER_NAMESPACE::components::ComponentConfig& confi
1617
{
1718
if (config.HasMember("middlewares")) {
1819
const auto& mw_config = config["middlewares"];
19-
const auto& registry = USERVER_NAMESPACE::chaotic::openapi::client::MiddlewareRegistry::Instance();
20-
const auto& factories = registry.GetFactories();
2120

22-
for (const auto& [name, factory] : factories) {
23-
if (mw_config.HasMember(name)) {
24-
auto middleware = factory->Create(mw_config[name]);
25-
client_.RegisterMiddleware(middleware);
26-
}
21+
for (const auto& [name, config] : Items(mw_config)) {
22+
auto &factory = context.FindComponent<chaotic::openapi::client::MiddlewareFactory>("chaotic-client-middleware-" + name);
23+
auto middleware = factory.Create(config);
24+
client_.RegisterMiddleware(middleware);
2725
}
2826
}
2927
}
@@ -33,4 +31,32 @@ Client& Component::GetClient()
3331
return client_;
3432
}
3533

34+
USERVER_NAMESPACE::yaml_config::Schema Component::GetStaticConfigSchema() {
35+
std::string base_schema = R"(
36+
type: object
37+
description: OpenAPI HTTP client with middlewares
38+
additionalProperties: false
39+
properties:
40+
base-url:
41+
type: string
42+
description: Base URL for the API
43+
timeout-ms:
44+
type: integer
45+
description: Request timeout in milliseconds
46+
minimum: 1
47+
attempts:
48+
type: integer
49+
description: Maximum number of retry attempts
50+
minimum: 1
51+
middlewares:
52+
type: object
53+
description: Middlewares configuration
54+
additionalProperties: true
55+
properties: {}
56+
)";
57+
58+
return USERVER_NAMESPACE::yaml_config::MergeSchemas<USERVER_NAMESPACE::components::ComponentBase>(base_schema);
59+
60+
}
61+
3662
}

chaotic-openapi/chaotic_openapi/back/cpp_client/templates/component.hpp.jinja

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public:
1515

1616
Client& GetClient();
1717

18-
static USERVER_NAMESPACE::yaml_config::Schema GetStaticConfigSchema() {
19-
return ClientImpl::GetStaticConfigSchema();
20-
}
18+
static yaml_config::Schema GetStaticConfigSchema();
2119

2220
private:
2321
ClientImpl client_;

chaotic-openapi/chaotic_openapi/back/cpp_client/templates/requests.cpp.jinja

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ namespace openapi = USERVER_NAMESPACE::chaotic::openapi;
2525
static constexpr openapi::Name k{{ parameter.cpp_name }} = "{{ parameter.raw_name }}";
2626
{% endfor %}
2727

28-
void SerializeRequest(const Request& request, USERVER_NAMESPACE::clients::http::Request& http_request)
28+
void SerializeRequest(const Request& request, const std::string& base_url, USERVER_NAMESPACE::clients::http::Request& http_request)
2929
{
3030
{# parameters #}
3131
openapi::ParameterSinkHttpClient sink(
3232
http_request,
33-
"{{ op.path }}"
33+
base_url + "{{ op.path }}"
3434
);
3535
{% for parameter in op.parameters %}
3636
{% if parameter.required %}
@@ -69,6 +69,8 @@ void SerializeRequest(const Request& request, USERVER_NAMESPACE::clients::http::
6969
UASSERT(false);
7070
}
7171
{% endif %}
72+
73+
sink.Flush();
7274
}
7375

7476
} // namespace

chaotic-openapi/chaotic_openapi/back/cpp_client/templates/requests.hpp.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace {{ namespace }} {
6060
{% endif %}
6161
};
6262

63-
void SerializeRequest(const Request& request, USERVER_NAMESPACE::clients::http::Request& http_request);
63+
void SerializeRequest(const Request& request, const std::string& base_url, USERVER_NAMESPACE::clients::http::Request& http_request);
6464
}
6565
{%- endif %}
6666
{% endif %}

chaotic-openapi/golden_tests/output/src/client/test/client_impl.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/* THIS FILE IS AUTOGENERATED, DON'T EDIT! */
22
#include <client/test/client_impl.hpp>
33

4-
#include <userver/chaotic/openapi/client/middleware_registry.hpp>
5-
#include <userver/chaotic/openapi/middlewares/follow_redirects_middleware.hpp>
6-
#include <userver/chaotic/openapi/middlewares/timeout_retry_middleware.hpp>
74
#include <userver/components/component_base.hpp>
85
#include <userver/components/component_context.hpp>
96
#include <userver/yaml_config/merge_schemas.hpp>
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <chrono>
4-
#include <optional>
54

65
#include <userver/chaotic/openapi/client/config.hpp>
76

@@ -12,11 +11,10 @@ namespace chaotic::openapi::client {
1211
struct CommandControl {
1312
std::chrono::milliseconds timeout{};
1413
int attempts{};
15-
std::optional<bool> follow_redirects{};
16-
17-
explicit operator bool() const { return timeout.count() != 0 || attempts != 0 || follow_redirects.has_value(); }
1814
};
1915

16+
void ApplyConfig(clients::http::Request& request, const CommandControl& cc, const Config& config);
17+
2018
} // namespace chaotic::openapi::client
2119

2220
USERVER_NAMESPACE_END

chaotic-openapi/include/userver/chaotic/openapi/client/middleware_factory.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
#include <memory>
44
#include <userver/chaotic/openapi/client/middleware.hpp>
5+
#include <userver/components/component_base.hpp>
56
#include <userver/components/component_config.hpp>
67
#include <userver/yaml_config/schema.hpp>
78

89
USERVER_NAMESPACE_BEGIN
910

1011
namespace chaotic::openapi::client {
1112

12-
class MiddlewareFactory {
13+
class MiddlewareFactory : public components::ComponentBase {
1314
public:
15+
using components::ComponentBase::ComponentBase;
16+
1417
virtual ~MiddlewareFactory() = default;
1518

1619
virtual std::shared_ptr<Middleware> Create(const USERVER_NAMESPACE::yaml_config::YamlConfig& config) = 0;

chaotic-openapi/include/userver/chaotic/openapi/client/middleware_registry.hpp

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)