use of org.apache.pulsar.client.admin.Sink in project pulsar by apache.
the class SinksImpl method createSinkAsync.
@Override
public CompletableFuture<Void> createSinkAsync(SinkConfig sinkConfig, String fileName) {
final CompletableFuture<Void> future = new CompletableFuture<>();
if (!validateSinkName(sinkConfig.getTenant(), sinkConfig.getNamespace(), sinkConfig.getName(), future)) {
return future;
}
try {
RequestBuilder builder = post(sink.path(sinkConfig.getTenant()).path(sinkConfig.getNamespace()).path(sinkConfig.getName()).getUri().toASCIIString()).addBodyPart(new StringPart("sinkConfig", ObjectMapperFactory.getThreadLocal().writeValueAsString(sinkConfig), MediaType.APPLICATION_JSON));
if (fileName != null && !fileName.startsWith("builtin://")) {
// If the function code is built in, we don't need to submit here
builder.addBodyPart(new FilePart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM));
}
asyncHttpClient.executeRequest(addAuthHeaders(sink, builder).build()).toCompletableFuture().thenAccept(response -> {
if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
future.completeExceptionally(getApiException(Response.status(response.getStatusCode()).entity(response.getResponseBody()).build()));
} else {
future.complete(null);
}
}).exceptionally(throwable -> {
future.completeExceptionally(getApiException(throwable));
return null;
});
} catch (Exception e) {
future.completeExceptionally(getApiException(e));
}
return future;
}
Aggregations