Skip to content

Commit

Permalink
Refactor SDKClientUtil for better readability, fix javadocs
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Feb 11, 2025
1 parent e7e2fcd commit 04e0ec3
Showing 1 changed file with 53 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public static BiConsumer<PutDataObjectResponse, Throwable> wrapPutCompletion(
Class<? extends Throwable>... exceptionTypesToUnwrap
) {
return (r, throwable) -> {
if (throwable == null) {
try {
IndexResponse indexResponse = r.parser() == null ? null : IndexResponse.fromXContent(r.parser());
listener.onResponse(indexResponse);
} catch (IOException e) {
handleParseFailure(listener, "put");
}
} else {
if (throwable != null) {
handleThrowable(listener, throwable, exceptionTypesToUnwrap);
return;
}
try {
IndexResponse indexResponse = r.parser() == null ? null : IndexResponse.fromXContent(r.parser());
listener.onResponse(indexResponse);
} catch (IOException e) {
handleParseFailure(listener, "put");
}
};
}
Expand All @@ -76,6 +76,7 @@ public static BiConsumer<PutDataObjectResponse, Throwable> wrapPutCompletion(
* Wraps the completion of a GET operation from the SdkClient into a format compatible with an ActionListener.
*
* @param listener The ActionListener that will receive the parsed GetResponse or any errors
* @param exceptionTypesToUnwrap optional list of exception types to unwrap. Defaults to {@link OpenSearchStatusException} and {@link CompletionException}.
* @return A BiConsumer that can be used directly with CompletionStage's whenComplete method
*/
@SafeVarargs
Expand All @@ -84,15 +85,15 @@ public static BiConsumer<GetDataObjectResponse, Throwable> wrapGetCompletion(
Class<? extends Throwable>... exceptionTypesToUnwrap
) {
return (r, throwable) -> {
if (throwable == null) {
try {
GetResponse getResponse = r.parser() == null ? null : GetResponse.fromXContent(r.parser());
listener.onResponse(getResponse);
} catch (IOException e) {
handleParseFailure(listener, "get");
}
} else {
if (throwable != null) {
handleThrowable(listener, throwable, exceptionTypesToUnwrap);
return;
}
try {
GetResponse getResponse = r.parser() == null ? null : GetResponse.fromXContent(r.parser());
listener.onResponse(getResponse);
} catch (IOException e) {
handleParseFailure(listener, "get");
}
};
}
Expand All @@ -101,6 +102,7 @@ public static BiConsumer<GetDataObjectResponse, Throwable> wrapGetCompletion(
* Wraps the completion of an UPDATE operation from the SdkClient into a format compatible with an ActionListener.
*
* @param listener The ActionListener that will receive the parsed UpdateResponse or any errors
* @param exceptionTypesToUnwrap optional list of exception types to unwrap. Defaults to {@link OpenSearchStatusException} and {@link CompletionException}.
* @return A BiConsumer that can be used directly with CompletionStage's whenComplete method
*/
@SafeVarargs
Expand All @@ -109,15 +111,15 @@ public static BiConsumer<UpdateDataObjectResponse, Throwable> wrapUpdateCompleti
Class<? extends Throwable>... exceptionTypesToUnwrap
) {
return (r, throwable) -> {
if (throwable == null) {
try {
UpdateResponse updateResponse = r.parser() == null ? null : UpdateResponse.fromXContent(r.parser());
listener.onResponse(updateResponse);
} catch (IOException e) {
handleParseFailure(listener, "update");
}
} else {
if (throwable != null) {
handleThrowable(listener, throwable, exceptionTypesToUnwrap);
return;
}
try {
UpdateResponse updateResponse = r.parser() == null ? null : UpdateResponse.fromXContent(r.parser());
listener.onResponse(updateResponse);
} catch (IOException e) {
handleParseFailure(listener, "update");
}
};
}
Expand All @@ -126,6 +128,7 @@ public static BiConsumer<UpdateDataObjectResponse, Throwable> wrapUpdateCompleti
* Wraps the completion of a DELETE operation from the SdkClient into a format compatible with an ActionListener.
*
* @param listener The ActionListener that will receive the parsed DeleteResponse or any errors
* @param exceptionTypesToUnwrap optional list of exception types to unwrap. Defaults to {@link OpenSearchStatusException} and {@link CompletionException}.
* @return A BiConsumer that can be used directly with CompletionStage's whenComplete method
*/
@SafeVarargs
Expand All @@ -134,15 +137,15 @@ public static BiConsumer<DeleteDataObjectResponse, Throwable> wrapDeleteCompleti
Class<? extends Throwable>... exceptionTypesToUnwrap
) {
return (r, throwable) -> {
if (throwable == null) {
try {
DeleteResponse deleteResponse = r.parser() == null ? null : DeleteResponse.fromXContent(r.parser());
listener.onResponse(deleteResponse);
} catch (IOException e) {
handleParseFailure(listener, "delete");
}
} else {
if (throwable != null) {
handleThrowable(listener, throwable, exceptionTypesToUnwrap);
return;
}
try {
DeleteResponse deleteResponse = r.parser() == null ? null : DeleteResponse.fromXContent(r.parser());
listener.onResponse(deleteResponse);
} catch (IOException e) {
handleParseFailure(listener, "delete");
}
};
}
Expand All @@ -151,6 +154,7 @@ public static BiConsumer<DeleteDataObjectResponse, Throwable> wrapDeleteCompleti
* Wraps the completion of a BULK operation from the SdkClient into a format compatible with an ActionListener.
*
* @param listener The ActionListener that will receive the parsed BulkResponse or any errors
* @param exceptionTypesToUnwrap optional list of exception types to unwrap. Defaults to {@link OpenSearchStatusException} and {@link CompletionException}.
* @return A BiConsumer that can be used directly with CompletionStage's whenComplete method
*/
@SafeVarargs
Expand All @@ -159,15 +163,15 @@ public static BiConsumer<BulkDataObjectResponse, Throwable> wrapBulkCompletion(
Class<? extends Throwable>... exceptionTypesToUnwrap
) {
return (r, throwable) -> {
if (throwable == null) {
try {
BulkResponse bulkResponse = r.parser() == null ? null : BulkResponse.fromXContent(r.parser());
listener.onResponse(bulkResponse);
} catch (IOException e) {
handleParseFailure(listener, "bulk");
}
} else {
if (throwable != null) {
handleThrowable(listener, throwable, exceptionTypesToUnwrap);
return;
}
try {
BulkResponse bulkResponse = r.parser() == null ? null : BulkResponse.fromXContent(r.parser());
listener.onResponse(bulkResponse);
} catch (IOException e) {
handleParseFailure(listener, "bulk");
}
};
}
Expand All @@ -176,6 +180,7 @@ public static BiConsumer<BulkDataObjectResponse, Throwable> wrapBulkCompletion(
* Wraps the completion of a SEARCH operation from the SdkClient into a format compatible with an ActionListener.
*
* @param listener The ActionListener that will receive the parsed SearchResponse or any errors
* @param exceptionTypesToUnwrap optional list of exception types to unwrap. Defaults to {@link OpenSearchStatusException} and {@link CompletionException}.
* @return A BiConsumer that can be used directly with CompletionStage's whenComplete method
*/
@SafeVarargs
Expand All @@ -184,15 +189,15 @@ public static BiConsumer<SearchDataObjectResponse, Throwable> wrapSearchCompleti
Class<? extends Throwable>... exceptionTypesToUnwrap
) {
return (r, throwable) -> {
if (throwable == null) {
try {
SearchResponse searchResponse = r.parser() == null ? null : SearchResponse.fromXContent(r.parser());
listener.onResponse(searchResponse);
} catch (IOException e) {
handleParseFailure(listener, "search");
}
} else {
if (throwable != null) {
handleThrowable(listener, throwable, exceptionTypesToUnwrap);
return;
}
try {
SearchResponse searchResponse = r.parser() == null ? null : SearchResponse.fromXContent(r.parser());
listener.onResponse(searchResponse);
} catch (IOException e) {
handleParseFailure(listener, "search");
}
};
}
Expand Down

0 comments on commit 04e0ec3

Please sign in to comment.