|
| 1 | +/* |
| 2 | + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.smithy.aws.typescript.codegen; |
| 17 | + |
| 18 | +import java.util.Set; |
| 19 | +import software.amazon.smithy.codegen.core.SymbolReference; |
| 20 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 21 | +import software.amazon.smithy.model.shapes.Shape; |
| 22 | +import software.amazon.smithy.model.shapes.StructureShape; |
| 23 | +import software.amazon.smithy.model.traits.TimestampFormatTrait.Format; |
| 24 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 25 | +import software.amazon.smithy.typescript.codegen.integration.HttpRpcProtocolGenerator; |
| 26 | + |
| 27 | +/** |
| 28 | + * Handles generating the aws.ec2 protocol for services. It handles reading and |
| 29 | + * writing from document bodies, including generating any functions needed for |
| 30 | + * performing serde. |
| 31 | + * |
| 32 | + * This builds on the foundations of the {@link HttpRpcProtocolGenerator} to handle |
| 33 | + * standard components of the HTTP requests and responses. |
| 34 | + * |
| 35 | + * @see Ec2ShapeSerVisitor |
| 36 | + * @see XmlShapeDeserVisitor |
| 37 | + * @see QueryMemberSerVisitor |
| 38 | + * @see XmlMemberDeserVisitor |
| 39 | + * @see AwsProtocolUtils |
| 40 | + * @see <a href="https://awslabs.github.io/smithy/spec/xml.html">Smithy XML traits.</a> |
| 41 | + * @see <a href="https://awslabs.github.io/smithy/spec/aws-core.html#ec2QueryName-trait">Smithy EC2 Query Name trait.</a> |
| 42 | + */ |
| 43 | +final class AwsEc2 extends HttpRpcProtocolGenerator { |
| 44 | + |
| 45 | + AwsEc2() { |
| 46 | + super(true); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected String getOperationPath(GenerationContext context, OperationShape operationShape) { |
| 51 | + return "/"; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getName() { |
| 56 | + return "aws.ec2"; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected String getDocumentContentType() { |
| 61 | + return "application/x-www-form-urlencoded"; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void generateDocumentBodyShapeSerializers(GenerationContext context, Set<Shape> shapes) { |
| 66 | + AwsProtocolUtils.generateDocumentBodyShapeSerde(context, shapes, new Ec2ShapeSerVisitor(context)); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void generateDocumentBodyShapeDeserializers(GenerationContext context, Set<Shape> shapes) { |
| 71 | + AwsProtocolUtils.generateDocumentBodyShapeSerde(context, shapes, new XmlShapeDeserVisitor(context)); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void generateSharedComponents(GenerationContext context) { |
| 76 | + super.generateSharedComponents(context); |
| 77 | + AwsProtocolUtils.generateXmlParseBody(context); |
| 78 | + AwsProtocolUtils.generateBuildFormUrlencodedString(context); |
| 79 | + |
| 80 | + TypeScriptWriter writer = context.getWriter(); |
| 81 | + |
| 82 | + // Generate a function that handles the complex rules around deserializing |
| 83 | + // an error code from an xml error body. |
| 84 | + SymbolReference responseType = getApplicationProtocol().getResponseType(); |
| 85 | + writer.openBlock("const loadEc2ErrorCode = (\n" |
| 86 | + + " output: $T,\n" |
| 87 | + + " data: any\n" |
| 88 | + + "): string => {", "};", responseType, () -> { |
| 89 | + |
| 90 | + // Attempt to fetch the error code from the specific location, including the wrapper. |
| 91 | + writer.openBlock("if (data.Errors.Error.Code !== undefined) {", "}", () -> { |
| 92 | + writer.write("return data.Errors.Error.Code;"); |
| 93 | + }); |
| 94 | + |
| 95 | + // Default a 404 status code to the NotFound code. |
| 96 | + writer.openBlock("if (output.statusCode == 404) {", "}", () -> writer.write("return 'NotFound';")); |
| 97 | + |
| 98 | + // Default to an empty error code so an unmodeled exception is built. |
| 99 | + writer.write("return '';"); |
| 100 | + }); |
| 101 | + writer.write(""); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected void writeDefaultHeaders(GenerationContext context, OperationShape operation) { |
| 106 | + super.writeDefaultHeaders(context, operation); |
| 107 | + AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operation); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + protected void serializeInputDocument( |
| 112 | + GenerationContext context, |
| 113 | + OperationShape operation, |
| 114 | + StructureShape inputStructure |
| 115 | + ) { |
| 116 | + TypeScriptWriter writer = context.getWriter(); |
| 117 | + |
| 118 | + // Gather all the explicit input entries. |
| 119 | + writer.write("const entries = $L;", |
| 120 | + inputStructure.accept(new QueryMemberSerVisitor(context, "input", Format.DATE_TIME))); |
| 121 | + |
| 122 | + // Set the form encoded string. |
| 123 | + writer.openBlock("body = buildFormUrlencodedString({", "});", () -> { |
| 124 | + writer.write("...entries,"); |
| 125 | + // Set the protocol required values. |
| 126 | + writer.write("Action: $S,", operation.getId().getName()); |
| 127 | + writer.write("Version: $S,", context.getService().getVersion()); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + protected void writeErrorCodeParser(GenerationContext context) { |
| 133 | + TypeScriptWriter writer = context.getWriter(); |
| 134 | + |
| 135 | + // Outsource error code parsing since it's complex for this protocol. |
| 136 | + writer.write("errorCode = loadEc2ErrorCode(output, parsedOutput.body);"); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + protected void deserializeOutputDocument( |
| 141 | + GenerationContext context, |
| 142 | + OperationShape operation, |
| 143 | + StructureShape outputStructure |
| 144 | + ) { |
| 145 | + TypeScriptWriter writer = context.getWriter(); |
| 146 | + |
| 147 | + writer.write("contents = $L;", |
| 148 | + outputStructure.accept(new XmlMemberDeserVisitor(context, "data", Format.DATE_TIME))); |
| 149 | + } |
| 150 | +} |
0 commit comments