Last active
December 18, 2015 06:52
-
-
Save twocity/8344055f9441c508a0ed to your computer and use it in GitHub Desktop.
Auto refresh token with Okhttp's interceptor
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 class RefreshTokenInterceptor implements Interceptor { | |
@Override public Response intercept(Chain chain) throws IOException { | |
Request originRequest = chain.request(); | |
Response response = chain.proceed(originRequest); | |
if (isTokenExpired(response)) { | |
Request newRequest = buildRefreshTokenRequest(originRequest); | |
Response refreshResponse = chain.proceed(newRequest); | |
if (refreshResponse.isSuccessful()) { | |
return chain.proceed(originRequest); // must with the new token | |
} else { | |
// TODO | |
} | |
} | |
return response; | |
} | |
private boolean isTokenExpired(Response response) { | |
// TODO | |
return false; | |
} | |
private Request buildRefreshTokenRequest(Request request) { | |
// TODO | |
return request.newBuilder().build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment