Skip to content

Commit

Permalink
fix: Retry requests with 202 status code and Retry-After header (bo…
Browse files Browse the repository at this point in the history
  • Loading branch information
box-sdk-build authored Jul 26, 2024
1 parent 18a6c68 commit 64f27b3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "engineHash": "47da32c", "specHash": "d36b9f0", "version": "0.3.0" }
{ "engineHash": "871ac70", "specHash": "d36b9f0", "version": "0.3.0" }
2 changes: 1 addition & 1 deletion BoxSdkGen.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Pod::Spec.new do |spec|
spec.tvos.deployment_target = '13.0'
spec.watchos.deployment_target = '6.0'
spec.visionos.deployment_target = '1.0'
spec.source = { :git => 'https://github.com/box/box-swift-sdk-gen.git', :tag => 'v'+spec.version.to_s }
spec.source = { :git => 'https://github.com/box/box-swift-sdk-gen.git', :tag => spec.version.to_s }
spec.swift_versions = ['5']
spec.requires_arc = true
spec.source_files = 'Sources/**/*.swift'
Expand Down
9 changes: 5 additions & 4 deletions Sources/Networking/NetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class NetworkClient {
/// - Parameters:
/// - urlRequest: The request object.
/// - networkSession: The Networking Session object which provides the URLSession object along with a network configuration parameters used in network communication.
/// - Returns: Tuple of of (Data, URLReponse)
/// - Returns: Tuple of of (Data, URLResponse)
/// - Throws: An error if the request fails for any reason.
private func sendDataRequest(_ urlRequest: URLRequest, networkSession: NetworkSession) async throws -> (Data, URLResponse) {
return try await withCheckedThrowingContinuation { continuation in
Expand Down Expand Up @@ -104,7 +104,7 @@ public class NetworkClient {
/// - Parameters:
/// - urlRequest: The request object.
/// - networkSession: The Networking Session object which provides the URLSession object along with a network configuration parameters used in network communication.
/// - Returns: Tuple of of (URL, URLReponse)
/// - Returns: Tuple of of (URL, URLResponse)
/// - Throws: An error if the request fails for any reason.
private func sendDownloadRequest(_ urlRequest: URLRequest, downloadDestinationURL: URL, networkSession: NetworkSession) async throws -> (URL, URLResponse) {
return try await withCheckedThrowingContinuation { continuation in
Expand Down Expand Up @@ -318,9 +318,10 @@ public class NetworkClient {
attempt: Int
) async throws -> FetchResponse {
let statusCode = conversation.urlResponse.statusCode
let isStatusCodeAcceptedWithRetryAfterHeader = statusCode == 202 && conversation.urlResponse.value(forHTTPHeaderField: HTTPHeaderKey.retryAfter) != nil

// OK
if statusCode >= 200 && statusCode < 400 {
if statusCode >= 200 && statusCode < 400 && !isStatusCodeAcceptedWithRetryAfterHeader {
return conversation.convertToFetchResponse()
}

Expand All @@ -336,7 +337,7 @@ public class NetworkClient {
}

// Retryable
if statusCode == 429 || statusCode >= 500 {
if statusCode == 429 || statusCode >= 500 || isStatusCodeAcceptedWithRetryAfterHeader {
let retryTimeout = Double(conversation.urlResponse.value(forHTTPHeaderField: HTTPHeaderKey.retryAfter) ?? "")
?? networkSession.networkSettings.retryStrategy.getRetryTimeout(attempt: attempt)
try await wait(seconds: retryTimeout)
Expand Down

0 comments on commit 64f27b3

Please sign in to comment.