package com.radiostream.networking;

import android.util.Base64;

import java.io.IOException;

import hugo.weaving.DebugLog;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by vitaly on 10/11/2016.
 */

@DebugLog
public class HttpServiceFactory {
    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create());

    public static <S> S createService(Class<S> serviceClass, String baseUrl, String username, String password) {
        if (username != null && password != null) {
            String credentials = username + ":" + password;
            final String basic =
                    "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                    Request original = chain.request();

                    Request.Builder requestBuilder = original.newBuilder()
                            .header("Authorization", basic)
                            .header("Accept", "application/json")
                            .method(original.method(), original.body());

                    Request request = requestBuilder.build();
                    return chain.proceed(request);
                }
            });
        }

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        httpClient.addInterceptor(logging);

        OkHttpClient client = httpClient.build();
        Retrofit retrofit = builder.baseUrl(baseUrl).client(client).build();
        return retrofit.create(serviceClass);
    }

}

@@ -675,24 +675,223 @@
 create());%0A%0A
+    static %7B%0A        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();%0A        logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);%0A        httpClient.addInterceptor(logging);%0A    %7D%0A%0A
     public s
@@ -1897,187 +1897,8 @@
  %7D%0A%0A
-        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();%0A        logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);%0A        httpClient.addInterceptor(logging);%0A
 %0A   

fix - okHttp logs stuff multiple times