Skip to content

Commit

Permalink
refactor code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Di Wu committed Sep 6, 2023
1 parent 7c2b7ab commit e9d0d5d
Showing 1 changed file with 99 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,81 @@ final public class AWSGraphQLOperation<R: Decodable>: GraphQLOperation<R> {
return
}

// Validate the request
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)
}
}

// 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)
}
}

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

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))
cancel()
}
}

}

private func validateRequest(_ request: GraphQLOperationRequest<R>) -> Result<GraphQLOperationRequest<R>, APIError> {
do {
try request.validate()
return .success(request)
} catch let error as APIError {
dispatch(result: .failure(error))
finish()
return
return .failure(error)
} catch {
dispatch(result: .failure(APIError.unknown("Could not validate request", "", nil)))
finish()
return
return .failure(APIError.unknown("Could not validate request", "", nil))
}
}

// Retrieve endpoint configuration
let endpointConfig: AWSAPICategoryPluginConfiguration.EndpointConfig
let requestInterceptors: AWSAPIEndpointInterceptors?

do {
endpointConfig = try pluginConfig.endpoints.getConfig(for: request.apiName, endpointType: .graphQL)

if let pluginOptions = request.options.pluginOptions as? AWSPluginOptions,
let authType = pluginOptions.authType {
requestInterceptors = try pluginConfig.interceptorsForEndpoint(withConfig: endpointConfig,
authType: authType)
} else {
requestInterceptors = pluginConfig.interceptorsForEndpoint(withConfig: endpointConfig)
private func buildURLRequest(from request: GraphQLOperationRequest<R>) -> Result<URLRequest, APIError> {
getEndpointConfig(from: request).flatMap { endpointConfig in
getRequestPayload(from: request).map { requestPayload in
GraphQLOperationRequestUtils.constructRequest(
with: endpointConfig.baseURL,
requestPayload: requestPayload
)
}
} catch let error as APIError {
dispatch(result: .failure(error))
finish()
return
} catch {
dispatch(result: .failure(APIError.unknown("Could not get endpoint configuration", "", nil)))
finish()
return
}
}

private func getRequestPayload(from request: GraphQLOperationRequest<R>) -> Result<Data, APIError> {
// Prepare request payload
let queryDocument = GraphQLOperationRequestUtils.getQueryDocument(document: request.document,
variables: request.variables)
Expand All @@ -87,63 +125,46 @@ final public class AWSGraphQLOperation<R: Decodable>: GraphQLOperation<R> {
let prettyPrintedQueryDocument = String(data: serializedJSON, encoding: .utf8) {
Amplify.API.log.verbose("\(prettyPrintedQueryDocument)")
}
let requestPayload: Data

do {
requestPayload = try JSONSerialization.data(withJSONObject: queryDocument)
return .success(try JSONSerialization.data(withJSONObject: queryDocument))
} catch {
dispatch(result: .failure(APIError.operationError("Failed to serialize query document",
"fix the document or variables",
error)))
finish()
return
return .failure(APIError.operationError(
"Failed to serialize query document",
"fix the document or variables",
error
))
}
}

// Create request
let urlRequest = GraphQLOperationRequestUtils.constructRequest(with: endpointConfig.baseURL,
requestPayload: requestPayload)
let amplifyInterceptors = requestInterceptors?.amplifyInterceptors ?? []
let customerInterceptors = requestInterceptors?.interceptors ?? []
let checksumInterceptors = requestInterceptors?.checksumInterceptors ?? []
Task {
var finalResult: Result<URLRequest, APIError> = .success(urlRequest)
// apply amplify interceptors
for interceptor in amplifyInterceptors {
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)
}
}
private func getEndpointConfig(from request: GraphQLOperationRequest<R>) -> Result<AWSAPICategoryPluginConfiguration.EndpointConfig, APIError> {
do {
return .success(try pluginConfig.endpoints.getConfig(for: request.apiName, endpointType: .graphQL))
} catch let error as APIError {
return .failure(error)

// apply checksum interceptor
for interceptor in checksumInterceptors {
finalResult = await finalResult.flatMapAsync { request in
await applyInterceptor(interceptor, request: request)
}
}
} catch {
return .failure(APIError.unknown("Could not get endpoint configuration", "", nil))
}
}

switch finalResult {
case .success(let finalRequest):
if isCancelled {
finish()
return
private func getEndpointInterceptors(from request: GraphQLOperationRequest<R>) -> Result<AWSAPIEndpointInterceptors?, APIError> {
getEndpointConfig(from: request).flatMap { endpointConfig in
do {
if let pluginOptions = request.options.pluginOptions as? AWSPluginOptions,
let authType = pluginOptions.authType
{
return .success(try pluginConfig.interceptorsForEndpoint(
withConfig: endpointConfig,
authType: authType
))
} else {
return .success(pluginConfig.interceptorsForEndpoint(withConfig: endpointConfig))
}

// 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))
cancel()
} catch let error as APIError {
return .failure(error)
} catch {
return .failure(APIError.unknown("Could not get endpoint interceptors", "", nil))
}
}
}
Expand Down

0 comments on commit e9d0d5d

Please sign in to comment.