use of com.voipgrid.vialer.util.AccountHelper in project vialer-android by VoIPGRID.
the class ServiceGenerator method getHttpClient.
/**
* Function to create the HttpClient to be used by retrofit for API calls.
* @param context
* @param username
* @param password
* @return
*/
private static OkHttpClient getHttpClient(final Context context, final String username, final String password) {
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder();
if (username != null && password != null) {
requestBuilder.header("Authorization", Credentials.basic(username, password));
}
requestBuilder.header("User-Agent", getUserAgentHeader(context));
if (ConnectivityHelper.get(context).hasNetworkConnection()) {
// read from cache for 1 minute
int maxAge = 60;
requestBuilder.header("Cache-Control", "public, max-age=" + maxAge);
} else {
// tolerate 4-weeks stale
int maxStale = 60 * 60 * 24 * 28;
requestBuilder.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale);
}
Request request = requestBuilder.build();
Response response = chain.proceed(request);
// Check if we get a 401 and are not in the onboarding.
if (response.code() == 401 && !context.getClass().getSimpleName().equals(SetupActivity.class.getSimpleName())) {
new RemoteLogger(ServiceGenerator.class).w("Logged out on 401 API response");
// Clear logged in values.
new JsonStorage(context).clear();
new AccountHelper(context).clearCredentials();
if (context instanceof Activity) {
// Start onboarding.
Intent intent = new Intent(context, SetupActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
((Activity) context).finish();
}
}
return response;
}
});
httpClient.cache(getCache(context));
return httpClient.build();
}
Aggregations