-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathcall-rest-api.ts
70 lines (62 loc) · 2.31 KB
/
call-rest-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import * as apigateway from '@aws-cdk/aws-apigateway';
import * as iam from '@aws-cdk/aws-iam';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CallApiGatewayEndpointBase } from './base';
import { CallApiGatewayEndpointBaseProps } from './base-types';
/**
* Properties for calling an REST API Endpoint
*/
export interface CallApiGatewayRestApiEndpointProps extends CallApiGatewayEndpointBaseProps {
/**
* API to call
*/
readonly api: apigateway.IRestApi;
/**
* Name of the stage where the API is deployed to in API Gateway
*/
readonly stageName: string;
}
/**
* Call REST API endpoint as a Task
*
* Be aware that the header values must be arrays. When passing the Task Token
* in the headers field `WAIT_FOR_TASK_TOKEN` integration, use
* `JsonPath.array()` to wrap the token in an array:
*
* ```ts
* import * as apigateway from '@aws-cdk/aws-apigateway';
* declare const api: apigateway.RestApi;
*
* new tasks.CallApiGatewayRestApiEndpoint(this, 'Endpoint', {
* api,
* stageName: 'Stage',
* method: tasks.HttpMethod.PUT,
* integrationPattern: sfn.IntegrationPattern.WAIT_FOR_TASK_TOKEN,
* headers: sfn.TaskInput.fromObject({
* TaskToken: sfn.JsonPath.array(sfn.JsonPath.taskToken),
* }),
* });
* ```
*
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-api-gateway.html
*/
export class CallApiGatewayRestApiEndpoint extends CallApiGatewayEndpointBase {
protected readonly taskMetrics?: sfn.TaskMetricsConfig | undefined;
protected readonly taskPolicies?: iam.PolicyStatement[] | undefined;
protected readonly apiEndpoint: string;
protected readonly arnForExecuteApi: string;
protected readonly stageName?: string;
constructor(scope: Construct, id: string, private readonly props: CallApiGatewayRestApiEndpointProps) {
super(scope, id, props);
this.apiEndpoint = this.getApiEndpoint();
this.arnForExecuteApi = props.api.arnForExecuteApi(props.method, props.apiPath, props.stageName);
this.stageName = props.stageName;
this.taskPolicies = this.createPolicyStatements();
}
private getApiEndpoint(): string {
const apiStack = cdk.Stack.of(this.props.api);
return `${this.props.api.restApiId}.execute-api.${apiStack.region}.${apiStack.urlSuffix}`;
}
}