Skip to content

Commit

Permalink
Fix concatenation of apiRoot and apiEndpoint props, to allow for dyna…
Browse files Browse the repository at this point in the history
…mic values in the path.

Update README entry with more complex example.
  • Loading branch information
MathieuGilbert committed Oct 3, 2024
1 parent 7a4f865 commit 5238cb7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-stepfunctions-tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ const connection = new events.Connection(this, 'Connection', {

new tasks.HttpInvoke(this, 'Invoke HTTP API', {
apiRoot: 'https://api.example.com',
apiEndpoint: sfn.TaskInput.fromText('path/to/resource'),
apiEndpoint: sfn.TaskInput.fromText(sfn.JsonPath.format('resource/{}/details', sfn.JsonPath.stringAt('$.resourceId'))),
body: sfn.TaskInput.fromObject({ foo: 'bar' }),
connection,
headers: sfn.TaskInput.fromObject({ 'Content-Type': 'application/json' }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class HttpInvoke extends sfn.TaskStateBase {

private buildTaskParameters() {
const parameters: TaskParameters = {
ApiEndpoint: `${this.props.apiRoot}/${this.props.apiEndpoint.value}`,
ApiEndpoint: sfn.JsonPath.format('{}/{}', this.props.apiRoot, this.props.apiEndpoint.value),
Authentication: {
ConnectionArn: this.props.connection.connectionArn,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ describe('AWS::StepFunctions::Tasks::HttpInvoke', () => {
beforeEach(() => {
stack = new cdk.Stack();
connection = new events.Connection(stack, 'Connection', {
authorization: events.Authorization.basic('username', cdk.SecretValue.unsafePlainText('password')),
authorization: events.Authorization.basic(
'username',
cdk.SecretValue.unsafePlainText('password'),
),
connectionName: 'testConnection',
});
});
Expand All @@ -44,11 +47,12 @@ describe('AWS::StepFunctions::Tasks::HttpInvoke', () => {
});

expectTaskWithParameters(task, {
ApiEndpoint: 'https://api.example.com/path/to/resource',
Authentication: {
'ApiEndpoint.$':
"States.Format('{}/{}', 'https://api.example.com', 'path/to/resource')",
'Authentication': {
ConnectionArn: stack.resolve(connection.connectionArn),
},
Method: 'POST',
'Method': 'POST',
});
});

Expand All @@ -68,22 +72,22 @@ describe('AWS::StepFunctions::Tasks::HttpInvoke', () => {
});

expectTaskWithParameters(task, {
ApiEndpoint: 'https://api.example.com/path/to/resource',
Authentication: {
'ApiEndpoint.$': "States.Format('{}/{}', 'https://api.example.com', 'path/to/resource')",
'Authentication': {
ConnectionArn: stack.resolve(connection.connectionArn),
},
Method: 'POST',
Headers: {
'Method': 'POST',
'Headers': {
'custom-header': 'custom-value',
'Content-Type': 'application/x-www-form-urlencoded',
},
Transform: {
'Transform': {
RequestBodyEncoding: 'URL_ENCODED',
RequestEncodingOptions: {
ArrayFormat: lib.URLEncodingFormat.BRACKETS,
},
},
QueryParameters: {
'QueryParameters': {
foo: 'bar',
},
});
Expand All @@ -99,15 +103,15 @@ describe('AWS::StepFunctions::Tasks::HttpInvoke', () => {
});

expectTaskWithParameters(task, {
ApiEndpoint: 'https://api.example.com/path/to/resource',
Authentication: {
'ApiEndpoint.$': "States.Format('{}/{}', 'https://api.example.com', 'path/to/resource')",
'Authentication': {
ConnectionArn: stack.resolve(connection.connectionArn),
},
Method: 'POST',
Headers: {
'Method': 'POST',
'Headers': {
'Content-Type': 'application/x-www-form-urlencoded',
},
Transform: {
'Transform': {
RequestBodyEncoding: 'URL_ENCODED',
},
});
Expand All @@ -123,11 +127,30 @@ describe('AWS::StepFunctions::Tasks::HttpInvoke', () => {
});

expectTaskWithParameters(task, {
ApiEndpoint: 'https://api.example.com/path/to/resource',
Authentication: {
'ApiEndpoint.$': "States.Format('{}/{}', 'https://api.example.com', 'path/to/resource')",
'Authentication': {
ConnectionArn: stack.resolve(connection.connectionArn),
},
Method: 'POST',
'Method': 'POST',
});
});

test('invoke with formatted apiEndpoint', () => {
const task = new lib.HttpInvoke(stack, 'Task', {
apiRoot: 'https://api.example.com',
apiEndpoint: sfn.TaskInput.fromText(
sfn.JsonPath.format('resource/{}/details', sfn.JsonPath.stringAt('$.resourceId')),
),
connection,
method: sfn.TaskInput.fromText('POST'),
});
expectTaskWithParameters(task, {
'ApiEndpoint.$':
"States.Format('{}/{}', 'https://api.example.com', States.Format('resource/{}/details', $.resourceId))",
'Authentication': {
ConnectionArn: stack.resolve(connection.connectionArn),
},
'Method': 'POST',
});
});
});

0 comments on commit 5238cb7

Please sign in to comment.