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 example for custom cart update action #466

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@

package commercetools.custom_command;

import java.net.URI;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import com.commercetools.api.client.ByProjectKeyCartsByIDPost;
import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.models.cart.*;
import commercetools.utils.CommercetoolsTestUtils;

import io.vrap.rmf.base.client.ApiHttpClient;
import io.vrap.rmf.base.client.ApiHttpRequest;
import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.CreateHttpRequestCommand;
import io.vrap.rmf.base.client.ResponseSerializer;

import org.junit.jupiter.api.Test;

public class CartDecoratorTest {
@Test
public void decoratedClient() {
final ProjectApiRoot apiRoot = CommercetoolsTestUtils.getProjectApiRoot();
final DecoratedApiHttpClient client = new DecoratedApiHttpClient(apiRoot.getApiHttpClient(), true);
final ProjectApiRoot decoratedRoot = ProjectApiRoot.fromClient(apiRoot.getProjectKey(), client);
final CartDraft cartDraft = CartDraftBuilder.of().currency("EUR").build();

final CartRecalculateAction cartRecalculateAction = CartUpdateActionBuilder.of().recalculateBuilder().build();
final CartChangeTaxModeAction cartChangeTaxModeAction = CartUpdateActionBuilder.of()
.changeTaxModeBuilder()
.taxMode(TaxMode.DISABLED)
.build();
final Cart cart = decoratedRoot.carts().create(cartDraft).execute().thenApply(ApiHttpResponse::getBody).join();
final Cart updatedCart = decoratedRoot.carts()
.withId(cart.getId())
.post(IncludeCartCartUpdateBuilder.of()
.cart(cart)
.version(cart.getVersion())
.actions(cartRecalculateAction, cartChangeTaxModeAction)
.build())
.execute()
.thenApply(ApiHttpResponse::getBody)
.join();
}

public static class DecoratedApiHttpClient implements ApiHttpClient {
private final ApiHttpClient apiHttpClient;

private ApiHttpRequest lastRequest;

private boolean enableResetLineItemExternalTotals;

public DecoratedApiHttpClient(ApiHttpClient apiHttpClient, boolean enableResetLineItemExternalTotals) {
this.apiHttpClient = apiHttpClient;
this.enableResetLineItemExternalTotals = enableResetLineItemExternalTotals;
}

@Override
public ResponseSerializer getSerializerService() {
return apiHttpClient.getSerializerService();
}

@Override
public URI getBaseUri() {
return apiHttpClient.getBaseUri();
}

@Override
public CompletableFuture<ApiHttpResponse<byte[]>> execute(ApiHttpRequest request) {
lastRequest = request;
return apiHttpClient.execute(request);
}

@Override
public <O> CompletableFuture<ApiHttpResponse<O>> execute(CreateHttpRequestCommand request,
Function<ApiHttpResponse<byte[]>, ApiHttpResponse<O>> mapper) {
return ApiHttpClient.super.execute(resetLineItemExternalTotals(request), mapper);
}

@Override
public <O> ApiHttpResponse<O> executeBlocking(CreateHttpRequestCommand method,
Function<ApiHttpResponse<byte[]>, ApiHttpResponse<O>> mapper, Duration timeout) {
return ApiHttpClient.super.executeBlocking(resetLineItemExternalTotals(method), mapper, timeout);
}

@Override
public CompletableFuture<ApiHttpResponse<byte[]>> send(CreateHttpRequestCommand method) {
return ApiHttpClient.super.send(resetLineItemExternalTotals(method));
}

@Override
public <T> ApiHttpResponse<byte[]> sendBlocking(CreateHttpRequestCommand method, Duration timeout) {
return ApiHttpClient.super.sendBlocking(resetLineItemExternalTotals(method), timeout);
}

@Override
public void close() throws Exception {
apiHttpClient.close();
}

public CreateHttpRequestCommand resetLineItemExternalTotals(CreateHttpRequestCommand method) {
if (enableResetLineItemExternalTotals && method instanceof ByProjectKeyCartsByIDPost) {
final ByProjectKeyCartsByIDPost byProjectKeyCartsByIDPost = (ByProjectKeyCartsByIDPost) method;
final CartUpdate cartUpdate = byProjectKeyCartsByIDPost.getBody();
if (cartUpdate instanceof WithCartCartUpdateImpl) {
final WithCartCartUpdateImpl withCartCartUpdate = (WithCartCartUpdateImpl) cartUpdate;
final Cart cart = withCartCartUpdate.getCart();

// do sth with the cart
}
}

return method;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

package commercetools.custom_command;

import java.util.List;
import java.util.Objects;

import com.commercetools.api.models.cart.Cart;
import com.commercetools.api.models.cart.CartUpdate;
import com.commercetools.api.models.cart.CartUpdateAction;
import com.commercetools.api.models.cart.CartUpdateBuilder;

public class IncludeCartCartUpdateBuilder extends CartUpdateBuilder {
private Cart cart;

public Cart getCart() {
return cart;
}

public IncludeCartCartUpdateBuilder cart(Cart cart) {
this.cart = cart;
return this;
}

public static IncludeCartCartUpdateBuilder of() {
return new IncludeCartCartUpdateBuilder();
}

@Override
public CartUpdate build() {
final Long version = super.getVersion();
final List<CartUpdateAction> actions = super.getActions();
Objects.requireNonNull(version, CartUpdate.class + ": version is missing");
Objects.requireNonNull(actions, CartUpdate.class + ": actions is missing");
Objects.requireNonNull(cart, CartUpdate.class + ": cart is missing");
return new WithCartCartUpdateImpl(cart, version, actions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

package commercetools.custom_command;

import com.commercetools.api.models.cart.Cart;

public class WithCartCartUpdateImpl extends com.commercetools.api.models.cart.CartUpdateImpl {

private Cart cart;

public WithCartCartUpdateImpl(final Cart cart, final Long version,
final java.util.List<com.commercetools.api.models.cart.CartUpdateAction> actions) {
this.cart = cart;
super.setActions(actions);
super.setVersion(version);
}

public Cart getCart() {
return cart;
}

public void setCart(final Cart cart) {
this.cart = cart;
}
}