package pro.beam.api;

import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.common.util.concurrent.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import pro.beam.api.http.RequestType;

import java.io.IOException;
import java.net.URI;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

public class BeamAPI {
    public static final URI BASE_PATH = URI.create("https://beam.pro/api/v1/");

    protected final ListeningExecutorService executor;
    protected final HttpRequestFactory requestFactory;
    protected final Gson gson = new GsonBuilder().create();

    public BeamAPI() {
        this.executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
        this.requestFactory = new NetHttpTransport().createRequestFactory();
    }

    public <T> ListenableFuture<T> get(String path, Class<T> type) throws IOException {
        return this.executor.submit(this.makeCallable(this.makeRequest(RequestType.GET, path), type));
    }

    private HttpRequest makeRequest(RequestType requestType, String path) throws IOException {
        return this.makeRequest(requestType, path, null);
    }

    private HttpRequest makeRequest(RequestType requestType, String path, HttpContent content) throws IOException {
        return this.requestFactory.buildRequest(requestType.name(),
                                                this.buildFromRelativePath(path),
                                                content);
    }

    private <T> Callable<T> makeCallable(final HttpRequest request, final Class<T> type) {
        return new Callable<T>() {
            @Override public T call() throws Exception {
                HttpResponse response = request.execute();
                return BeamAPI.this.gson.fromJson(response.parseAsString(), type);
            }
        };
    }

    private GenericUrl buildFromRelativePath(String path) {
        return new GenericUrl(BASE_PATH.resolve(path));
    }
}

@@ -1052,32 +1052,117 @@
  type));%0A    %7D%0A%0A
+    /**%0A     * Forwards the method call to #makeRequest without any content.%0A     */%0A
     private Http
@@ -1313,334 +1313,1620 @@
     
-private HttpRequest makeRequest(RequestType requestType, String path, HttpContent content) throws IOException %7B%0A        return this.requestFactory.buildRequest(requestType.name(),%0A                                                this.buildFromRelativePath(path),%0A                                                content);%0A    %7D%0A
+/**%0A     * Creates a %7B@link com.google.api.client.http.HttpRequest%7D given a type, path, and optional content.%0A     *%0A     * @param requestType The type of HTTP/1.1 request to make (see %7B@link pro.beam.api.http.RequestType%7D%0A     *                    for more details.%0A     * @param path The absolute path to direct the request at (see #buildFromRelativePath for more details)%0A     * @param content The content of the request.  This parameter is optional and considered to be @Nullable.%0A     *%0A     * @return A request built from the specification above.%0A     */%0A    private HttpRequest makeRequest(RequestType requestType, String path, HttpContent content) throws IOException %7B%0A        return this.requestFactory.buildRequest(requestType.name(),%0A                                                this.buildFromRelativePath(path),%0A                                                content);%0A    %7D%0A%0A    /**%0A     * This public-facing method constructs a callable which has the responsibility of executing the request%0A     * given, and parsing it using an instance of a GSON parser into the desired class-type,  Typically this method%0A     * is used to submit a %7B@link java.util.concurrent.Callable%7D to the%0A     * %7B@link com.google.common.util.concurrent.ListeningExecutorService%7D such that a callback can be fired when the%0A     * request and parsing is complete.%0A     *%0A     * @param request The %7B@link com.google.api.client.http.HttpRequest%7D to be executed.%0A     * @param type The class-type of the response body.  (See the %22response%22 package for examples.)%0A     *%0A     * @return The callable as described above.%0A     */
 %0A   
@@ -3275,24 +3275,257 @@
   %7D;%0A    %7D%0A%0A
+    /**%0A     * Takes a relative path and makes it absolute with respect to the root of the API.%0A     *%0A     * @param path The relative path (ex., %22channels/%22)%0A     *%0A     * @return The absolute resolved path as a GenericUrl.%0A     */%0A
     private 

Add some documentation