Skip to content

Add synchronous support to the DeliveryClient #109

@Jmathieu3289

Description

@Jmathieu3289

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();

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions