Skip to content

Commit

Permalink
Merge branch 'fix-network-requests-may-not-complete' into 'master'
Browse files Browse the repository at this point in the history
Fix network requests may not complete when no access token available

See merge request pace/mobile/ios/pace-cloud-sdk!424
  • Loading branch information
Carl Philipp Knoblauch committed Aug 20, 2024
2 parents e536999 + 4c667b5 commit 71376bc
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 108 deletions.
53 changes: 26 additions & 27 deletions PACECloudSDK/API/Fueling/Generated/FuelingAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ public class FuelingAPIClient {
IDKit.refreshToken { [weak self] result in
guard let self else { return }
guard case let .failure(error) = result else {
guard case let .success(accessToken) = result,
let accessToken else { return }
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
guard case let .success(accessToken) = result else { return }
if let accessToken {
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
}

self.validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
Expand All @@ -107,45 +109,42 @@ public class FuelingAPIClient {
complete: complete)
return
}

if case .failedTokenRefresh = error {
completionQueue.async {
let response = FuelingAPIResponse<T>(request: request,
result: .failure(APIClientError
.unexpectedStatusCode(statusCode: 401,
data: Data("UNAUTHORIZED".utf8))))
result: .failure(APIClientError
.unexpectedStatusCode(statusCode: 401,
data: Data("UNAUTHORIZED".utf8))))
complete(response)
}
} else {
completionQueue.async {
let response = FuelingAPIResponse<T>(request: request,
result: .failure(APIClientError.unknownError(error)))
result: .failure(APIClientError.unknownError(error)))
complete(response)
}
}
}
} else if let accessToken = API.accessToken {

return cancellableRequest

}

if let accessToken = API.accessToken {
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
self.validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)
}
} else {
validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)
}

validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)

return cancellableRequest
}
Expand Down
53 changes: 26 additions & 27 deletions PACECloudSDK/API/POI/Generated/POIAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ public class POIAPIClient {
IDKit.refreshToken { [weak self] result in
guard let self else { return }
guard case let .failure(error) = result else {
guard case let .success(accessToken) = result,
let accessToken else { return }
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
guard case let .success(accessToken) = result else { return }
if let accessToken {
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
}

self.validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
Expand All @@ -107,45 +109,42 @@ public class POIAPIClient {
complete: complete)
return
}

if case .failedTokenRefresh = error {
completionQueue.async {
let response = POIAPIResponse<T>(request: request,
result: .failure(APIClientError
.unexpectedStatusCode(statusCode: 401,
data: Data("UNAUTHORIZED".utf8))))
result: .failure(APIClientError
.unexpectedStatusCode(statusCode: 401,
data: Data("UNAUTHORIZED".utf8))))
complete(response)
}
} else {
completionQueue.async {
let response = POIAPIResponse<T>(request: request,
result: .failure(APIClientError.unknownError(error)))
result: .failure(APIClientError.unknownError(error)))
complete(response)
}
}
}
} else if let accessToken = API.accessToken {

return cancellableRequest

}

if let accessToken = API.accessToken {
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
self.validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)
}
} else {
validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)
}

validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)

return cancellableRequest
}
Expand Down
53 changes: 26 additions & 27 deletions PACECloudSDK/API/Pay/Generated/PayAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ public class PayAPIClient {
IDKit.refreshToken { [weak self] result in
guard let self else { return }
guard case let .failure(error) = result else {
guard case let .success(accessToken) = result,
let accessToken else { return }
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
guard case let .success(accessToken) = result else { return }
if let accessToken {
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
}

self.validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
Expand All @@ -107,45 +109,42 @@ public class PayAPIClient {
complete: complete)
return
}

if case .failedTokenRefresh = error {
completionQueue.async {
let response = PayAPIResponse<T>(request: request,
result: .failure(APIClientError
.unexpectedStatusCode(statusCode: 401,
data: Data("UNAUTHORIZED".utf8))))
result: .failure(APIClientError
.unexpectedStatusCode(statusCode: 401,
data: Data("UNAUTHORIZED".utf8))))
complete(response)
}
} else {
completionQueue.async {
let response = PayAPIResponse<T>(request: request,
result: .failure(APIClientError.unknownError(error)))
result: .failure(APIClientError.unknownError(error)))
complete(response)
}
}
}
} else if let accessToken = API.accessToken {

return cancellableRequest

}

if let accessToken = API.accessToken {
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
self.validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)
}
} else {
validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)
}

validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)

return cancellableRequest
}
Expand Down
53 changes: 26 additions & 27 deletions PACECloudSDK/API/User/Generated/UserAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ public class UserAPIClient {
IDKit.refreshToken { [weak self] result in
guard let self else { return }
guard case let .failure(error) = result else {
guard case let .success(accessToken) = result,
let accessToken else { return }
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
guard case let .success(accessToken) = result else { return }
if let accessToken {
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
}

self.validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
Expand All @@ -107,45 +109,42 @@ public class UserAPIClient {
complete: complete)
return
}

if case .failedTokenRefresh = error {
completionQueue.async {
let response = UserAPIResponse<T>(request: request,
result: .failure(APIClientError
.unexpectedStatusCode(statusCode: 401,
data: Data("UNAUTHORIZED".utf8))))
result: .failure(APIClientError
.unexpectedStatusCode(statusCode: 401,
data: Data("UNAUTHORIZED".utf8))))
complete(response)
}
} else {
completionQueue.async {
let response = UserAPIResponse<T>(request: request,
result: .failure(APIClientError.unknownError(error)))
result: .failure(APIClientError.unknownError(error)))
complete(response)
}
}
}
} else if let accessToken = API.accessToken {

return cancellableRequest

}

if let accessToken = API.accessToken {
urlRequest.setValue("Bearer \(accessToken)",
forHTTPHeaderField: HttpHeaderFields.authorization.rawValue)
self.validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)
}
} else {
validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)
}

validateNetworkRequest(request: request,
urlRequest: urlRequest,
cancellableRequest: cancellableRequest,
requestBehaviour: requestBehaviour,
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount,
currentRetryCount: currentRetryCount,
completionQueue: completionQueue,
complete: complete)

return cancellableRequest
}
Expand Down

0 comments on commit 71376bc

Please sign in to comment.