Created
June 29, 2017 13:28
-
-
Save chris-carneiro/64d3bfc98bf226db06eeda13bc2306d8 to your computer and use it in GitHub Desktop.
Simple class that adds a secret key to each ws request using okhttp3
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 APIKeyInterceptor implements Interceptor { | |
| private static final String SECRET_KEY_HEADER = "secret_key"; | |
| private static final String UUID_HEADER = "uuid"; | |
| private static final String TAG = "APIKeyInterceptor"; | |
| @Override | |
| public Response intercept(Chain chain) throws IOException { | |
| Request request = chain.request(); | |
| Log.d(TAG, "intercept ===============> "+request.url()); | |
| try { | |
| List<User> users = DatabaseManager.getInstance().getHelper().getUserDAO().queryForAll(); | |
| if (null!=users&&!users.isEmpty()){ | |
| if (request.url().toString().contains("<endpoint>")){ | |
| request = request.newBuilder() | |
| .addHeader(SECRET_KEY_HEADER, BuildConfig.API_SECRET_KEY) | |
| .build(); | |
| }else{ | |
| request = request.newBuilder() | |
| .addHeader(SECRET_KEY_HEADER, BuildConfig.API_SECRET_KEY) | |
| .addHeader(UUID_HEADER, <uuid>) | |
| .build(); | |
| } | |
| } | |
| } catch (SQLException e) { | |
| Log.e(TAG, e.getMessage(), e); | |
| } | |
| return chain.proceed(request); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment