Created
June 29, 2017 13:25
-
-
Save chris-carneiro/af49a9894c5da07020b7801e54666ebc to your computer and use it in GitHub Desktop.
Token interceptor for okhttp 3
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
| public class TokenInterceptor implements Interceptor { | |
| private static final String TAG = TokenManager.class.getName(); | |
| private static final String header = "X-Header"; | |
| /** | |
| * Constructor for Token interceptor | |
| */ | |
| public TokenInterceptor() { | |
| } | |
| @Override | |
| public Response intercept(Chain chain) throws IOException { | |
| Log.e(TAG, "START INTERCEPT"); | |
| Request original = chain.request(); | |
| Request modifiedRequest = original; | |
| if (TokenManager.getInstance().hasToken()) { | |
| modifiedRequest = original.newBuilder() | |
| .addHeader(header, TokenManager.getInstance().getToken()) | |
| .build(); | |
| } | |
| Response response = chain.proceed(modifiedRequest); | |
| Log.e(TAG, "END INTERCEPT : "+response.body()); | |
| return response; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment