package com.blockscore.net;

import com.blockscore.common.Constants;
import com.blockscore.exceptions.NoApiKeyFoundException;
import org.jetbrains.annotations.NotNull;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
import retrofit.converter.JacksonConverter;

import java.util.Base64;

/**
 * The Blockscore Java API client.
 * Created by tealocean on 9/29/14.
 */
public class BlockscoreApiClient {
    private static String sApiKey;

    /**
     * Initializes the API client with the API key. Must be done first or else all calls will fail.
     * @param apiKey API key to use.
     */
    public static void init(@NotNull final String apiKey) {
        sApiKey = apiKey + ":";
    }

    public BlockscoreApiClient() {
        RestAdapter.Builder restBuilder = new RestAdapter.Builder()
                .setClient(new OkClient())
                .setConverter(new JacksonConverter())
                .setEndpoint(Constants.BLOCKSCORE_DOMAIN);

        //Sets up the authentication headers and accept headers.
        restBuilder.setRequestInterceptor(new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                request.addHeader(Constants.AUTHORIZATION_HEADER, getEncodedAuthorization());
                request.addHeader(Constants.ACCEPT_HEADER, Constants.getAcceptHeaders());
            }
        });
    }

    /**
     * Encodes the API key for Basic authentication.
     * @return API key encoded with Base 64.
     */
    @NotNull
    private String getEncodedAuthorization() {
        if (sApiKey == null || sApiKey.isEmpty()) {
            throw new NoApiKeyFoundException();
        }
        return "Basic " + Base64.getEncoder().encodeToString(sApiKey.getBytes());
    }
}

@@ -119,16 +119,96 @@
 eption;%0A
+import com.blockscore.models.Person;%0Aimport com.blockscore.models.Verification;%0A
 import o
@@ -241,16 +241,42 @@
 otNull;%0A
+import retrofit.Callback;%0A
 import r
@@ -408,16 +408,38 @@
 nverter;
+%0Aimport rx.Observable;
 %0A%0Aimport
@@ -595,22 +595,149 @@
 tic 
-String sApiKey
+RestAdapter.LogLevel sLogLevel = RestAdapter.LogLevel.NONE;%0A    private static String sApiKey;%0A%0A    private BlockscoreRetrofitAPI restAdapter
 ;%0A%0A 
@@ -983,24 +983,295 @@
 %22:%22;%0A    %7D%0A%0A
+    /**%0A     * Turns on/off logging. Should be done after init(), but before API client usage.%0A     */%0A    public static void useVerboseLogs(final boolean useVerboseLogs) %7B%0A        if (useVerboseLogs) %7B%0A            sLogLevel = RestAdapter.LogLevel.FULL;%0A        %7D%0A    %7D%0A%0A
     public B
@@ -1942,16 +1942,1690 @@
      %7D);
+%0A%0A        restBuilder.setLogLevel(sLogLevel);%0A        restAdapter = restBuilder.build().create(BlockscoreRetrofitAPI.class);%0A    %7D%0A%0A    /**%0A     * Creates a new verification.%0A     * @see com.blockscore.net.BlockscoreRetrofitAPI#createVerification(com.blockscore.models.Person, retrofit.Callback)%0A     * @param person Person to verify.%0A     * @param callback Callback to use.%0A     */%0A    public void createVerification(@NotNull final Person person, @NotNull final Callback%3CVerification%3E callback) %7B%0A        restAdapter.createVerification(person, callback);%0A    %7D%0A%0A    /**%0A     * Creates a new verification.%0A     * @see com.blockscore.net.BlockscoreRetrofitAPI#createVerification(com.blockscore.models.Person)%0A     * @param person Person to verify.%0A     * @return Observable containing the verification results.%0A     */%0A    public Observable%3CVerification%3E createVerification(@NotNull final Person person) %7B%0A        return restAdapter.createVerification(person);%0A    %7D%0A%0A    /**%0A     * Pulls up a single verification.%0A     * @see com.blockscore.net.BlockscoreRetrofitAPI#getVerification(String, retrofit.Callback)%0A     * @param id ID of verification to verify.%0A     * @param callback Callback to use.%0A     */%0A    public void getVerification(@NotNull final String id, final Callback%3CVerification%3E callback) %7B%0A        restAdapter.getVerification(id, callback);%0A    %7D%0A%0A    /**%0A     * Pulls up a single verification.%0A     * @see com.blockscore.net.BlockscoreRetrofitAPI#getVerification(String)%0A     * @param id ID of verification to verify.%0A     */%0A    @NotNull%0A    Observable%3CVerification%3E getVerification(@NotNull final String id) %7B%0A        return restAdapter.getVerification(id);
 %0A    %7D%0A%0A

Create and get verifications