All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gihub.gkutiel.Fbi Maven / Gradle / Ivy

There is a newer version: 6-RELEASE
Show newest version
package com.gihub.gkutiel;

import java.io.IOException;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.ListenableFuture;
import com.ning.http.client.Response;

public class Fbi {
	public interface Future {
		T get();
	}
	private static final Gson gson = new Gson();

	public static Fbi request(final GraphApi graphApiUrl) {
		try {
			return new Fbi(new AsyncHttpClient().prepareGet(graphApiUrl.asString()).execute());
		} catch (final IOException e) {
			throw new RuntimeException();
		}
	}

	private final ListenableFuture futureResponse;

	private Fbi(final ListenableFuture futureResponse) {
		this.futureResponse = futureResponse;
	}

	public  Future as(final Class type) {
		return new Future() {
			@Override
			public T get() {
				final String json = asString().get();
				try {
					return gson.fromJson(json, type);
				} catch (final JsonSyntaxException e) {
					throw new RuntimeException("failed to parse:\n" + json, e);
				}
			}
		};
	}

	public Future asString() {
		return new Future() {

			@Override
			public String get() {
				try {
					return futureResponse.get().getResponseBody();
				} catch (final Exception e) {
					throw new RuntimeException(e);
				}
			}
		};
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy