-
-
Save fmcarvalho/fcfe91effc495bae5fe1110b13b5c26d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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