/
AwsEc2.java
173 lines (149 loc) · 6.63 KB
/
AwsEc2.java
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
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.smithy.aws.typescript.codegen;
import java.util.Set;
import software.amazon.smithy.aws.traits.protocols.Ec2QueryTrait;
import software.amazon.smithy.codegen.core.SymbolReference;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.integration.HttpRpcProtocolGenerator;
import software.amazon.smithy.utils.SmithyInternalApi;
/**
* Handles generating the aws.ec2 protocol for services. It handles reading and
* writing from document bodies, including generating any functions needed for
* performing serde.
*
* This builds on the foundations of the {@link HttpRpcProtocolGenerator} to handle
* standard components of the HTTP requests and responses.
*
* @see Ec2ShapeSerVisitor
* @see XmlShapeDeserVisitor
* @see QueryMemberSerVisitor
* @see XmlMemberDeserVisitor
* @see AwsProtocolUtils
* @see <a href="https://awslabs.github.io/smithy/spec/xml.html">Smithy XML traits.</a>
* @see <a href="https://awslabs.github.io/smithy/spec/aws-core.html#ec2QueryName-trait">Smithy EC2 Query Name trait.</a>
*/
@SmithyInternalApi
final class AwsEc2 extends HttpRpcProtocolGenerator {
AwsEc2() {
super(true);
}
@Override
protected String getOperationPath(GenerationContext context, OperationShape operationShape) {
return "/";
}
@Override
public ShapeId getProtocol() {
return Ec2QueryTrait.ID;
}
@Override
public String getName() {
return "aws.ec2";
}
@Override
protected String getDocumentContentType() {
return "application/x-www-form-urlencoded";
}
@Override
protected void generateDocumentBodyShapeSerializers(GenerationContext context, Set<Shape> shapes) {
AwsProtocolUtils.generateDocumentBodyShapeSerde(context, shapes, new Ec2ShapeSerVisitor(context));
}
@Override
protected void generateDocumentBodyShapeDeserializers(GenerationContext context, Set<Shape> shapes) {
AwsProtocolUtils.generateDocumentBodyShapeSerde(context, shapes, new XmlShapeDeserVisitor(context));
}
@Override
public void generateSharedComponents(GenerationContext context) {
super.generateSharedComponents(context);
AwsProtocolUtils.generateXmlParseBody(context);
AwsProtocolUtils.generateXmlParseErrorBody(context);
AwsProtocolUtils.generateBuildFormUrlencodedString(context);
AwsProtocolUtils.addItempotencyAutofillImport(context);
TypeScriptWriter writer = context.getWriter();
// Generate a function that handles the complex rules around deserializing
// an error code from an xml error body.
SymbolReference responseType = getApplicationProtocol().getResponseType();
writer.openBlock("const loadEc2ErrorCode = (\n"
+ " output: $T,\n"
+ " data: any\n"
+ "): string | undefined => {", "};", responseType, () -> {
// Attempt to fetch the error code from the specific location.
String errorCodeLocation = getErrorBodyLocation(context, "data") + ".Code";
writer.openBlock("if ($L !== undefined) {", "}", errorCodeLocation, () -> {
writer.write("return $L;", errorCodeLocation);
});
// Default a 404 status code to the NotFound code.
writer.openBlock("if (output.statusCode == 404) {", "}", () -> writer.write("return 'NotFound';"));
});
writer.write("");
}
@Override
protected String getErrorBodyLocation(GenerationContext context, String outputLocation) {
return outputLocation + ".Errors.Error";
}
@Override
protected void writeDefaultHeaders(GenerationContext context, OperationShape operation) {
super.writeDefaultHeaders(context, operation);
AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operation);
}
@Override
protected void serializeInputDocument(
GenerationContext context,
OperationShape operation,
StructureShape inputStructure
) {
TypeScriptWriter writer = context.getWriter();
// Set the form encoded string.
writer.openBlock("body = buildFormUrlencodedString({", "});", () -> {
// Gather all the explicit input entries.
writer.write("...$L,",
inputStructure.accept(new QueryMemberSerVisitor(context, "input", Format.DATE_TIME)));
// Set the protocol required values.
ServiceShape serviceShape = context.getService();
writer.write("Action: $S,", operation.getId().getName(serviceShape));
writer.write("Version: $S,", serviceShape.getVersion());
});
}
@Override
protected boolean writeUndefinedInputBody(GenerationContext context, OperationShape operation) {
return AwsProtocolUtils.generateUndefinedQueryInputBody(context, operation);
}
@Override
protected void writeErrorCodeParser(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();
// Outsource error code parsing since it's complex for this protocol.
writer.write("const errorCode = loadEc2ErrorCode(output, parsedOutput.body);");
}
@Override
protected void deserializeOutputDocument(
GenerationContext context,
OperationShape operation,
StructureShape outputStructure
) {
TypeScriptWriter writer = context.getWriter();
writer.write("contents = $L;",
outputStructure.accept(new XmlMemberDeserVisitor(context, "data", Format.DATE_TIME)));
}
@Override
public void generateProtocolTests(GenerationContext context) {
AwsProtocolUtils.generateProtocolTests(this, context);
}
}