1
1
/*
2
- * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License").
5
- * You may not use this file except in compliance with the License.
6
- * A copy of the License is located at
7
- *
8
- * http://aws.amazon.com/apache2.0
9
- *
10
- * or in the "license" file accompanying this file. This file is distributed
11
- * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12
- * express or implied. See the License for the specific language governing
13
- * permissions and limitations under the License.
14
- */
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License").
5
+ * You may not use this file except in compliance with the License.
6
+ * A copy of the License is located at
7
+ *
8
+ * http://aws.amazon.com/apache2.0
9
+ *
10
+ * or in the "license" file accompanying this file. This file is distributed
11
+ * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12
+ * express or implied. See the License for the specific language governing
13
+ * permissions and limitations under the License.
14
+ */
15
15
16
16
using System ;
17
17
using System . Collections . Generic ;
@@ -38,18 +38,20 @@ public override void InvokeSync(IExecutionContext executionContext)
38
38
{
39
39
var requestContext = executionContext . RequestContext ;
40
40
var regionalEndpoint = requestContext . Request . Endpoint ;
41
- PreInvoke ( executionContext ) ;
41
+ var immutableCredentials = ( requestContext . Identity as AWSCredentials ) ? . GetCredentials ( ) ;
42
42
43
+ PreInvoke ( executionContext , immutableCredentials ) ;
44
+
43
45
try
44
46
{
45
47
base . InvokeSync ( executionContext ) ;
46
48
return ;
47
49
}
48
50
catch ( Exception exception )
49
- {
51
+ {
50
52
if ( IsInvalidEndpointException ( exception ) )
51
53
{
52
- EvictCacheKeyForRequest ( requestContext , regionalEndpoint ) ;
54
+ EvictCacheKeyForRequest ( requestContext , regionalEndpoint , immutableCredentials ) ;
53
55
}
54
56
55
57
throw ;
@@ -69,22 +71,24 @@ public override async System.Threading.Tasks.Task<T> InvokeAsync<T>(IExecutionCo
69
71
{
70
72
var requestContext = executionContext . RequestContext ;
71
73
var regionalEndpoint = requestContext . Request . Endpoint ;
72
- PreInvoke ( executionContext ) ;
74
+ var immutableCredentials = await ( ( requestContext . Identity as AWSCredentials ) ? . GetCredentialsAsync ( ) ) . ConfigureAwait ( false ) ;
73
75
76
+ PreInvoke ( executionContext , immutableCredentials ) ;
77
+
74
78
try
75
79
{
76
- return await base . InvokeAsync < T > ( executionContext ) . ConfigureAwait ( false ) ;
80
+ return await base . InvokeAsync < T > ( executionContext ) . ConfigureAwait ( false ) ;
77
81
}
78
82
catch ( Exception exception )
79
83
{
80
84
var capturedException = System . Runtime . ExceptionServices . ExceptionDispatchInfo . Capture ( exception ) ;
81
85
if ( IsInvalidEndpointException ( capturedException . SourceException ) )
82
86
{
83
- EvictCacheKeyForRequest ( requestContext , regionalEndpoint ) ;
87
+ EvictCacheKeyForRequest ( requestContext , regionalEndpoint , immutableCredentials ) ;
84
88
}
85
89
86
90
capturedException . Throw ( ) ;
87
- }
91
+ }
88
92
89
93
throw new AmazonClientException ( "Neither a response was returned nor an exception was thrown in the Runtime EndpointDiscoveryResolver." ) ;
90
94
}
@@ -94,22 +98,22 @@ public override async System.Threading.Tasks.Task<T> InvokeAsync<T>(IExecutionCo
94
98
/// Resolves the endpoint to be used for the current request
95
99
/// before invoking the next handler.
96
100
/// </summary>
97
- /// <param name="executionContext">The execution context, it contains the
98
- /// request and response context.</ param>
99
- protected static void PreInvoke ( IExecutionContext executionContext )
100
- {
101
- DiscoverEndpoints ( executionContext . RequestContext , false ) ;
101
+ /// <param name="executionContext">The execution context, it contains the request and response context.</param>
102
+ /// < param name="credentials">The AWS credentials for the account making the service call.</param>
103
+ protected static void PreInvoke ( IExecutionContext executionContext , ImmutableCredentials credentials )
104
+ {
105
+ DiscoverEndpoints ( executionContext . RequestContext , false , credentials ) ;
102
106
}
103
107
104
- public static void EvictCacheKeyForRequest ( IRequestContext requestContext , Uri regionalEndpoint )
108
+ public static void EvictCacheKeyForRequest ( IRequestContext requestContext , Uri regionalEndpoint , ImmutableCredentials credentials )
105
109
{
106
- DiscoverEndpoints ( requestContext , true ) ;
110
+ DiscoverEndpoints ( requestContext , true , credentials ) ;
107
111
requestContext . Request . Endpoint = regionalEndpoint ;
108
112
}
109
113
110
- public static void DiscoverEndpoints ( IRequestContext requestContext , bool evictCacheKey )
114
+ public static void DiscoverEndpoints ( IRequestContext requestContext , bool evictCacheKey , ImmutableCredentials credentials )
111
115
{
112
- var discoveryEndpoints = ProcessEndpointDiscovery ( requestContext , evictCacheKey , requestContext . Request . Endpoint ) ;
116
+ var discoveryEndpoints = ProcessEndpointDiscovery ( requestContext , evictCacheKey , requestContext . Request . Endpoint , credentials ) ;
113
117
if ( discoveryEndpoints != null )
114
118
{
115
119
foreach ( var endpoint in discoveryEndpoints )
@@ -118,7 +122,7 @@ public static void DiscoverEndpoints(IRequestContext requestContext, bool evictC
118
122
//and we couldn't get an endpoint back during an asynchronous discovery
119
123
//attempt. The null address endpoint will be evicted after 60 seconds but will
120
124
//prevent multiple server requests during this time.
121
- if ( endpoint . Address == null )
125
+ if ( endpoint . Address == null )
122
126
{
123
127
continue ;
124
128
}
@@ -130,12 +134,11 @@ public static void DiscoverEndpoints(IRequestContext requestContext, bool evictC
130
134
}
131
135
}
132
136
133
- private static IEnumerable < DiscoveryEndpointBase > ProcessEndpointDiscovery ( IRequestContext requestContext , bool evictCacheKey , Uri evictUri )
134
- {
137
+ private static IEnumerable < DiscoveryEndpointBase > ProcessEndpointDiscovery ( IRequestContext requestContext , bool evictCacheKey , Uri evictUri , ImmutableCredentials credentials )
138
+ {
135
139
var options = requestContext . Options ;
136
- var immutableCredentials = ( requestContext . Identity as AWSCredentials ) ? . GetCredentials ( ) ;
137
140
138
- if ( options . EndpointDiscoveryMarshaller != null && options . EndpointOperation != null && immutableCredentials != null )
141
+ if ( options . EndpointDiscoveryMarshaller != null && options . EndpointOperation != null && credentials != null )
139
142
{
140
143
//Endpoint discovery is supported by this operation and we have an endpoint operation available to use
141
144
var endpointDiscoveryData = options . EndpointDiscoveryMarshaller . Marshall ( requestContext . OriginalRequest ) ;
@@ -144,11 +147,11 @@ private static IEnumerable<DiscoveryEndpointBase> ProcessEndpointDiscovery(IRequ
144
147
{
145
148
operationName = AWSSDKUtils . ExtractOperationName ( requestContext . RequestName ) ;
146
149
}
147
- return options . EndpointOperation ( new EndpointOperationContext ( immutableCredentials . AccessKey , operationName , endpointDiscoveryData , evictCacheKey , evictUri ) ) ;
150
+ return options . EndpointOperation ( new EndpointOperationContext ( credentials . AccessKey , operationName , endpointDiscoveryData , evictCacheKey , evictUri ) ) ;
148
151
}
149
152
150
153
return null ;
151
- }
154
+ }
152
155
153
156
private static bool IsInvalidEndpointException ( Exception exception )
154
157
{
@@ -161,4 +164,4 @@ private static bool IsInvalidEndpointException(Exception exception)
161
164
return false ;
162
165
}
163
166
}
164
- }
167
+ }
0 commit comments