@@ -11,19 +11,22 @@ import AWSPluginsCore
11
11
import AWSCore
12
12
13
13
public extension AWSAPICategoryPluginConfiguration {
14
-
15
14
struct EndpointConfig {
16
-
15
+ // API name
17
16
let name : String
17
+
18
18
let baseURL : URL
19
19
let region : AWSRegionType ?
20
+
21
+ // default authorization type
20
22
let authorizationType : AWSAuthorizationType
23
+
24
+ // default authorization configuration
21
25
let authorizationConfiguration : AWSAuthorizationConfiguration
26
+
22
27
let endpointType : AWSAPICategoryPluginEndpointType
23
- // TODO: Refactor into an "Intercepting connection configuration" or similar --
24
- // EndpointConfig shouldn't be holding onto interceptors; it should just be a data holder.
25
- // https://github.com/aws-amplify/amplify-ios/issues/73
26
- var interceptors = [ URLRequestInterceptor] ( )
28
+
29
+ var apiKey : String ?
27
30
28
31
public init ( name: String ,
29
32
jsonValue: JSONValue ,
@@ -42,12 +45,17 @@ public extension AWSAPICategoryPluginConfiguration {
42
45
)
43
46
}
44
47
48
+ var apiKeyValue : String ?
49
+ if case . string( let apiKey) = endpointJSON [ " apiKey " ] {
50
+ apiKeyValue = apiKey
51
+ }
52
+
45
53
try self . init ( name: name,
46
54
baseURL: EndpointConfig . getBaseURL ( from: endpointJSON) ,
47
- region: EndpointConfig . getRegion ( from: endpointJSON) ,
48
- authorizationType: EndpointConfig . getAuthorizationType ( from: endpointJSON) ,
49
- authorizationConfiguration: EndpointConfig . getAuthorizationConfiguration ( from: endpointJSON) ,
55
+ region: AWSRegionType . region ( from: endpointJSON) ,
56
+ authorizationType: AWSAuthorizationType . from ( endpointJSON: endpointJSON) ,
50
57
endpointType: EndpointConfig . getEndpointType ( from: endpointJSON) ,
58
+ apiKey: apiKeyValue,
51
59
apiAuthProviderFactory: apiAuthProviderFactory,
52
60
authService: authService)
53
61
}
@@ -56,62 +64,25 @@ public extension AWSAPICategoryPluginConfiguration {
56
64
baseURL: URL ,
57
65
region: AWSRegionType ? ,
58
66
authorizationType: AWSAuthorizationType ,
59
- authorizationConfiguration: AWSAuthorizationConfiguration ,
60
67
endpointType: AWSAPICategoryPluginEndpointType ,
68
+ apiKey: String ? = nil ,
61
69
apiAuthProviderFactory: APIAuthProviderFactory ,
62
70
authService: AWSAuthServiceBehavior ? = nil ) throws {
63
71
self . name = name
64
72
self . baseURL = baseURL
65
73
self . region = region
66
74
self . authorizationType = authorizationType
67
- self . authorizationConfiguration = authorizationConfiguration
75
+ self . authorizationConfiguration = try AWSAuthorizationConfiguration . makeConfiguration ( authType: authorizationType,
76
+ region: region,
77
+ apiKey: apiKey)
68
78
self . endpointType = endpointType
69
- try addInterceptors ( authService : authService , apiAuthProviderFactory : apiAuthProviderFactory )
79
+ self . apiKey = apiKey
70
80
}
71
81
72
- public mutating func addInterceptor( interceptor: URLRequestInterceptor ) {
73
- interceptors. append ( interceptor)
74
- }
75
-
76
- // MARK: Private
77
-
78
- /// Adds auto-discovered interceptors. Currently only works for authorization interceptors
79
- private mutating func addInterceptors( authService: AWSAuthServiceBehavior ? = nil ,
80
- apiAuthProviderFactory: APIAuthProviderFactory ) throws {
81
- switch authorizationConfiguration {
82
- case . none:
83
- // No interceptors needed
84
- break
85
- case . apiKey( let apiKeyConfig) :
86
- let provider = BasicAPIKeyProvider ( apiKey: apiKeyConfig. apiKey)
87
- let interceptor = APIKeyURLRequestInterceptor ( apiKeyProvider: provider)
88
- addInterceptor ( interceptor: interceptor)
89
- case . awsIAM( let iamConfig) :
90
- guard let authService = authService else {
91
- throw PluginError . pluginConfigurationError ( " AuthService is not set for IAM " ,
92
- " " )
93
- }
94
- let provider = BasicIAMCredentialsProvider ( authService: authService)
95
- let interceptor = IAMURLRequestInterceptor ( iamCredentialsProvider: provider,
96
- region: iamConfig. region,
97
- endpointType: endpointType)
98
- addInterceptor ( interceptor: interceptor)
99
- case . amazonCognitoUserPools:
100
- guard let authService = authService else {
101
- throw PluginError . pluginConfigurationError ( " AuthService not set for cognito user pools " ,
102
- " " )
103
- }
104
- let provider = BasicUserPoolTokenProvider ( authService: authService)
105
- let interceptor = UserPoolURLRequestInterceptor ( userPoolTokenProvider: provider)
106
- addInterceptor ( interceptor: interceptor)
107
- case . openIDConnect:
108
- guard let oidcAuthProvider = apiAuthProviderFactory. oidcAuthProvider ( ) else {
109
- return
110
- }
111
- let wrappedAuthProvider = AuthTokenProviderWrapper ( oidcAuthProvider: oidcAuthProvider)
112
- let interceptor = UserPoolURLRequestInterceptor ( userPoolTokenProvider: wrappedAuthProvider)
113
- addInterceptor ( interceptor: interceptor)
114
- }
82
+ public func authorizationConfigurationFor( authType: AWSAuthorizationType ) throws -> AWSAuthorizationConfiguration {
83
+ return try AWSAuthorizationConfiguration . makeConfiguration ( authType: authType,
84
+ region: region,
85
+ apiKey: apiKey)
115
86
}
116
87
117
88
// MARK: - Configuration file helpers
@@ -142,23 +113,6 @@ public extension AWSAPICategoryPluginConfiguration {
142
113
return baseURL
143
114
}
144
115
145
- private static func getRegion( from endpointJSON: [ String : JSONValue ] ) throws -> AWSRegionType ? {
146
- let region : AWSRegionType ?
147
-
148
- if case . string( let endpointRegion) = endpointJSON [ " region " ] {
149
- let regionType = endpointRegion. aws_regionTypeValue ( )
150
- guard regionType != AWSRegionType . Unknown else {
151
- return nil
152
- }
153
-
154
- region = regionType
155
- } else {
156
- region = nil
157
- }
158
-
159
- return region
160
- }
161
-
162
116
private static func getEndpointType( from endpointJSON: [ String : JSONValue ] ) throws ->
163
117
AWSAPICategoryPluginEndpointType {
164
118
@@ -187,99 +141,64 @@ public extension AWSAPICategoryPluginConfiguration {
187
141
188
142
return endpointType
189
143
}
144
+ }
145
+ }
190
146
191
- private static func getAuthorizationType(
192
- from endpointJSON: [ String : JSONValue ]
193
- ) throws -> AWSAuthorizationType {
194
- guard case . string( let authorizationTypeString) = endpointJSON [ " authorizationType " ] else {
195
- throw PluginError . pluginConfigurationError (
196
- " Could not get `AuthorizationType` from plugin configuration " ,
197
- """
198
- The specified configuration does not have a string with the key `AuthorizationType`. Review the \
199
- configuration and ensure it contains the expected values:
200
- \( endpointJSON)
201
- """
202
- )
203
- }
204
-
205
- guard let authorizationType = AWSAuthorizationType ( rawValue: authorizationTypeString) else {
206
- let authTypes = AWSAuthorizationType . allCases. map { $0. rawValue } . joined ( separator: " , " )
207
- throw PluginError . pluginConfigurationError (
208
- " Could not convert ` \( authorizationTypeString) ` to an AWSAuthorizationType " ,
209
- """
210
- The " authorizationType " value in the specified configuration cannot be converted to an \
211
- AWSAuthorizationType. Review the configuration and ensure it contains a valid value \
212
- ( \( authTypes) ):
213
- \( endpointJSON)
214
- """
215
- )
216
- }
147
+ // MARK: - AWSRegionType + fromEndpointJSON
217
148
218
- return authorizationType
219
- }
149
+ private extension AWSRegionType {
150
+ static func region( from endpointJSON: [ String : JSONValue ] ) throws -> AWSRegionType ? {
151
+ let region : AWSRegionType ?
220
152
221
- // TODO: Refactor auth configuration creation into separate files--this file is for endpoint configs
222
- // https://github.com/aws-amplify/amplify-ios/issues/73
223
- private static func getAuthorizationConfiguration( from endpointJSON: [ String : JSONValue ] )
224
- throws -> AWSAuthorizationConfiguration {
225
- let authType = try getAuthorizationType ( from: endpointJSON)
226
-
227
- switch authType {
228
- case . none:
229
- return . none
230
- case . apiKey:
231
- return try apiKeyAuthorizationConfiguration ( from: endpointJSON)
232
- case . awsIAM:
233
- return try awsIAMAuthorizationConfiguration ( from: endpointJSON)
234
- case . openIDConnect:
235
- return try oidcAuthorizationConfiguration ( from: endpointJSON)
236
- case . amazonCognitoUserPools:
237
- return try userPoolsAuthorizationConfiguration ( from: endpointJSON)
153
+ if case . string( let endpointRegion) = endpointJSON [ " region " ] {
154
+ let regionType = endpointRegion. aws_regionTypeValue ( )
155
+ guard regionType != AWSRegionType . Unknown else {
156
+ return nil
238
157
}
239
158
159
+ region = regionType
160
+ } else {
161
+ region = nil
240
162
}
241
163
242
- private static func apiKeyAuthorizationConfiguration( from endpointJSON: [ String : JSONValue ] )
243
- throws -> AWSAuthorizationConfiguration {
244
-
245
- guard case . string( let apiKey) = endpointJSON [ " apiKey " ] else {
246
- throw PluginError . pluginConfigurationError (
247
- " Could not get `ApiKey` from plugin configuration " ,
248
- """
249
- The specified configuration does not have a string with the key `ApiKey`. Review the \
250
- configuration and ensure it contains the expected values:
251
- \( endpointJSON)
252
- """
253
- )
254
- }
255
-
256
- let config = APIKeyConfiguration ( apiKey: apiKey)
257
- return . apiKey( config)
258
- }
164
+ return region
165
+ }
166
+ }
259
167
260
- static func awsIAMAuthorizationConfiguration( from endpointJSON: [ String : JSONValue ] )
261
- throws -> AWSAuthorizationConfiguration {
262
- let regionOptional = try EndpointConfig . getRegion ( from: endpointJSON)
263
- guard let region = regionOptional else {
264
- throw PluginError . pluginConfigurationError ( " Region is not set for IAM " ,
265
- " Set the region " )
266
- }
267
- return . awsIAM( AWSIAMConfiguration ( region: region) )
268
- }
168
+ // MARK: - AWSAuthorizationType + fromEndpointJSON
269
169
270
- static func oidcAuthorizationConfiguration( from endpointJSON: [ String : JSONValue ] )
271
- throws -> AWSAuthorizationConfiguration {
272
- return . openIDConnect( OIDCConfiguration ( ) )
170
+ private extension AWSAuthorizationType {
171
+ static func from( endpointJSON: [ String : JSONValue ] ) throws -> AWSAuthorizationType {
172
+ guard case . string( let authorizationTypeString) = endpointJSON [ " authorizationType " ] else {
173
+ throw PluginError . pluginConfigurationError (
174
+ " Could not get `AuthorizationType` from plugin configuration " ,
175
+ """
176
+ The specified configuration does not have a string with the key `AuthorizationType`. Review the \
177
+ configuration and ensure it contains the expected values:
178
+ \( endpointJSON)
179
+ """
180
+ )
273
181
}
274
182
275
- static func userPoolsAuthorizationConfiguration( from endpointJSON: [ String : JSONValue ] )
276
- throws -> AWSAuthorizationConfiguration {
277
- return . amazonCognitoUserPools( CognitoUserPoolsConfiguration ( ) )
183
+ guard let authorizationType = AWSAuthorizationType ( rawValue: authorizationTypeString) else {
184
+ let authTypes = AWSAuthorizationType . allCases. map { $0. rawValue } . joined ( separator: " , " )
185
+ throw PluginError . pluginConfigurationError (
186
+ " Could not convert ` \( authorizationTypeString) ` to an AWSAuthorizationType " ,
187
+ """
188
+ The " authorizationType " value in the specified configuration cannot be converted to an \
189
+ AWSAuthorizationType. Review the configuration and ensure it contains a valid value \
190
+ ( \( authTypes) ):
191
+ \( endpointJSON)
192
+ """
193
+ )
278
194
}
279
195
196
+ return authorizationType
280
197
}
281
198
}
282
199
200
+ // MARK: - Dictionary + AWSAPICategoryPluginConfiguration.EndpointConfig
201
+
283
202
extension Dictionary where Key == String , Value == AWSAPICategoryPluginConfiguration . EndpointConfig {
284
203
285
204
/// Getting the `EndpointConfig` resolves to the following rules:
0 commit comments