rafayali
7/29/2016 - 12:21 PM

HttpManager boilerplace class for managing http calls. This call is dependent on following libararies: apt 'com.bluelinelabs:logansquare-co

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-interceptor:3.4.1'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
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;
    }
}