package com.yammer.metrics.reporting;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class HttpTransport implements Transport {
    private final String seriesUrl;

    public HttpTransport(String apiKey) {
        this.seriesUrl = String.format("https://app.datadoghq.com/api/v1/series?api_key=%s", apiKey);
    }

    public Request prepare() throws IOException {
        return new HttpRequest(this);
    }

    public static class HttpRequest implements Transport.Request {
        private final HttpTransport transport;
        private final HttpPost request;
        private final ByteArrayOutputStream out;

        public HttpRequest(HttpTransport transport) throws IOException {
            this.transport = transport;
            this.request = new HttpPost(this.transport.seriesUrl);
            this.out = new ByteArrayOutputStream();
        }

        public OutputStream getBodyWriter() {
            return out;
        }

        public void send() throws Exception {
            this.out.flush();
            this.out.close();
            this.request.setEntity(new ByteArrayEntity(out.toByteArray(), ContentType.APPLICATION_JSON));

            org.apache.http.client.fluent.Request.Post(this.transport.seriesUrl)
                    .addHeader("Content-Type", "application/json")
                    .bodyByteArray(this.out.toByteArray())
                    .execute();
        }
    }
}

@@ -36,103 +36,8 @@
 g;%0A%0A
-import org.apache.http.client.methods.HttpPost;%0Aimport org.apache.http.entity.ByteArrayEntity;%0A
 impo
@@ -75,16 +75,16 @@
 ntType;%0A
+
 %0Aimport 
@@ -172,16 +172,72 @@
 tream;%0A%0A
+import static org.apache.http.client.fluent.Request.*;%0A%0A
 public c
@@ -679,48 +679,8 @@
 rt;%0A
-        private final HttpPost request;%0A
     
@@ -842,75 +842,8 @@
 rt;%0A
-            this.request = new HttpPost(this.transport.seriesUrl);%0A
     
@@ -1104,308 +1104,142 @@
     
-this.request.setEntity(new ByteArrayEntity(out.toByteArray(), ContentType.APPLICATION_JSON));%0A%0A            org.apache.http.client.fluent.Request.Post(this.transport.seriesUrl)%0A                    .addHeader(%22Content-Type%22, %22application/json%22)%0A                    .bodyByteArray(this.out.toByteArray()
+Post(this.transport.seriesUrl).useExpectContinue()%0A                    .bodyString(out.toString(%22UTF-8%22), ContentType.APPLICATION_JSON
 )%0A  
@@ -1266,16 +1266,33 @@
 xecute()
+.discardContent()
 ;%0A      

Clean up HttpTransport to follow conventions in the fluent client docs