kontent-ai / java-packages

Delivery Java SDK for Kontent.ai including examples for Spring, Gradle, Kotlin and Java Android app
https://kontent.ai/learn/tutorials/develop-apps/overview/?tech=java
MIT License
14 stars 28 forks source link

Add synchronous support to the DeliveryClient #109

Open Jmathieu3289 opened 4 years ago

Jmathieu3289 commented 4 years ago

Motivation

After updating a large application to the latest version of the Java SDK, all calls to the DeliveryClient had to wrapped in a synchronous wrapper. It would be helpful if there were a built-in way to call the DeliveryClient synchronously rather than introducing additional code to the project.

Proposed solution

Add alternate methods for working with the DeliveryClient synchronously, for example:

deliveryClient.getItems().toSync() or deliveryClient.getItemsSynchronously()

Additional context

Here is the wrapper class that I had to write:

public class DeliveryClientHelper {
    public static <T> T toSync(CompletionStage<T> completionStage) {
        try {
            return completionStage.toCompletableFuture().get();
        } catch (InterruptedException e) {
            log.error("Error converting completion stage: {}", e.getMessage());
        } catch (ExecutionException e) {
            log.error("Error converting completion stage: {}", e.getMessage());
        }
        return null;
    }
}

An example call using the wrapper:

List<AgencySiteBlogLanding> blogHomepages = DeliveryClientHelper.toSync(deliveryClient.getItems(AgencySiteBlogLanding.class));

An ideal way to use the DeliveryClient

List<AgencySiteBlogLanding> blogHomepages = deliveryClient.getItems(AgencySiteBlogLanding.class).toSync();
cunhazera commented 4 years ago

@Jmathieu3289 can you assign to me?

Simply007 commented 4 years ago

@cunhazera - it is yours.

Please keep in mind to update the documentation and ideally add a test for the functionality.

I would love to see this approach implemented:

List<AgencySiteBlogLanding> blogHomepages = deliveryClient.getItems(AgencySiteBlogLanding.class).toSync();

I am still thinking, what is the best approach for the SDK to allow custom logging logic in case of failure so that this toSync method does not have the logging hardcoded via system error output.

what do you think @Jmathieu3289?

cunhazera commented 4 years ago

@Simply007 here is the PR: https://github.com/Kentico/kontent-java-packages/pull/112

Do you know why this repo is invalid for hacktoberfest?

Simply007 commented 4 years ago

@cunhazera,

you could check the reason on your profile on https://hacktoberfest.digitalocean.com/profile.

There have been a bunch of anti-spam initiatives this year. If there is anything I can help you to covert the pull request to a valid one, I am happy to do so. This is a normal and valid contribution if everything does well.

I will comment on implementation in the pull request.

cunhazera commented 4 years ago

@Simply007 I think you need to add the "hacktoberfest" topic. image

Thanks for the review, I'll check it out

Simply007 commented 4 years ago

Added "hacktoberfest" topic.