package com.librato.metrics;

import com.ning.http.client.AsyncHttpClient;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * An HTTPPoster which uses ning's HTTP client
 */
@SuppressWarnings("unused")
public class NingHttpPoster implements HttpPoster {
    private final AsyncHttpClient httpClient = new AsyncHttpClient();
    private final String authHeader;
    private final String apiUrl;

    public NingHttpPoster(String authHeader, String apiUrl) {
        this.authHeader = authHeader;
        this.apiUrl = apiUrl;
    }

    /**
     * Return a new poster with the correct authentication header.
     *
     * @param username the librato username
     * @param token    the librato token
     * @param apiUrl   the URL to post to
     * @return a new NingHttpPoster
     */
    public static NingHttpPoster newPoster(String username, String token, String apiUrl) {
        return new NingHttpPoster(Authorization.buildAuthHeader(username, token), apiUrl);
    }

    /**
     * Adapts a ning {@link com.ning.http.client.Response} future to a @{link Response} future
     */
    static class FutureAdapter implements Future<Response> {
        private final Future<com.ning.http.client.Response> delegate;

        FutureAdapter(Future<com.ning.http.client.Response> delegate) {
            this.delegate = delegate;
        }

        public boolean cancel(boolean mayInterruptIfRunning) {
            return delegate.cancel(mayInterruptIfRunning);
        }

        public boolean isCancelled() {
            return delegate.isCancelled();
        }

        public boolean isDone() {
            return delegate.isDone();
        }

        public Response get() throws InterruptedException, ExecutionException {
            return adapt(delegate.get());
        }


        public Response get(long timeout, @SuppressWarnings("NullableProblems") TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
            return adapt(delegate.get(timeout, unit));
        }

        private Response adapt(final com.ning.http.client.Response response) {
            return new Response() {
                public int getStatusCode() {
                    return response.getStatusCode();
                }

                public String getBody() throws IOException {
                    return response.getResponseBody();
                }
            };
        }
    }

    public Future<Response> post(String userAgent, String payload) throws IOException {
        final AsyncHttpClient.BoundRequestBuilder builder = httpClient.preparePost(apiUrl);
        builder.addHeader("Content-Type", "application/json");
        builder.addHeader("Authorization", authHeader);
        builder.addHeader("User-Agent", userAgent);
        builder.setBody(payload);
        return new FutureAdapter(builder.execute());
    }
}

@@ -67,16 +67,67 @@
 pClient;
+%0Aimport com.ning.http.client.AsyncHttpClientConfig;
 %0A%0Aimport
@@ -500,32 +500,8 @@
 ient
- = new AsyncHttpClient()
 ;%0A  
@@ -699,16 +699,307 @@
 apiUrl;%0A
+        this.httpClient = new AsyncHttpClient();%0A    %7D%0A%0A    public NingHttpPoster(String authHeader, String apiUrl, AsyncHttpClientConfig httpClientConfig) %7B%0A        this.authHeader = authHeader;%0A        this.apiUrl = apiUrl;%0A        this.httpClient = new AsyncHttpClient(httpClientConfig);%0A
     %7D%0A%0A 
@@ -1333,32 +1333,32 @@
 tring apiUrl) %7B%0A
-
         return n
@@ -1431,32 +1431,657 @@
 apiUrl);%0A    %7D%0A%0A
+    /**%0A     * Return a new poster with the correct authentication header.%0A     *%0A     * @param username         the librato username%0A     * @param token            the librato token%0A     * @param apiUrl           the URL to post to%0A     * @param httpClientConfig configuration of http client, this can be use to pass a proxy server.%0A     * @return a new NingHttpPoster%0A     */%0A    public static NingHttpPoster newPoster(String username, String token, String apiUrl, AsyncHttpClientConfig httpClientConfig) %7B%0A        return new NingHttpPoster(Authorization.buildAuthHeader(username, token), apiUrl, httpClientConfig);%0A    %7D%0A%0A
     /**%0A     * A

Add possibility to use a proxy server