Repository navigation
Expand file tree
/
Copy pathClientGenerator.java
More file actions
184 lines (169 loc) · 7.11 KB
/
Copy pathClientGenerator.java
File metadata and controls
184 lines (169 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package io.smithycpp.codegen;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.DocumentationTrait;
/** Emits client.h / src/client.cc: the <Service>Client class over the protocol generator. */
final class ClientGenerator {
private final CppContext context;
private final ServiceShape service;
private final ProtocolGenerator protocol;
ClientGenerator(CppContext context, ServiceShape service, ProtocolGenerator protocol) {
this.context = context;
this.service = service;
this.protocol = protocol;
}
List<OperationShape> operations() {
List<OperationShape> operations =
new ArrayList<>(TopDownIndex.of(context.model()).getContainedOperations(service));
operations.sort(Comparator.comparing(OperationShape::getId));
return operations;
}
private String clientName() {
return CppReservedWords.escape(service.getId().getName()) + "Client";
}
void run() {
context
.writerDelegator()
.useFileWriter(context.settings().clientHeaderFile(), this::writeHeader);
context.writerDelegator().useFileWriter("src/client.cc", this::writeSource);
}
private void writeHeader(CppWriter w) {
w.addInclude("<memory>");
w.addInclude("<string>");
w.addInclude("\"" + context.settings().includePrefix() + "/types.h\"");
w.addInclude("\"smithy/client/config.h\"");
w.addInclude("\"smithy/core/outcome.h\"");
w.addInclude("\"smithy/http/transport.h\"");
String name = clientName();
w.write("/// $L client for $L.", protocol.name(), service.getId().toString());
w.write("/// Modeled service errors surface as smithy::Error with kind kModeled,");
w.write("/// code() set to the error shape name, and the deserialized error");
w.write("/// structure attached: error.detail<TheErrorShape>().");
w.openBlock("class $L {", name);
w.write("public:").indent();
w.write("/// Fails when the endpoint cannot be parsed and no transport is injected.");
w.write("static smithy::Outcome<$L> Create(smithy::ClientConfig config);", name);
w.write("");
for (OperationShape operation : operations()) {
operation
.getTrait(DocumentationTrait.class)
.ifPresent(
docs -> {
for (String line : docs.getValue().split("\n", -1)) {
w.write("/// $L", line);
}
});
StructureShape input = ProtocolSupport.inputShape(context, operation);
String inputType = context.cppSymbols().toSymbol(input).getName();
String outputType =
context.cppSymbols().toSymbol(ProtocolSupport.outputShape(context, operation)).getName();
String defaulted = input.members().isEmpty() ? " = {}" : "";
w.write(
"smithy::Outcome<$L> $L(const $L& input$L) const;",
outputType,
CppReservedWords.escape(operation.getId().getName()),
inputType,
defaulted);
}
w.write("").dedent();
w.write("private:").indent();
w.write(
"$L(smithy::ClientConfig config, std::shared_ptr<smithy::http::HttpClient> "
+ "transport, std::string path_prefix);",
name);
w.write(
"smithy::Outcome<smithy::http::HttpResponse> "
+ "Send(smithy::http::HttpRequest request) const;");
w.write("");
w.write("smithy::ClientConfig config_;");
w.write("std::shared_ptr<smithy::http::HttpClient> transport_;");
w.write("std::string path_prefix_;").dedent();
w.closeBlock("};");
w.write("");
}
private void writeSource(CppWriter w) {
for (String include : ProtocolSupport.sharedClientIncludes(context)) {
w.addInclude(include);
}
for (String include : protocol.clientIncludes()) {
w.addInclude(include);
}
String name = clientName();
w.write("namespace {");
w.write("");
protocol.writeClientHelpers(w, context);
ProtocolSupport.writeOperationErrorDeserializers(w, context, service, protocol, operations());
w.write("} // namespace");
w.write("");
w.openBlock("smithy::Outcome<$L> $L::Create(smithy::ClientConfig config) {", name, name);
w.write("std::shared_ptr<smithy::http::HttpClient> transport = config.http_client;");
w.write("std::string prefix;");
w.openBlock("if (!config.endpoint.empty()) {");
w.write("auto endpoint = smithy::http::ParseEndpoint(config.endpoint);");
w.write("if (!endpoint) return std::move(endpoint).error();");
w.write("prefix = endpoint->path_prefix;");
w.openBlock("if (transport == nullptr) {");
w.write(
"transport = std::make_shared<smithy::http::SocketHttpClient>(endpoint->host, "
+ "endpoint->port, config.request_timeout_ms);");
w.closeBlock("}");
w.closeBlock("}");
w.openBlock("if (transport == nullptr) {");
w.write(
"return smithy::Error::Validation($S);",
name + ": config needs an endpoint or an http_client");
w.closeBlock("}");
w.write("return $L(std::move(config), std::move(transport), std::move(prefix));", name);
w.closeBlock("}");
w.write("");
w.openBlock(
"$L::$L(smithy::ClientConfig config, "
+ "std::shared_ptr<smithy::http::HttpClient> transport, std::string path_prefix)",
name,
name);
w.write(": config_(std::move(config)),");
w.write(" transport_(std::move(transport)),");
w.write(" path_prefix_(std::move(path_prefix)) {}");
w.dedent();
w.write("");
w.openBlock(
"smithy::Outcome<smithy::http::HttpResponse> $L::Send("
+ "smithy::http::HttpRequest request) const {",
name);
w.write("// Operations with a non-document response payload set their own accept.");
w.write(
"if (!request.headers.Get(\"accept\").has_value()) "
+ "request.headers.Set(\"accept\", $S);",
protocol.contentType());
w.write("request.headers.Set(\"user-agent\", config_.user_agent);");
w.openBlock("if (!request.body.empty()) {");
w.write("request.headers.Set(\"content-length\", std::to_string(request.body.size()));");
w.closeBlock("}");
w.write("return smithy::SendWithRetries(*transport_, request, config_.retry);");
w.closeBlock("}");
w.write("");
for (OperationShape operation : operations()) {
StructureShape input = ProtocolSupport.inputShape(context, operation);
String inputType = context.cppSymbols().toSymbol(input).getName();
String outputType =
context.cppSymbols().toSymbol(ProtocolSupport.outputShape(context, operation)).getName();
w.openBlock(
"smithy::Outcome<$L> $L::$L(const $L& input) const {",
outputType,
name,
CppReservedWords.escape(operation.getId().getName()),
inputType);
if (input.members().isEmpty()) {
w.write("(void)input;");
}
protocol.writeOperationBody(w, context, service, operation);
w.closeBlock("}");
w.write("");
}
}
}