Skip to content

Instantly share code, notes, and snippets.

@malcommac
Created March 15, 2022 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcommac/be151a8a7c7907b6dc0a2fccb12bb8a9 to your computer and use it in GitHub Desktop.
Save malcommac/be151a8a7c7907b6dc0a2fccb12bb8a9 to your computer and use it in GitHub Desktop.
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