I'm accessing an API where you authorize first, and you get an access token back, which you use in successive calls. At some point (hours later), the access token expires. So I'm caching the token on the server so any users using the web app will initiate API calls using that token.
But when it expires, I do a recursive call after updating the access token. So, for example (in pseudo-JS):
function getDetails (id) {
data = HTTP.get(url, {params: ...});
if (!data.success) {
updateToken(function () {
return getDetails(id);
});
} else { /*.. we're good*/ }
}
There would also be a recursion depth check in there too. If there's a better way to do this, I'd love to hear it. Basically:
- Call API
- (failure)
- Update token
- Call API again