Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to KiwiResponses that accept Response suppliers #1141

Closed
sleberknight opened this issue Jun 2, 2024 · 0 comments · Fixed by #1142
Closed

Add methods to KiwiResponses that accept Response suppliers #1141

sleberknight opened this issue Jun 2, 2024 · 0 comments · Fixed by #1142
Assignees
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Milestone

Comments

@sleberknight
Copy link
Member

sleberknight commented Jun 2, 2024

Motivation

KiwiResponses has methods to perform various actions on a Response, which means code calling these methods must have already obtained the Response using a Client, and should have handled any (runtime) exceptions that can be thrown, e.g., ProcessingException and subclasses.

Once the Response has been obtained, then the code can call one of the KiwiResponses methods. This splits the code making request from the code handling the response. Sometimes this might be acceptable and/or desirable but usually these activities happen in one place at the same time.

Example

Many times it would be nicer to make the request and process the response in one location. For example, currently you might do something like:

try {
    var response = client.target("/tasks/unresolved").request.get();
} catch (ProcessingException e) {
    return newEmptyTaskListAndNotifyError(null, e);
}

return KiwiResponses.onSuccessOrFailureWithResult(
    response,
    successResponse -> successResponse.readEntity(new GenericType<List<Task>>() {}),
    failedResponse -> newEmptyTaskListAndNotifyError(failedResponse, null)
);

But by changing the first argument to a Supplier<Response> and adding a 4th argument which accepts an exception and returns an object, the above code can be changed to:

return KiwiResponses.onSuccessOrFailureWithResult(
    () -> client.target("/tasks/unresolved").request.get(),
    successResponse -> successResponse.readEntity(new GenericType<List<Task>>() {}),
    failedResponse -> newEmptyTaskListAndNotifyError(failedResponse, null),
    supplierException -> newEmptyTaskListAndNotifyError(null, supplierException)
);

In this example, if the request succeeds, then the response entity is resolved to a List<Task and returned.

If the request fails (has a status that is not 200 in this case) or if the Supplier threw an exception, then an empty list is returned and the code (presumably) performs some kind of notification about the error.

And the code ensures that any Response (successful or not) is closed. If the Supplier threw an exception, there is no response to close.

New Feature Description

This task adds overloaded onXxx, accept, and apply methods which accept a Supplier<Response as the first argument instead of a Response. This will allow request/response processing code to be located in one location and not have try/catch blocks followed immediately by a KiwiResponses.onXxx method.

Some of the overloads will add additional parameters, like the above example adds a 4th argument, Function<RuntimeException, T> exceptionFun, which accepts a RuntimeException and must return a result of type T.

This will increase the surface area of KiwiResponses by adding 11 new methods: 9 onXxx methods, accept, and apply.

@sleberknight sleberknight added the new feature A new feature such as a new class, method, package, group of classes, etc. label Jun 2, 2024
@sleberknight sleberknight added this to the 4.0.0 milestone Jun 2, 2024
@sleberknight sleberknight self-assigned this Jun 2, 2024
@sleberknight sleberknight changed the title Add methods to KiwiResponses that accept Supplier of Response Add methods to KiwiResponses that accept Response suppliers Jun 2, 2024
sleberknight added a commit that referenced this issue Jun 2, 2024
* Add overloaded onXxx, accept, and apply methods which accept a
Supplier<Response>
* Where appropriate, some of the new methods add an additional parameter
to consume or
  return a value for exceptions thrown by the Supplier

Closes #1141
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant