use of com.yahoo.pulsar.client.api.AuthenticationDataProvider in project pulsar by yahoo.
the class HttpClient method get.
public <T> CompletableFuture<T> get(String path, Class<T> clazz) {
final CompletableFuture<T> future = new CompletableFuture<>();
try {
String requestUrl = new URL(url, path).toString();
AuthenticationDataProvider authData = authentication.getAuthData();
BoundRequestBuilder builder = httpClient.prepareGet(requestUrl);
// Add headers for authentication if any
if (authData.hasDataForHttp()) {
for (Map.Entry<String, String> header : authData.getHttpHeaders()) {
builder.setHeader(header.getKey(), header.getValue());
}
}
final ListenableFuture<Response> responseFuture = builder.setHeader("Accept", "application/json").execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response) throws Exception {
return response;
}
@Override
public void onThrowable(Throwable t) {
log.warn("[{}] Failed to perform http request: {}", requestUrl, t.getMessage());
future.completeExceptionally(new PulsarClientException(t));
}
});
responseFuture.addListener(() -> {
try {
Response response = responseFuture.get();
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
log.warn("[{}] HTTP get request failed: {}", requestUrl, response.getStatusText());
future.completeExceptionally(new PulsarClientException("HTTP get request failed: " + response.getStatusText()));
return;
}
T data = ObjectMapperFactory.getThreadLocal().readValue(response.getResponseBodyAsBytes(), clazz);
future.complete(data);
} catch (Exception e) {
log.warn("[{}] Error during HTTP get request: {}", requestUrl, e.getMessage());
future.completeExceptionally(new PulsarClientException(e));
}
}, MoreExecutors.sameThreadExecutor());
} catch (Exception e) {
log.warn("[{}] Failed to get authentication data for lookup: {}", path, e.getMessage());
if (e instanceof PulsarClientException) {
future.completeExceptionally(e);
} else {
future.completeExceptionally(new PulsarClientException(e));
}
}
return future;
}
Aggregations