Skip to content

Instantly share code, notes, and snippets.

@fmcarvalho
Created May 25, 2016 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmcarvalho/fcfe91effc495bae5fe1110b13b5c26d to your computer and use it in GitHub Desktop.
Save fmcarvalho/fcfe91effc495bae5fe1110b13b5c26d to your computer and use it in GitHub Desktop.
package util;
import org.asynchttpclient.AsyncCompletionHandler;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClient;
import org.asynchttpclient.Response;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
/**
*
* @author Miguel Gamboa at CCISEL
*/
public class HttpGetter implements AutoCloseable{
final AsyncHttpClient client;
public HttpGetter(AsyncHttpClient client) {
this.client = client;
}
public HttpGetter() {
this(new DefaultAsyncHttpClient());
}
public CompletableFuture<Response> getDataAsync(String path){
CompletableFuture<Response> promise = new CompletableFuture<>();
client
.prepareGet(path)
.execute(new AsyncCompletionHandler<Object>() {
@Override
public Object onCompleted(Response response) throws Exception {
promise.complete(response);
return response;
}
@Override
public void onThrowable(Throwable t) {
promise.completeExceptionally(t);
}
});
return promise;
}
@Override
public void close() throws IOException {
if(!client.isClosed())
client.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment