package pro.beam.api;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
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 java.io.IOException;
import java.net.URI;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

public class BeamAPI {
    public static final ListeningExecutorService EXECUTOR_SERVICE = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));

    public static final URI BASE_PATH = URI.create("https://beam.pro/api/v1/");
    protected final HttpRequestFactory requestFactory;
    protected final Gson gson = new GsonBuilder().create();

    public BeamAPI() {
        this.requestFactory = new NetHttpTransport().createRequestFactory();
    }

    public <T> ListenableFuture<T> get(String path, final Class<T> type) throws IOException {
        final HttpRequest request = this.requestFactory.buildGetRequest(this.buildFromRelativePath(path));

        return EXECUTOR_SERVICE.submit(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));
    }
}

@@ -559,199 +559,118 @@
 nal 
-ListeningExecutorService EXECUTOR_SERVICE = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));%0A%0A    public static final URI BASE_PATH = URI.create(%22https://beam.pro/api/v1/%22)
+URI BASE_PATH = URI.create(%22https://beam.pro/api/v1/%22);%0A%0A    protected final ListeningExecutorService executor
 ;%0A  
@@ -802,24 +802,116 @@
 BeamAPI() %7B%0A
+        this.executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));%0A
         this
@@ -1033,22 +1033,16 @@
 ng path,
- final
  Class%3CT
@@ -1082,35 +1082,69 @@
     
-final HttpRequest request =
+return this.executor.submit(this.callRequest(%0A               
  thi
@@ -1176,16 +1176,37 @@
 Request(
+%0A                    
 this.bui
@@ -1234,50 +1234,130 @@
 th))
+, type))
 ;%0A
-%0A
     
+%7D%0A%0A
     
-return EXECUTOR_SERVICE.submit(
+private %3CT%3E Callable%3CT%3E callRequest(final HttpRequest request, final Class%3CT%3E type) %7B%0A        return 
 new 
@@ -1594,17 +1594,16 @@
        %7D
-)
 ;%0A    %7D%0A

Refactor BeamAPI client to support more request types