Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(integ-tests): http flattenResponse #30361

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class HttpApiCall extends ApiCallBase {
parameters: props,
expected: Lazy.any({ produce: () => this.expectedResult }),
stateMachineArn: Lazy.string({ produce: () => this.stateMachineArn }),
flattenResponse: Lazy.string({ produce: () => this.flattenResponse }),
salt: Date.now().toString(),
},
resourceType: `${HTTP_RESOURCE_TYPE}${name}`.substring(0, 60),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import fetch, { Response } from 'node-fetch';
// TODO: can use builtin fetch on node18
import { flatten } from '@aws-cdk/aws-custom-resource-sdk-adapter';
import { CustomResourceHandler } from './base';
import { HttpRequest, HttpResponseWrapper } from './types';
import { deepParseJson } from './utils';

export class HttpHandler extends CustomResourceHandler<HttpRequest, HttpResponseWrapper> {
protected async processEvent(request: HttpRequest): Promise<HttpResponseWrapper> {
export class HttpHandler extends CustomResourceHandler<HttpRequest, HttpResponseWrapper | { [key: string]: unknown }> {
protected async processEvent(request: HttpRequest): Promise<HttpResponseWrapper | { [key: string]: unknown }> {
console.log('request', request);
const response: Response = await fetch(request.parameters.url, request.parameters.fetchOptions);
const result: any = {
Expand All @@ -22,8 +24,17 @@ export class HttpHandler extends CustomResourceHandler<HttpRequest, HttpResponse
} catch (e) {
// Okay
}
return {
apiCallResponse: result,
};

let resp: HttpResponseWrapper | { [key: string]: unknown };
if (request.flattenResponse === 'true') {
// Flatten and explode JSON fields
resp = flatten(deepParseJson({ apiCallResponse: result }));
} else {
// Otherwise just return the response as-is, without exploding JSON fields
resp = { apiCallResponse: result };
}
console.log(`Returning result ${JSON.stringify(resp)}`);
Comment on lines +28 to +36
Copy link
Contributor Author

@nmussy nmussy Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth considering a refactor of the flattenResponse in the common CustomResourceHandler implementation, given the SDK handler does pretty much the same thing:

let resp: AwsApiCallResult | { [key: string]: unknown };
if (request.outputPaths || request.flattenResponse === 'true') {
// Flatten and explode JSON fields
const flattened = flatten(deepParseJson({ apiCallResponse: response }));
resp = request.outputPaths ? filterKeys(flattened, request.outputPaths) : flattened;


return resp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ export interface HttpRequest {
* Parameters from the custom resource
*/
readonly parameters: HttpRequestParameters;

/**
* Whether or not to flatten the response from the HTTP request
*
* Valid values are 'true' or 'false' as strings
*
* Typically when using an HttpRequest you will be passing it as the
* `actual` value to an assertion provider so this would be set
* to 'false' (you want the actual response).
*
* If you are using the HttpRequest to perform more of a query to return
* a single value to use, then this should be set to 'true'.
*
* @default 'false'
*/
readonly flattenResponse?: string;
}

/**
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading