Skip to content

Instantly share code, notes, and snippets.

@twocity
Last active December 18, 2015 06:52
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 twocity/8344055f9441c508a0ed to your computer and use it in GitHub Desktop.
Save twocity/8344055f9441c508a0ed to your computer and use it in GitHub Desktop.
Auto refresh token with Okhttp's interceptor
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