Created
March 15, 2022 19:24
-
-
Save malcommac/be151a8a7c7907b6dc0a2fccb12bb8a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func fetch() async throws { | |
// prepare the request and execute it | |
let task = try await request.urlSessionTask(inClient: client) | |
let response = try await fetch(request, task: sessionTask: task) | |
// ask to validator the action to perform on this request | |
let action = client.validate(response: response, forRequest: request) | |
switch action { | |
case .failChain(let error): | |
return HTTPResponse(error: error) // fail with error | |
case .retry(let strategy): | |
if request.isAltRequest || request.reachedMaxRetries { | |
// alt request cannot be retried to avoid infinite loops | |
return response | |
} else { | |
// perform retry strategy | |
let retryResponse = try await performRetryStrategy(strategy, | |
forRequest: request, task: sessionTask, | |
withResponse: response) | |
return retryResponse | |
} | |
case .nextValidator: | |
return response // validation okay | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment