@@ -23,9 +23,13 @@ public ApiGatewayHelper(IAmazonAPIGateway apiGatewayV1Client, IAmazonApiGatewayV
23
23
_httpClient = new HttpClient ( ) ;
24
24
}
25
25
26
- public async Task WaitForApiAvailability ( string apiId , string apiUrl , bool isHttpApi , int maxWaitTimeSeconds = 30 )
26
+ public async Task WaitForApiAvailability ( string apiId , string apiUrl , bool isHttpApi , int maxWaitTimeSeconds = 60 )
27
27
{
28
28
var startTime = DateTime . UtcNow ;
29
+ var successStartTime = DateTime . UtcNow ;
30
+ var requiredSuccessDuration = TimeSpan . FromSeconds ( 10 ) ;
31
+ bool hasBeenSuccessful = false ;
32
+
29
33
while ( ( DateTime . UtcNow - startTime ) . TotalSeconds < maxWaitTimeSeconds )
30
34
{
31
35
try
@@ -47,77 +51,107 @@ public async Task WaitForApiAvailability(string apiId, string apiUrl, bool isHtt
47
51
{
48
52
var response = await httpClient . PostAsync ( apiUrl , new StringContent ( "{}" ) ) ;
49
53
50
- // Check if we get a response, even if it's an error
51
- if ( response . StatusCode != HttpStatusCode . NotFound && response . StatusCode != HttpStatusCode . Forbidden )
54
+ // Check if we get a successful response
55
+ if ( response . StatusCode != HttpStatusCode . Forbidden && response . StatusCode != HttpStatusCode . NotFound )
56
+ {
57
+ if ( ! hasBeenSuccessful )
58
+ {
59
+ successStartTime = DateTime . UtcNow ;
60
+ hasBeenSuccessful = true ;
61
+ }
62
+
63
+ if ( ( DateTime . UtcNow - successStartTime ) >= requiredSuccessDuration )
64
+ {
65
+ return ; // API has been responding successfully for at least 10 seconds
66
+ }
67
+ }
68
+ else
52
69
{
53
- return ; // API is available and responding
70
+ // Reset the success timer if we get a non-successful response
71
+ hasBeenSuccessful = false ;
72
+ Console . WriteLine ( $ "API responded with status code: { response . StatusCode } ") ;
54
73
}
55
74
}
56
75
}
57
76
catch ( Amazon . ApiGatewayV2 . Model . NotFoundException ) when ( isHttpApi )
58
77
{
59
78
// HTTP API not found yet, continue waiting
79
+ hasBeenSuccessful = false ;
60
80
}
61
81
catch ( Amazon . APIGateway . Model . NotFoundException ) when ( ! isHttpApi )
62
82
{
63
83
// REST API not found yet, continue waiting
84
+ hasBeenSuccessful = false ;
64
85
}
65
86
catch ( Exception ex )
66
87
{
67
- // Log unexpected exceptions
88
+ // Log unexpected exceptions and reset success timer
68
89
Console . WriteLine ( $ "Unexpected error while checking API availability: { ex . Message } ") ;
90
+ hasBeenSuccessful = false ;
69
91
}
70
92
await Task . Delay ( 1000 ) ; // Wait for 1 second before checking again
71
93
}
72
- throw new TimeoutException ( $ "API { apiId } did not become available within { maxWaitTimeSeconds } seconds") ;
94
+ throw new TimeoutException ( $ "API { apiId } did not become consistently available within { maxWaitTimeSeconds } seconds") ;
73
95
}
74
96
75
- public async Task < string > AddRouteToRestApi ( string restApiId , string lambdaArn , string route = "/test" )
97
+
98
+ public async Task < string > AddRouteToRestApi ( string restApiId , string lambdaArn , string route = "/test" , string httpMethod = "ANY" )
76
99
{
77
- var rootResourceId = ( await _apiGatewayV1Client . GetResourcesAsync ( new GetResourcesRequest { RestApiId = restApiId } ) ) . Items [ 0 ] . Id ;
100
+ // Get all resources and find the root resource
101
+ var resources = await _apiGatewayV1Client . GetResourcesAsync ( new GetResourcesRequest { RestApiId = restApiId } ) ;
102
+ var rootResource = resources . Items . First ( r => r . Path == "/" ) ;
103
+ var rootResourceId = rootResource . Id ;
78
104
79
- var pathParts = route . Trim ( '/' ) . Split ( '/' ) ;
80
- var currentResourceId = rootResourceId ;
81
- foreach ( var pathPart in pathParts )
105
+ // Split the route into parts and create each part
106
+ var routeParts = route . Trim ( '/' ) . Split ( '/' ) ;
107
+ string currentPath = "" ;
108
+ string parentResourceId = rootResourceId ;
109
+
110
+ foreach ( var part in routeParts )
82
111
{
83
- var resources = await _apiGatewayV1Client . GetResourcesAsync ( new GetResourcesRequest { RestApiId = restApiId } ) ;
84
- var existingResource = resources . Items . FirstOrDefault ( r => r . ParentId == currentResourceId && r . PathPart == pathPart ) ;
112
+ currentPath += "/" + part ;
85
113
114
+ // Check if the resource already exists
115
+ var existingResource = resources . Items . FirstOrDefault ( r => r . Path == currentPath ) ;
86
116
if ( existingResource == null )
87
117
{
118
+ // Create the resource if it doesn't exist
88
119
var createResourceResponse = await _apiGatewayV1Client . CreateResourceAsync ( new CreateResourceRequest
89
120
{
90
121
RestApiId = restApiId ,
91
- ParentId = currentResourceId ,
92
- PathPart = pathPart
122
+ ParentId = parentResourceId ,
123
+ PathPart = part
93
124
} ) ;
94
- currentResourceId = createResourceResponse . Id ;
125
+ parentResourceId = createResourceResponse . Id ;
95
126
}
96
127
else
97
128
{
98
- currentResourceId = existingResource . Id ;
129
+ parentResourceId = existingResource . Id ;
99
130
}
100
131
}
101
132
133
+ // Create the method for the final resource
102
134
await _apiGatewayV1Client . PutMethodAsync ( new PutMethodRequest
103
135
{
104
136
RestApiId = restApiId ,
105
- ResourceId = currentResourceId ,
106
- HttpMethod = "ANY" ,
137
+ ResourceId = parentResourceId ,
138
+ HttpMethod = httpMethod ,
107
139
AuthorizationType = "NONE"
108
140
} ) ;
109
141
142
+ // Create the integration for the method
110
143
await _apiGatewayV1Client . PutIntegrationAsync ( new PutIntegrationRequest
111
144
{
112
145
RestApiId = restApiId ,
113
- ResourceId = currentResourceId ,
114
- HttpMethod = "ANY" ,
146
+ ResourceId = parentResourceId ,
147
+ HttpMethod = httpMethod ,
115
148
Type = APIGateway . IntegrationType . AWS_PROXY ,
116
149
IntegrationHttpMethod = "POST" ,
117
150
Uri = $ "arn:aws:apigateway:{ _apiGatewayV1Client . Config . RegionEndpoint . SystemName } :lambda:path/2015-03-31/functions/{ lambdaArn } /invocations"
118
151
} ) ;
119
152
120
- await _apiGatewayV1Client . CreateDeploymentAsync ( new APIGateway . Model . CreateDeploymentRequest
153
+ // Deploy the API
154
+ var deploymentResponse = await _apiGatewayV1Client . CreateDeploymentAsync ( new APIGateway . Model . CreateDeploymentRequest
121
155
{
122
156
RestApiId = restApiId ,
123
157
StageName = "test"
0 commit comments