package co.paystack.android.api;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import co.paystack.android.api.service.ApiService;
import co.paystack.android.api.utils.TLSSocketFactory;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//import retrofit2.RestAdapter;
//import retrofit2.converter.GsonConverter;

/**
 * API Client Class
 *
 * Provides a service by which we can make API calls
 */
public class ApiClient {

  private static final String BASE_URL = "https://standard.paystack.co/";
  public static String API_URL = BASE_URL;

  private ApiService apiService;

  public ApiClient() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
    Gson gson = new GsonBuilder()
        .setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
        .create();

    TLSSocketFactory tlsV1point2factory = new TLSSocketFactory();
    OkHttpClient okHttpClient = new OkHttpClient
        .Builder()
        .sslSocketFactory(tlsV1point2factory, tlsV1point2factory.getX509TrustManager())
        .build();

    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(API_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

    apiService = retrofit.create(ApiService.class);
  }

  public ApiService getApiService() {
    return apiService;
  }
}

@@ -93,16 +93,44 @@
 ilder;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -158,32 +158,32 @@
 ementException;%0A
-
 import java.secu
@@ -254,16 +254,91 @@
 ption;%0A%0A
+import co.paystack.android.BuildConfig;%0Aimport co.paystack.android.Config;%0A
 import c
@@ -435,16 +435,44 @@
 actory;%0A
+import okhttp3.Interceptor;%0A
 import o
@@ -492,16 +492,65 @@
 Client;%0A
+import okhttp3.Request;%0Aimport okhttp3.Response;%0A
 import r
@@ -1294,16 +1294,16 @@
 pClient%0A
-
         
@@ -1305,32 +1305,813 @@
      .Builder()%0A
+            .addInterceptor(new Interceptor() %7B%0A                @Override%0A                public Response intercept(Interceptor.Chain chain) throws IOException %7B%0A                    Request original = chain.request();%0A                    // Add headers so we get Android version and Paystack Library version%0A                    Request.Builder builder = original.newBuilder()%0A                            .header(%22User-Agent%22, %22Android_%22 + Config.VERSION_CODE + %22_Paystack_%22 + BuildConfig.VERSION_NAME)%0A                            .header(%22Accept%22, %22application/json%22)%0A                            .method(original.method(), original.body());%0A                    Request request = builder.build();%0A%0A                    return chain.proceed(request);%0A                %7D%0A            %7D)%0A
         .sslSock

Add UserAgent header