Skip to content

Commit

Permalink
update interceptor names
Browse files Browse the repository at this point in the history
  • Loading branch information
Di Wu committed Sep 7, 2023
1 parent 319e2e4 commit a6e0df8
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ struct AWSAPIEndpointInterceptors {
let apiAuthProviderFactory: APIAuthProviderFactory
let authService: AWSAuthServiceBehavior?

var amplifyInterceptors: [URLRequestInterceptor] = []

var checksumInterceptors: [URLRequestInterceptor] = []
/// The order of interceptor decoration is as follows:
/// 1. **prelude interceptors**: append default auth info
/// 2. **cutomize headers**: headers provided when constructing the request
/// 3. **customer interceptors**: customer defined interceptors
/// 4. **postlude interceptors**: calculate checksums, signatures
var preludeInterceptors: [URLRequestInterceptor] = []

var interceptors: [URLRequestInterceptor] = []

var postludeInterceptors: [URLRequestInterceptor] = []

init(endpointName: APIEndpointName,
apiAuthProviderFactory: APIAuthProviderFactory,
authService: AWSAuthServiceBehavior? = nil) {
Expand All @@ -46,7 +51,7 @@ struct AWSAPIEndpointInterceptors {
case .apiKey(let apiKeyConfig):
let provider = BasicAPIKeyProvider(apiKey: apiKeyConfig.apiKey)
let interceptor = APIKeyURLRequestInterceptor(apiKeyProvider: provider)
amplifyInterceptors.append(interceptor)
preludeInterceptors.append(interceptor)
case .awsIAM(let iamConfig):
guard let authService = authService else {
throw PluginError.pluginConfigurationError("AuthService is not set for IAM",
Expand All @@ -56,31 +61,31 @@ struct AWSAPIEndpointInterceptors {
let interceptor = IAMURLRequestInterceptor(iamCredentialsProvider: provider,
region: iamConfig.region,
endpointType: endpointType)
checksumInterceptors.append(interceptor)
postludeInterceptors.append(interceptor)
case .amazonCognitoUserPools:
guard let authService = authService else {
throw PluginError.pluginConfigurationError("AuthService not set for cognito user pools",
"")
}
let provider = BasicUserPoolTokenProvider(authService: authService)
let interceptor = AuthTokenURLRequestInterceptor(authTokenProvider: provider)
amplifyInterceptors.append(interceptor)
preludeInterceptors.append(interceptor)
case .openIDConnect:
guard let oidcAuthProvider = apiAuthProviderFactory.oidcAuthProvider() else {
throw PluginError.pluginConfigurationError("AuthService not set for OIDC",
"Provide an AmplifyOIDCAuthProvider via API plugin configuration")
}
let wrappedAuthProvider = AuthTokenProviderWrapper(tokenAuthProvider: oidcAuthProvider)
let interceptor = AuthTokenURLRequestInterceptor(authTokenProvider: wrappedAuthProvider)
amplifyInterceptors.append(interceptor)
preludeInterceptors.append(interceptor)
case .function:
guard let functionAuthProvider = apiAuthProviderFactory.functionAuthProvider() else {
throw PluginError.pluginConfigurationError("AuthService not set for function auth",
"Provide an AmplifyFunctionAuthProvider via API plugin configuration")
}
let wrappedAuthProvider = AuthTokenProviderWrapper(tokenAuthProvider: functionAuthProvider)
let interceptor = AuthTokenURLRequestInterceptor(authTokenProvider: wrappedAuthProvider)
amplifyInterceptors.append(interceptor)
preludeInterceptors.append(interceptor)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,63 +34,65 @@ final public class AWSGraphQLOperation<R: Decodable>: GraphQLOperation<R> {
}

override public func main() {
Task { await mainAsync() }
}

private func mainAsync() async {
Amplify.API.log.debug("Starting \(request.operationType) \(id)")

if isCancelled {
finish()
return
}

Task {
let urlRequest = validateRequest(request).flatMap(buildURLRequest(from:))
let finalRequest = await getEndpointInterceptors(from: request).flatMapAsync { requestInterceptors in
var finalResult = urlRequest
let amplifyInterceptors = requestInterceptors?.amplifyInterceptors ?? []
let customerInterceptors = requestInterceptors?.interceptors ?? []
let checksumInterceptors = requestInterceptors?.checksumInterceptors ?? []
// apply amplify interceptors
for interceptor in amplifyInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
let urlRequest = validateRequest(request).flatMap(buildURLRequest(from:))
let finalRequest = await getEndpointInterceptors(from: request).flatMapAsync { requestInterceptors in
let preludeInterceptors = requestInterceptors?.preludeInterceptors ?? []
let customerInterceptors = requestInterceptors?.interceptors ?? []
let postludeInterceptors = requestInterceptors?.postludeInterceptors ?? []

var finalResult = urlRequest
// apply prelude interceptors
for interceptor in preludeInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
}

// there is no customer headers for GraphQLOperationRequest

// apply customer interceptors
for interceptor in customerInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
}
// there is no customize headers for GraphQLOperationRequest

// apply checksum interceptor
for interceptor in checksumInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
// apply customer interceptors
for interceptor in customerInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
return finalResult
}

switch finalRequest {
case .success(let finalRequest):
if isCancelled {
finish()
return
// apply postlude interceptor
for interceptor in postludeInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
}
return finalResult
}

// Begin network task
Amplify.API.log.debug("Starting network task for \(request.operationType) \(id)")
let task = session.dataTaskBehavior(with: finalRequest)
mapper.addPair(operation: self, task: task)
task.resume()
case .failure(let error):
dispatch(result: .failure(error))
switch finalRequest {
case .success(let finalRequest):
if isCancelled {
finish()
return
}
}

// Begin network task
Amplify.API.log.debug("Starting network task for \(request.operationType) \(id)")
let task = session.dataTaskBehavior(with: finalRequest)
mapper.addPair(operation: self, task: task)
task.resume()
case .failure(let error):
dispatch(result: .failure(error))
finish()
}
}

private func validateRequest(_ request: GraphQLOperationRequest<R>) -> Result<GraphQLOperationRequest<R>, APIError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,69 +40,71 @@ final public class AWSRESTOperation: AmplifyOperation<

/// The work to execute for this operation
override public func main() {
Task { await mainAsync() }
}

private func mainAsync() async {
if isCancelled {
finish()
return
}

Task {
let urlRequest = validateRequest(request).flatMap(buildURLRequest(from:))
let finalRequest = await getEndpointConfig(from: request).flatMapAsync { endpointConfig in
let interceptorConfig = pluginConfig.interceptorsForEndpoint(withConfig: endpointConfig)
let amplifyInterceptors = interceptorConfig?.amplifyInterceptors ?? []
let customerInterceptors = interceptorConfig?.interceptors ?? []
let checksumInterceptors = interceptorConfig?.checksumInterceptors ?? []

var finalResult = urlRequest
// apply amplify interceptors
for interceptor in amplifyInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
}

// apply customer headers
finalResult = finalResult.map { urlRequest in
var mutableRequest = urlRequest
for (key, value) in request.headers ?? [:] {
mutableRequest.setValue(value, forHTTPHeaderField: key)
}
return mutableRequest
let urlRequest = validateRequest(request).flatMap(buildURLRequest(from:))
let finalRequest = await getEndpointConfig(from: request).flatMapAsync { endpointConfig in
let interceptorConfig = pluginConfig.interceptorsForEndpoint(withConfig: endpointConfig)
let preludeInterceptors = interceptorConfig?.preludeInterceptors ?? []
let customerInterceptors = interceptorConfig?.interceptors ?? []
let postludeInterceptors = interceptorConfig?.postludeInterceptors ?? []

var finalResult = urlRequest
// apply prelude interceptors
for interceptor in preludeInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
}

// apply customer interceptors
for interceptor in customerInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
// apply customize headers
finalResult = finalResult.map { urlRequest in
var mutableRequest = urlRequest
for (key, value) in request.headers ?? [:] {
mutableRequest.setValue(value, forHTTPHeaderField: key)
}
return mutableRequest
}

// apply checksum interceptor
for interceptor in checksumInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
// apply customer interceptors
for interceptor in customerInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
return finalResult
}

switch finalRequest {
case .success(let finalRequest):
if isCancelled {
finish()
return
// apply postlude interceptor
for interceptor in postludeInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
}
return finalResult
}

// Begin network task
Amplify.API.log.debug("Starting network task for \(request.operationType) \(id)")
let task = session.dataTaskBehavior(with: finalRequest)
mapper.addPair(operation: self, task: task)
task.resume()
case .failure(let error):
Amplify.API.log.debug("Dispatching error \(error)")
dispatch(result: .failure(error))
switch finalRequest {
case .success(let finalRequest):
if isCancelled {
finish()
return
}

// Begin network task
Amplify.API.log.debug("Starting network task for \(request.operationType) \(id)")
let task = session.dataTaskBehavior(with: finalRequest)
mapper.addPair(operation: self, task: task)
task.resume()
case .failure(let error):
Amplify.API.log.debug("Dispatching error \(error)")
dispatch(result: .failure(error))
finish()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class GraphQLConnectionScenario4Tests: XCTestCase {
}
let predicate = field("postID").eq(post.id)
var results: List<Comment4>?
let result = try await Amplify.API.query(request: .list(Comment4.self, where: predicate, limit: 1))
let result = try await Amplify.API.query(request: .list(Comment4.self, where: predicate, limit: 3000))
switch result {
case .success(let comments):
results = comments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ class AWSAPICategoryPluginInterceptorBehaviorTests: AWSAPICategoryPluginTestBase

func testAddInterceptor() throws {
XCTAssertNotNil(apiPlugin.pluginConfig.endpoints[apiName])
XCTAssertEqual(apiPlugin.pluginConfig.interceptorsForEndpoint(named: apiName)?.amplifyInterceptors.count, 0)
XCTAssertEqual(apiPlugin.pluginConfig.interceptorsForEndpoint(named: apiName)?.preludeInterceptors.count, 0)
XCTAssertEqual(apiPlugin.pluginConfig.interceptorsForEndpoint(named: apiName)?.interceptors.count, 0)
XCTAssertEqual(apiPlugin.pluginConfig.interceptorsForEndpoint(named: apiName)?.checksumInterceptors.count, 0)
XCTAssertEqual(apiPlugin.pluginConfig.interceptorsForEndpoint(named: apiName)?.postludeInterceptors.count, 0)

let provider = BasicUserPoolTokenProvider(authService: authService)
let requestInterceptor = AuthTokenURLRequestInterceptor(authTokenProvider: provider)
try apiPlugin.add(interceptor: requestInterceptor, for: apiName)

XCTAssertEqual(apiPlugin.pluginConfig.interceptorsForEndpoint(named: apiName)?.amplifyInterceptors.count, 0)
XCTAssertEqual(apiPlugin.pluginConfig.interceptorsForEndpoint(named: apiName)?.preludeInterceptors.count, 0)
XCTAssertEqual(apiPlugin.pluginConfig.interceptorsForEndpoint(named: apiName)?.interceptors.count, 1)
XCTAssertEqual(apiPlugin.pluginConfig.interceptorsForEndpoint(named: apiName)?.checksumInterceptors.count, 0)
XCTAssertEqual(apiPlugin.pluginConfig.interceptorsForEndpoint(named: apiName)?.postludeInterceptors.count, 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class AWSAPICategoryPluginConfigurationTests: XCTestCase {
func testAddInterceptors() {
let apiKeyInterceptor = APIKeyURLRequestInterceptor(apiKeyProvider: BasicAPIKeyProvider(apiKey: apiKey))
config?.addInterceptor(apiKeyInterceptor, toEndpoint: graphQLAPI)
XCTAssertEqual(config?.interceptorsForEndpoint(named: graphQLAPI)?.amplifyInterceptors.count, 0)
XCTAssertEqual(config?.interceptorsForEndpoint(named: graphQLAPI)?.preludeInterceptors.count, 0)
XCTAssertEqual(config?.interceptorsForEndpoint(named: graphQLAPI)?.interceptors.count, 1)
XCTAssertEqual(config?.interceptorsForEndpoint(named: graphQLAPI)?.checksumInterceptors.count, 0)
XCTAssertEqual(config?.interceptorsForEndpoint(named: graphQLAPI)?.postludeInterceptors.count, 0)
}

/// Given: multiple interceptors conforming to URLRequestInterceptor and an EndpointConfig
Expand All @@ -71,9 +71,9 @@ class AWSAPICategoryPluginConfigurationTests: XCTestCase {
config?.addInterceptor(apiKeyInterceptor, toEndpoint: graphQLAPI)
config?.addInterceptor(CustomURLInterceptor(), toEndpoint: graphQLAPI)
let interceptors = config?.interceptorsForEndpoint(withConfig: endpointConfig!)
XCTAssertEqual(interceptors!.amplifyInterceptors.count, 0)
XCTAssertEqual(interceptors!.preludeInterceptors.count, 0)
XCTAssertEqual(interceptors!.interceptors.count, 2)
XCTAssertEqual(interceptors!.checksumInterceptors.count, 0)
XCTAssertEqual(interceptors!.postludeInterceptors.count, 0)
}

/// Given: multiple interceptors conforming to URLRequestInterceptor
Expand All @@ -88,8 +88,8 @@ class AWSAPICategoryPluginConfigurationTests: XCTestCase {
let interceptors = try config?.interceptorsForEndpoint(withConfig: endpointConfig!,
authType: .amazonCognitoUserPools)

XCTAssertEqual(interceptors!.amplifyInterceptors.count, 1)
XCTAssertNotNil(interceptors!.amplifyInterceptors[0] as? AuthTokenURLRequestInterceptor)
XCTAssertEqual(interceptors!.preludeInterceptors.count, 1)
XCTAssertNotNil(interceptors!.preludeInterceptors[0] as? AuthTokenURLRequestInterceptor)
XCTAssertNotNil(interceptors!.interceptors[1] as? CustomURLInterceptor)
}

Expand All @@ -103,8 +103,8 @@ class AWSAPICategoryPluginConfigurationTests: XCTestCase {

let interceptors = try config?.interceptorsForEndpoint(withConfig: endpointConfig!, authType: .apiKey)

XCTAssertEqual(interceptors!.amplifyInterceptors.count, 1)
XCTAssertNotNil(interceptors!.amplifyInterceptors[0] as? APIKeyURLRequestInterceptor)
XCTAssertEqual(interceptors!.preludeInterceptors.count, 1)
XCTAssertNotNil(interceptors!.preludeInterceptors[0] as? APIKeyURLRequestInterceptor)
}

// MARK: - Helpers
Expand Down
Loading

0 comments on commit a6e0df8

Please sign in to comment.