Last active
December 7, 2017 17:31
-
-
Save nderkach/5e054375fc04e7b78a2705aaea734040 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
func refreshAuth(_ username: String, _ password: String) -> Request { | |
return self.login(username, password, onSuccess: { | |
}, onFailure: { error in | |
}) | |
} | |
func refreshTokenOnAuthFailure(request: Siesta.Request) -> Request { | |
return request.chained { | |
guard case .failure(let error) = $0.response, // Did request fail… | |
error.httpStatusCode == 401 else { // …because of expired token? | |
return .useThisResponse // If not, use the response we got. | |
} | |
return .passTo( | |
self.refreshAuth("test", "test").chained { // If so, first request a new token, then: | |
if case .failure = $0.response { // If token request failed… | |
return .useThisResponse // …report that error. | |
} else { | |
return .passTo(request.repeated()) // We have a new token! Repeat the original request. | |
} | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment