Skip to content

Commit

Permalink
address feedbacks: fix indentation; readResponseBody; add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed Dec 24, 2019
1 parent c9454c4 commit 6297831
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ private List<HttpBinding> readResponseBody(
.orElse(false);

if (!documentBindings.isEmpty()) {
// If response has document binding, the body can be parsed to JavaScript object.
writer.write("const data: any = await parseBody(output.body, context);");
deserializeOutputDocument(context, operationOrError, documentBindings);
return documentBindings;
Expand All @@ -734,12 +735,11 @@ private List<HttpBinding> readResponseBody(
} else if (target instanceof BlobShape) {
// If payload is blob, only need to collect stream to binary data(Uint8Array).
writer.write("const data: any = await collectBody(output.body, context);");
} else if (target instanceof CollectionShape || target instanceof StructureShape) {
// If body is Collection or Structure, they we need to parse the string into JavaScript object.
} else if (target instanceof StructureShape || target instanceof UnionShape) {
// If body is Structure or Union, they we need to parse the string into JavaScript object.
writer.write("const data: any = await parseBody(output.body, context);");
} else {
// If payload is other scalar types(not Collection or Structure), because payload will be values in
// string instead of valid JSON or XML. So we need to collect body and encode binary to string.
// If payload is string, we need to collect body and encode binary to string.
writer.write("const data: any = await collectBodyString(output.body, context);");
}
writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ static void generateCollectBody(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.write("// Collect low-level response body stream to Uint8Array.");
writer.openBlock("const collectBody = (streamBody: any, context: __SerdeContext): Promise<Uint8Array> => {",
"};", () -> {
writer.write("return context.streamCollector(streamBody) || new Uint8Array();");
Expand All @@ -124,6 +125,7 @@ static void generateCollectBodyString(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.write("// Encode Uint8Array data into string with utf-8.");
writer.openBlock("const collectBodyString = (streamBody: any, context: __SerdeContext): Promise<string> => {",
"};", () -> {
writer.write("return collectBody(streamBody, context).then(body => context.utf8Encoder(body));");
Expand Down Expand Up @@ -167,11 +169,12 @@ static Set<StructureShape> generateErrorDispatcher(
// Prepare error response for parsing error code. If error code needs to be parsed from response body
// then we collect body and parse it to JS object, otherwise leave the response body as is.
writer.openBlock(
"const $L: any = {", "};", shouldParseErrorBody ? "parsedOutput" : "errorOutput", () -> {
writer.write("...output,");
writer.write("body: $L,",
shouldParseErrorBody ? "await parseBody(output.body, context)" : "output.body");
});
"const $L: any = {", "};", shouldParseErrorBody ? "parsedOutput" : "errorOutput",
() -> {
writer.write("...output,");
writer.write("body: $L,",
shouldParseErrorBody ? "await parseBody(output.body, context)" : "output.body");
});

// Error responses must be at least SmithyException and MetadataBearer implementations.
writer.addImport("SmithyException", "__SmithyException",
Expand Down

0 comments on commit 6297831

Please sign in to comment.