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 synchronous support to the DeliveryClient #109

Open
Jmathieu3289 opened this issue Sep 18, 2020 · 6 comments
Open

Add synchronous support to the DeliveryClient #109

Jmathieu3289 opened this issue Sep 18, 2020 · 6 comments
Labels

Comments

@Jmathieu3289
Copy link

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
Copy link
Contributor

@Jmathieu3289 can you assign to me?

@Simply007
Copy link
Contributor

Simply007 commented Oct 2, 2020

@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
Copy link
Contributor

@Simply007 here is the PR: #112

Do you know why this repo is invalid for hacktoberfest?

@Simply007
Copy link
Contributor

Simply007 commented Oct 6, 2020

@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
Copy link
Contributor

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

Thanks for the review, I'll check it out

@Simply007
Copy link
Contributor

Added "hacktoberfest" topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants