Skip to content

Commit

Permalink
Implement timeout for Risk SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
okhan-okbay-cko committed Aug 2, 2024
1 parent b4fd428 commit 1aeb8c1
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions Checkout/Source/Tokenisation/CheckoutAPIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ final public class CheckoutAPIService: CheckoutAPIProtocol {
}
}

let timeoutInterval: TimeInterval = 10
let taskCompletionQueue = DispatchQueue(label: "taskCompletionQueue")
var isTaskCompleted = false

private func createToken(requestParameters: NetworkManager.RequestParameters,
paymentType: TokenRequest.TokenType,
completion: @escaping (Result<TokenDetails, TokenisationError.TokenRequest>) -> Void) {
Expand All @@ -164,19 +168,26 @@ final public class CheckoutAPIService: CheckoutAPIProtocol {
return
}

self.riskSDK.configure { configurationResult in
switch configurationResult {
case .failure:
completion(.success(tokenDetails))
logManager.resetCorrelationID()
case .success():
self.riskSDK.publishData(cardToken: tokenDetails.token) { _ in
logManager.queue(event: .riskSDKCompletion)
completion(.success(tokenDetails))
logManager.resetCorrelationID()
}
}
DispatchQueue.global().asyncAfter(deadline: .now() + timeoutInterval) {
self.taskCompletionQueue.sync { [weak self] in
guard let self else { return }
if !isTaskCompleted {
isTaskCompleted = true
completion(.failure(.networkError(.connectionTimeout)))
}
}
}

callRiskSDK(tokenDetails: tokenDetails) { [weak self] result in
guard let self else { return }
taskCompletionQueue.sync {
if !self.isTaskCompleted {
self.isTaskCompleted = true
}
}
completion(result)
}

case .errorResponse(let errorResponse):
completion(.failure(.serverError(errorResponse)))
logManager.resetCorrelationID()
Expand All @@ -187,6 +198,26 @@ final public class CheckoutAPIService: CheckoutAPIProtocol {
}
}

private func callRiskSDK(tokenDetails: TokenDetails,
completion: @escaping (Result<TokenDetails, TokenisationError.TokenRequest>) -> Void) {
DispatchQueue.global().async {
self.riskSDK.configure { [weak self] configurationResult in
guard let self else { return }
switch configurationResult {
case .failure:
completion(.success(tokenDetails))
logManager.resetCorrelationID()
case .success():
self.riskSDK.publishData(cardToken: tokenDetails.token) { _ in
self.logManager.queue(event: .riskSDKCompletion)
completion(.success(tokenDetails))
self.logManager.resetCorrelationID()
}
}
}
}
}

private func logTokenResponse(tokenResponseResult: NetworkRequestResult<TokenResponse, TokenisationError.ServerError>,
paymentType: TokenRequest.TokenType,
httpURLResponse: HTTPURLResponse?) {
Expand Down

0 comments on commit 1aeb8c1

Please sign in to comment.