Class Configuration

java.lang.Object
com.commercetools.docs.meta.Configuration

public class Configuration extends Object
Table of content

Configuration

ApiRoot and ProjectApiRoot

The SDK modules provide an ApiRootBuilder to create an ApiHttpClient. To ease the development the builder can also create a module specific ApiRoot or ProjectApiRoot. E.g.: ApiRoot and ProjectApiRoot

Creating http requests starts from the ApiRoot which holds information specific to the project. The following example shows how to configure an API root and a project scoped root:

// ApiRoot config for Europe projects
ApiRoot apiRoot = ApiRootBuilder.of()
        .defaultClient(ClientCredentials.of()
                .withClientId("your-client-id")
                .withClientSecret("your-client-secret")
                .build(),
            ServiceRegion.GCP_EUROPE_WEST1)
        .build();

// Project scoped ApiRoot config for Europe projects
ProjectApiRoot projectApiRoot = ApiRootBuilder.of()
        .defaultClient(ClientCredentials.of()
                .withClientId("your-client-id")
                .withClientSecret("your-client-secret")
                .build(),
            ServiceRegion.GCP_EUROPE_WEST1)
        .build("my-project");

// Project scoped ApiRoot config for Europe projects
ProjectApiRoot projectApiRootGcpEu = ApiRootBuilder.of()
        .defaultClient(ClientCredentials.of()
                .withClientId("your-client-id")
                .withClientSecret("your-client-secret")
                .build(),
            ServiceRegion.valueOf("GCP_EUROPE_WEST1"))
        .build("my-project");

See the test code.

Similar configuration to create the root instances for the Import API

ApiRoot apiRoot = ImportApiRootBuilder.of()
        .defaultClient(ClientCredentials.of()
                .withClientId("your-client-id")
                .withClientSecret("your-client-secret")
                .build(),
            ServiceRegion.GCP_EUROPE_WEST1)
        .build();

ProjectApiRoot projectApiRoot = ImportApiRootBuilder.of()
        .defaultClient(ClientCredentials.of()
                .withClientId("your-client-id")
                .withClientSecret("your-client-secret")
                .build(),
            ServiceRegion.GCP_EUROPE_WEST1)
        .build("my-projectkey");

See the test code.

Custom URLs

To use custom URLs for API endpoints and authentication you have to provide the base URIs to the defaultClient method
// Project scoped ApiRoot config using ServiceRegion class
ProjectApiRoot projectApiRoot = ApiRootBuilder.of()
        .defaultClient(
            ClientCredentials.of()
                    .withClientId("your-client-id")
                    .withClientSecret("your-client-secret")
                    .build(),
            ServiceRegion.GCP_EUROPE_WEST1.getOAuthTokenUrl(), ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
        .build("my-project");

// Project scoped ApiRoot config using URI strings
ProjectApiRoot projectApiRoot2 = ApiRootBuilder.of()
        .defaultClient(
            ClientCredentials.of()
                    .withClientId("your-client-id")
                    .withClientSecret("your-client-secret")
                    .build(),
            "https://auth.europe-west1.gcp.commercetools.com/oauth/token",
            "https://api.europe-west1.gcp.commercetools.com/")
        .build("my-project");

See the test code.

Custom HTTP client

The builder can be instantiated with a custom VrapHttpClient instance. For example a specific instance of a client

ApiRootBuilder builder = ApiRootBuilder.of(new CtOkHttp4Client());

See the test code.

This can also be useful to wrap preconfigured clients e.g. with additional middlewares

ApiHttpClient client = ApiRootBuilder.of()
        .defaultClient(ClientCredentials.of()
                .withClientId("your-client-id")
                .withClientSecret("your-client-secret")
                .build(),
            ServiceRegion.GCP_EUROPE_WEST1)
        .buildClient();

ProjectApiRoot projectApiRoot = ApiRootBuilder.of(client)
        .withApiBaseUrl(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
        .withMiddleware((request, next) -> {
            return next.apply(request.addHeader("x-custom-header", "custom-header-value"));
        })
        .build("my-project-key");

See the test code.

Error handling

The client builder adds methods to allow the customization of the error handling

Unwrap exceptions

By default the ErrorMiddleware is configured to throw a CompletionException. The ClientBuilder.withErrorMiddleware(ErrorMiddleware.ExceptionMode) method allows to configure the middleware to unwrap the causing exception.

ApiRootBuilder.of()
        .defaultClient(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
        .withErrorMiddleware(ErrorMiddleware.ExceptionMode.UNWRAP_COMPLETION_EXCEPTION)
        // ...
        .build();

See the test code.

Not found

The ErrorMiddleware throws for every response with a status code 400 or higher an exception. This can be unwanted behaviour for a NotFoundException. The method ClientBuilder.addNotFoundExceptionMiddleware() configures the client to return a body with a NULL value instead. The method also allows the filtering of specific request to be captured.

ApiRootBuilder.of()
        .defaultClient(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
        .addNotFoundExceptionMiddleware(Collections.singleton(ApiHttpMethod.GET))
        .addNotFoundExceptionMiddleware(apiHttpRequest -> apiHttpRequest.getMethod() == ApiHttpMethod.GET
                || apiHttpRequest.getMethod() == ApiHttpMethod.DELETE)
        // ...
        .build();

See the test code.

  • Constructor Details

    • Configuration

      public Configuration()