Skip to content

Commit

Permalink
feat: update runtime plugin interface (#400)
Browse files Browse the repository at this point in the history
* feat: remove generated SerDe runtime dependencies

Instead, use the runtime-specific utils specified in client config

* fix: change plugin interface to callback functions mutating the stack
  • Loading branch information
AllanZhengYP authored and trivikr committed Jan 3, 2020
1 parent ff70fac commit d75c620
Show file tree
Hide file tree
Showing 20 changed files with 226 additions and 299 deletions.
14 changes: 6 additions & 8 deletions clients/node/client-rds-data-node/RDSDataClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { contentLengthPlugin } from "@aws-sdk/middleware-content-length";
import { UserAgentPlugin, UserAgentConfig } from "@aws-sdk/middleware-user-agent";
import { userAgentPlugin, UserAgentConfig } from "@aws-sdk/middleware-user-agent";
import { retryPlugin, RetryConfig } from "@aws-sdk/retry-middleware";
import { signingPlugin, AwsAuthConfiguration } from "@aws-sdk/signing-middleware";
import { awsAuthPlugin, AwsAuthConfiguration } from "@aws-sdk/signing-middleware";
import {
RDSDataConfiguration,
RDSDataResolvedConfiguration,
Expand All @@ -22,19 +22,17 @@ export class RDSDataClient extends SmithyClient<HttpOptions, InputTypesUnion, Ou
...RDSRuntimeConfiguration,
...configuration
});
super(intermediaConfig_0);
let intermediaConfig_1 = RegionConfiguration.resolve(intermediaConfig_0);
let intermediaConfig_2 = AwsAuthConfiguration.resolve(intermediaConfig_1);
let intermediaConfig_3 = EndpointsConfig.resolve(intermediaConfig_2);
let intermediaConfig_4 = RetryConfig.resolve(intermediaConfig_3);
let intermediaConfig_5 = UserAgentConfig.resolve(intermediaConfig_4);
super(intermediaConfig_0);
this.config = intermediaConfig_5;
super.use(contentLengthPlugin(this.config));
if (this.config.maxRetries > 0) {
super.use(retryPlugin(this.config));
}
super.use(signingPlugin(this.config));
super.use(UserAgentPlugin(this.config));
super.use(retryPlugin(this.config));
super.use(awsAuthPlugin(this.config));
super.use(userAgentPlugin(this.config));
}

destroy(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Command } from "@aws-sdk/smithy-client";
import {
deserializerPlugin,
serializerPlugin
} from "@aws-sdk/middleware-serde";
import * as __aws_sdk_types from "@aws-sdk/types";
import { serdePlugin } from "@aws-sdk/middleware-serde";
import { HttpOptions, Handler, HandlerExecutionContext } from "@aws-sdk/types";
import { RDSDataResolvedConfiguration } from "../RDSDataConfiguration";
import { HttpRequest } from "@aws-sdk/protocol-http";
import {
Expand All @@ -22,38 +19,32 @@ type OutputTypesUnion = any;
export class ExecuteStatementCommand extends Command<
ExecuteStatementRequest,
ExecuteStatementResponse
> {
> {
constructor(readonly input: ExecuteStatementRequest) {
super();
}

resolveMiddleware(
clientStack: MiddlewareStack<InputTypesUnion, OutputTypesUnion>,
configuration: RDSDataResolvedConfiguration,
options?: __aws_sdk_types.HttpOptions
): __aws_sdk_types.Handler<
options?: HttpOptions
): Handler<
ExecuteStatementRequest,
ExecuteStatementResponse
> {
const { httpHandler } = configuration;
const { protocol: { handler } } = configuration;

this.use(serializerPlugin(configuration, executeStatementSerializer));
this.use(
deserializerPlugin<ExecuteStatementResponse>(
configuration,
executeStatementDeserializer
)
);
this.use(serdePlugin(configuration, executeStatementSerializer, executeStatementDeserializer));

const stack = clientStack.concat(this.middlewareStack);

const handlerExecutionContext: __aws_sdk_types.HandlerExecutionContext = {
const handlerExecutionContext: HandlerExecutionContext = {
logger: {} as any
};

return stack.resolve(
(request: FinalizeHandlerArguments<any>) =>
httpHandler.handle(request.request as HttpRequest, options || {}),
handler.handle(request.request as HttpRequest, options || {}),
handlerExecutionContext
);
}
Expand Down
96 changes: 46 additions & 50 deletions clients/node/client-rds-data-node/protocol/AwsRestJson1_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ import {
} from "../models/rdsdataservice";
import { HttpRequest, HttpResponse } from "@aws-sdk/protocol-http";
import { SerializerUtils, DeserializerUtils } from "@aws-sdk/types";
import * as __aws_sdk_stream_collector_node from "@aws-sdk/stream-collector-node";
import * as __aws_sdk_util_utf8_node from "@aws-sdk/util-utf8-node";
import { ResponseMetadata } from "@aws-sdk/types";

type Utils = { [key: string]: any };

export function executeStatementAwsRestJson1_1Serialize(
input: ExecuteStatementRequest,
utils?: Utils
utils: SerializerUtils
): HttpRequest {
let body: any = {};
if (input.resourceArn !== undefined) {
Expand All @@ -44,7 +40,7 @@ export function executeStatementAwsRestJson1_1Serialize(
}

if (input.parameters !== undefined) {
body.parameters = sqlParameterListAwsRestJson1_1Serialize(input.parameters);
body.parameters = sqlParameterListAwsRestJson1_1Serialize(input.parameters, utils);
}

if (input.transactionId !== undefined) {
Expand Down Expand Up @@ -72,51 +68,54 @@ export function executeStatementAwsRestJson1_1Serialize(

export async function executeStatementAwsRestJson1_1Deserialize(
output: HttpResponse,
utils?: Utils
utils: DeserializerUtils
): Promise<ExecuteStatementResponse> {
if (output.statusCode !== 200) {
return executeStatementAwsRestJson1_1DeserializeError(output);
return executeStatementAwsRestJson1_1DeserializeError(output, utils);
}
let data: any = await parseBody(output.body, utils);
return Promise.resolve({
$metadata: deserializeMetadata(output),
__type: "com.amazon.rdsdataservice#ExecuteStatementResponse",
records: recordsAwsRestJson1_1Deserialize(data.records),
records: recordsAwsRestJson1_1Deserialize(data.records, utils),
columnMetadata: columnMetadataListAwsRestJson1_1Deserialize(
data.columnMetadata
data.columnMetadata,
utils
),
numberOfRecordsUpdated: data.numberOfRecordsUpdated,
generatedFields: generatedFieldsAwsRestJson1_1Deserialize(
data.generatedFields
data.generatedFields,
utils
)
});
}

async function executeStatementAwsRestJson1_1DeserializeError(
output: HttpResponse
output: HttpResponse,
utils: DeserializerUtils
): Promise<ExecuteStatementResponse> {
let data = await parseBody(output.body);
let data = await parseBody(output.body, utils);
let response: any;
switch (output.headers["x-amzn-ErrorType"]) {
case "BadRequestException":
case "com.amazon.rdsdataservice#BadRequestException":
response = badRequestExceptionDeserialize(data);
response = badRequestExceptionDeserialize(data, utils);
break;
case "StatementTimeoutException":
case "com.amazon.rdsdataservice#StatementTimeoutException":
response = statementTimeoutExceptionDeserialize(data);
response = statementTimeoutExceptionDeserialize(data, utils);
break;
case "ForbiddenException":
case "com.amazon.rdsdataservice#ForbiddenException":
response = forbiddenExceptionDeserialize(data);
response = forbiddenExceptionDeserialize(data, utils);
break;
case "InternalServerErrorException":
case "com.amazon.rdsdataservice#InternalServerErrorException":
response = internalServerErrorExceptionDeserialize(data);
response = internalServerErrorExceptionDeserialize(data, utils);
break;
case "ServiceUnavailableError":
case "com.amazon.rdsdataservice#ServiceUnavailableError":
response = serviceUnavailableErrorDeserialize(data);
response = serviceUnavailableErrorDeserialize(data, utils);
break;
default:
response = {
Expand All @@ -130,19 +129,20 @@ async function executeStatementAwsRestJson1_1DeserializeError(
}

const sqlParameterListAwsRestJson1_1Serialize = (
input: Array<SqlParameter>
input: Array<SqlParameter>,
utils: SerializerUtils
): Array<SqlParameter> =>
input &&
input.map(sqlParameter => sqlParameterAwsRestJson1_1Serialize(sqlParameter));
input.map(sqlParameter => sqlParameterAwsRestJson1_1Serialize(sqlParameter, utils));

const sqlParameterAwsRestJson1_1Serialize = (input: SqlParameter): any =>
const sqlParameterAwsRestJson1_1Serialize = (input: SqlParameter, utils: SerializerUtils): any =>
input.name &&
input.value && {
name: input.name,
value: fieldAwsRestJson1_1Serialize(input.value)
value: fieldAwsRestJson1_1Serialize(input.value, utils)
};

const fieldAwsRestJson1_1Serialize = (input: Field): any =>
const fieldAwsRestJson1_1Serialize = (input: Field, utils: SerializerUtils): any =>
Field.visit(input, {
blobValue: value => {
value;
Expand Down Expand Up @@ -174,7 +174,8 @@ const fieldAwsRestJson1_1Serialize = (input: Field): any =>
});

export function columnMetadataAwsRestJson1_1Deserialize(
input: any
input: any,
utils: DeserializerUtils
): ColumnMetadata {
let columnMetadata: any = {
$namespace: "com.amazon.rdsdataservice",
Expand Down Expand Up @@ -240,14 +241,15 @@ export function columnMetadataAwsRestJson1_1Deserialize(
}

const columnMetadataListAwsRestJson1_1Deserialize = (
input: any
input: any,
utils: DeserializerUtils
): Array<ColumnMetadata> =>
input &&
input.map((columnMetadata: any) =>
columnMetadataAwsRestJson1_1Deserialize(columnMetadata)
columnMetadataAwsRestJson1_1Deserialize(columnMetadata, utils)
);

const fieldAwsRestJson1_1Deserialize = (input: any): any =>
const fieldAwsRestJson1_1Deserialize = (input: any, utils: DeserializerUtils): any =>
Field.visit(input, {
blobValue: value => {
value;
Expand Down Expand Up @@ -278,27 +280,28 @@ const fieldAwsRestJson1_1Deserialize = (input: any): any =>
}
});

const generatedFieldsAwsRestJson1_1Deserialize = (input: any): Array<Field> =>
input && input.map((field: any) => fieldAwsRestJson1_1Deserialize(field));
const generatedFieldsAwsRestJson1_1Deserialize = (input: any, utils: DeserializerUtils): Array<Field> =>
input && input.map((field: any) => fieldAwsRestJson1_1Deserialize(field, utils));

const recordsAwsRestJson1_1Deserialize = (input: any): Array<Array<Field>> =>
const recordsAwsRestJson1_1Deserialize = (input: any, utils: DeserializerUtils): Array<Array<Field>> =>
input &&
input.map((recordsList: any) =>
recordsListAwsRestJson1_1Deserialize(recordsList)
recordsListAwsRestJson1_1Deserialize(recordsList, utils)
);

const recordsListAwsRestJson1_1Deserialize = (input: any): Array<Field> =>
input && input.map((field: any) => fieldAwsRestJson1_1Deserialize(field));
const recordsListAwsRestJson1_1Deserialize = (input: any, utils: DeserializerUtils): Array<Field> =>
input && input.map((field: any) => fieldAwsRestJson1_1Deserialize(field, utils));

const badRequestExceptionDeserialize = (input: any): BadRequestException => ({
const badRequestExceptionDeserialize = (input: any, utils: DeserializerUtils): BadRequestException => ({
__type: "com.amazon.rdsdataservice#BadRequestException",
$name: "BadRequestException",
$fault: "client",
message: input.message
});

const statementTimeoutExceptionDeserialize = (
input: any
input: any,
utils: DeserializerUtils
): StatementTimeoutException => ({
__type: "com.amazon.rdsdataservice#StatementTimeoutException",
$name: "StatementTimeoutException",
Expand All @@ -307,23 +310,25 @@ const statementTimeoutExceptionDeserialize = (
dbConnectionId: input.dbConnectionId
});

const forbiddenExceptionDeserialize = (input: any): ForbiddenException => ({
const forbiddenExceptionDeserialize = (input: any, utils: DeserializerUtils): ForbiddenException => ({
__type: "com.amazon.rdsdataservice#ForbiddenException",
$name: "ForbiddenException",
$fault: "client",
message: input.message
});

const internalServerErrorExceptionDeserialize = (
input: any
input: any,
utils: DeserializerUtils
): InternalServerErrorException => ({
__type: "com.amazon.rdsdataservice#InternalServerErrorException",
$name: "InternalServerErrorException",
$fault: "server"
});

const serviceUnavailableErrorDeserialize = (
input: any
input: any,
utils: DeserializerUtils
): ServiceUnavailableError => ({
__type: "com.amazon.rdsdataservice#ServiceUnavailableError",
$name: "ServiceUnavailableError",
Expand All @@ -336,17 +341,8 @@ const deserializeMetadata = (output: HttpResponse): ResponseMetadata => ({
requestId: output.headers["x-amzn-requestid"]
});

const parseBody = (streamBody: any, utils?: Utils): any => {
const streamCollector =
utils && utils["streamCollector"]
? (<DeserializerUtils>utils)["streamCollector"]
: __aws_sdk_stream_collector_node.streamCollector;
const toUtf8 =
utils && utils["streamCollector"]
? (<DeserializerUtils>utils)["utf8Encoder"]
: __aws_sdk_util_utf8_node.toUtf8;

return streamCollector(streamBody).then(body => {
return JSON.parse(toUtf8(body));
const parseBody = (streamBody: any, utils: DeserializerUtils): any => {
return utils.streamCollector(streamBody).then(body => {
return JSON.parse(utils.utf8Encoder(body));
});
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HttpRequest, HttpResponse } from "@aws-sdk/protocol-http";
import { SerializerUtils, DeserializerUtils } from "@aws-sdk/types";
import {
ExecuteStatementRequest,
ExecuteStatementResponse
Expand All @@ -8,12 +9,10 @@ import {
executeStatementAwsRestJson1_1Deserialize
} from "./AwsRestJson1_1";

type Utils = { [key: string]: any };

export function executeStatementSerializer(
input: ExecuteStatementRequest,
protocol: string,
utils?: Utils
utils: SerializerUtils
): HttpRequest {
switch (protocol) {
case "aws.rest-json-1.1":
Expand All @@ -26,7 +25,7 @@ export function executeStatementSerializer(
export async function executeStatementDeserializer(
output: HttpResponse,
protocol: string,
utils?: Utils
utils: DeserializerUtils
): Promise<ExecuteStatementResponse> {
switch (protocol) {
case "aws.rest-json-1.1":
Expand Down
13 changes: 6 additions & 7 deletions packages/middleware-content-length/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
BodyLengthCalculator,
MetadataBearer,
BuildHandlerOutput,
InjectableMiddleware
Injectable
} from "@aws-sdk/types";
import { HttpRequest } from "@aws-sdk/protocol-http";

Expand Down Expand Up @@ -44,12 +44,11 @@ export function contentLengthMiddleware(
};
}

export function contentLengthPlugin(options: {
export const contentLengthPlugin = (options: {
bodyLengthChecker: BodyLengthCalculator;
}): InjectableMiddleware {
return {
middleware: contentLengthMiddleware(options.bodyLengthChecker),
}): Injectable<any, any> => clientStack => {
clientStack.add(contentLengthMiddleware(options.bodyLengthChecker), {
step: "build",
tags: { SET_CONTENT_LENGTH: true }
};
}
});
};
Loading

0 comments on commit d75c620

Please sign in to comment.