Class Using

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

public class Using extends Object
Table of content

Using SDK

SDK follows a builder pattern when creating requests and model entities. Category resource will be used to demonstrate how to use the SDK. This behaviour is the same for all resources.

Models and Builders

Models are defined as interfaces. For each model a Builder class exists which builds the implementation class of a model. The model builder has a build method which checks that required properties have been set. The buildUnchecked method creates a new instance without checking for required properties

Classes of a model
CategoryDraft categoryDraft = CategoryDraftBuilder.of()
        .name(LocalizedStringBuilder.of().addValue("en", "name").build())
        .slug(LocalizedStringBuilder.of().addValue("en", "slug").build())
        .description(LocalizedStringBuilder.of().addValue("en", "description").build())
        .externalId("random-id")
        .key("random-key")
        .metaDescription(LocalizedStringBuilder.of().addValue("en", "metaDescription").build())
        .orderHint("hint")
        .build();

Assertions.assertThat(categoryDraft).isInstanceOf(CategoryDraft.class);

return categoryDraft;

See the test code.

Request DSL

Building request can be done with the help of an ApiRoot, e.g. ProjectApiRoot. The DSL mirrors the directory structure and HTTP methods of the API and allows navigating and discovering the commercetools Composable Commerce API while typing. The HTTP method builders includes methods for every query parameter. Additionally there are the ApiMethod.execute() and RequestCommand.executeBlocking() methods which are sending the request using the configured ApiHttpClient and mapping the result to the correct response class. In case the raw JSON is needed ApiMethod.send() and ApiMethod.sendBlocking() will return an ApiHttpResponse with the byte array.

ProjectApiRoot apiRoot = createProjectClient();

// Use in the previous step configured ApiRoot instance to send and receive a newly created Category
Category category = apiRoot.categories().post(draftBuilder()).executeBlocking().getBody();

// Get Category by id
Category queriedCategory = apiRoot.categories().withId(category.getId()).get().executeBlocking().getBody();

// Get Category by key
Category queriedCategoryByKey = apiRoot.categories()
        .withKey(category.getKey())
        .get()
        .executeBlocking()
        .getBody();

// Query Categories
CategoryPagedQueryResponse response = apiRoot.categories()
        .get()
        .withWhere("id = :catId")
        .withPredicateVar("catId", category.getId())
        .executeBlocking()
        .getBody();

// Update Category
Category updatedCategory = apiRoot.categories()
        .withId(category.getId())
        .post(CategoryUpdateBuilder.of()
                .version(category.getVersion())
                .actions(CategoryChangeNameActionBuilder.of()
                        .name(LocalizedStringBuilder.of().addValue("key-Temp", "value-Temp").build())
                        .build())
                .build())
        .executeBlocking()
        .getBody();

// Delete Category by key
Category deletedCategoryByKey = apiRoot.categories()
        .withKey(category.getKey())
        .delete()
        .withVersion(category.getVersion())
        .executeBlocking()
        .getBody();

See the test code.

  • Constructor Details

    • Using

      public Using()