Skip to content

Instantly share code, notes, and snippets.

@rafayali
Last active September 17, 2016 05:54
Show Gist options
  • Save rafayali/13e1255a837c5bee58a14c31a9143af8 to your computer and use it in GitHub Desktop.
Save rafayali/13e1255a837c5bee58a14c31a9143af8 to your computer and use it in GitHub Desktop.
HttpManager boilerplace class for managing http calls. This call is dependent on following libararies: apt 'com.bluelinelabs:logansquare-compiler:1.3.6' compile 'com.bluelinelabs:logansquare:1.3.6' compile "com.github.aurae.retrofit2:converter-logansquare:1.4.1" compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.squareup.okhttp3:logging-in…
package com.android.popularmovies.http;
import android.util.Log;
import com.franmontiel.persistentcookiejar.ClearableCookieJar;
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.github.aurae.retrofit2.LoganSquareConverterFactory;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
/**
* Contains all network call builders for server endpoints.
*
* Created by Rafay Ali on 4/8/2016.
*/
public class HttpManager {
public static final String TAG = HttpManager.class.getSimpleName();
private static HttpManager instance;
private OkHttpClient okHttpClient;
private Retrofit retrofit;
private HttpManager(){
ClearableCookieJar cookieJar =
new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(BaseApplication.getContext()));
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
Log.v(TAG, "Authenticating for response: " + response);
Log.v(TAG, "Challenges: " + response.challenges());
if (ApplicationProperties.USE_AUTHENTICATION){
String credentials = Credentials.basic(ApplicationProperties.AUTHENTICATION_USERNAME, ApplicationProperties.AUTHENTICATION_PASSWORD);
return response.request().newBuilder().header("Authorization", credentials).build();
}
Log.w(TAG, "No Authentication is being used");
return null;
}
})
.cookieJar(cookieJar)
.build();
retrofit = new Retrofit.Builder()
.baseUrl(ApplicationProperties.SERVER_ADDRESS + ApplicationProperties.NAMESPACE)
.addConverterFactory(LoganSquareConverterFactory.create())
.client(okHttpClient)
.build();
}
public static HttpManager getInstance(){
if (instance == null)
instance = new HttpManager();
return instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment