package com.boundary.metrics.vmware.client.client.subaccount;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.filter.ClientFilter;
import com.sun.jersey.core.util.Base64;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedMap;
import java.net.URI;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

public class SubaccountClient {

    private final WebResource baseResource;
    private static final Joiner PATH_JOINER = Joiner.on('/');

    public SubaccountClient(Client client, URI baseUrl, String apiKey) {
        checkNotNull(client);
        checkNotNull(baseUrl);
        checkArgument(!Strings.isNullOrEmpty(apiKey));
        final String authorization = "Basic " + new String(Base64.encode(apiKey + ":"), Charsets.US_ASCII);
        client.addFilter(new ClientFilter() {
            @Override
            public ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
                final MultivaluedMap<String, Object> headers = cr.getHeaders();
                if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) {
                    headers.add(HttpHeaders.AUTHORIZATION, authorization);
                }
                return getNext().handle(cr);
            }
        });
        this.baseResource = client.resource(baseUrl);
    }

    public String getMetricCredentials(String orgId) {
        SubaccountInfo subaccountInfo = baseResource
                .path(PATH_JOINER.join(orgId, "subaccount"))
                .get(SubaccountInfo.class);
        return subaccountInfo.getCredentials();
        // TODO error handling
    }
}

@@ -534,16 +534,100 @@
 otNull;%0A
+/**%0A * %0A * Handles calling of the SubAccount API to get authentication tokens%0A *%0A */
 %0Apublic 
@@ -759,16 +759,177 @@
 ('/');%0A%0A
+    /**%0A     * Constructor%0A     * %0A     * @param client Jersey Client%0A     * @param baseUrl Url to make REST call%0A     * @param apiKey Authorization key%0A     */%0A
     publ
@@ -1752,16 +1752,190 @@
 %0A    %7D%0A%0A
+    /**%0A     * Returns the authorization credentials to work with %0A     * HLM (Host Level Metrics)%0A     * %0A     * @param orgId%0A     * @return String with credentials%0A     */%0A
     publ
@@ -1988,25 +1988,25 @@
 %0A        Sub
-a
+A
 ccountInfo s
@@ -2119,17 +2119,17 @@
 .get(Sub
-a
+A
 ccountIn

Add comments and Refactor