001
002package com.commercetools.api.client;
003
004import java.net.URI;
005import java.nio.charset.StandardCharsets;
006import java.time.Duration;
007import java.util.ArrayList;
008import java.util.List;
009import java.util.concurrent.CompletableFuture;
010
011import io.vrap.rmf.base.client.*;
012import io.vrap.rmf.base.client.utils.Generated;
013
014import org.apache.commons.lang3.builder.EqualsBuilder;
015import org.apache.commons.lang3.builder.HashCodeBuilder;
016
017/**
018 *  <p>Creates a new Cart by replicating an existing Cart or Order of the authenticated Customer. This can be useful in cases where a customer wants to cancel a recent order to make some changes or reorder a previous order.</p>
019 *  <p>The replicated Cart preserves Customer information, Line Items and Custom Line Items, Custom Fields, Discount Codes, and other settings of the Cart or Order. If the Line Items become invalid, for example, due to removed Products or Prices, they are removed from the new Cart. If the Customer switches to another Customer Group, the new Cart is updated with the new value. It has up-to-date Tax Rates, Prices, and Line Item product data and is in <code>Active</code> CartState.</p>
020 *  <p>The new Cart does not contain Payments or Deliveries. The State of Line Items and Custom Line Items is reset to <code>initial</code>.</p>
021 *  <p>In case the Cart or Order to be replicated does not belong to the authenticaed Customer, the API returns a ResourceNotFound error</p>
022 *
023 * <hr>
024 * <div class=code-example>
025 * <pre><code class='java'>{@code
026 *   CompletableFuture<ApiHttpResponse<com.commercetools.api.models.cart.Cart>> result = apiRoot
027 *            .withProjectKey("{projectKey}")
028 *            .me()
029 *            .carts()
030 *            .replicate()
031 *            .post("")
032 *            .execute()
033 * }</code></pre>
034 * </div>
035 */
036@Generated(value = "io.vrap.rmf.codegen.rendering.CoreCodeGenerator", comments = "https://github.com/commercetools/rmf-codegen")
037public class ByProjectKeyMeCartsReplicatePostString
038        extends StringBodyApiMethod<ByProjectKeyMeCartsReplicatePostString, com.commercetools.api.models.cart.Cart>
039        implements com.commercetools.api.client.ErrorableTrait<ByProjectKeyMeCartsReplicatePostString> {
040
041    private String projectKey;
042
043    private String replicaMyCartDraft;
044
045    public ByProjectKeyMeCartsReplicatePostString(final ApiHttpClient apiHttpClient, String projectKey,
046            String replicaMyCartDraft) {
047        super(apiHttpClient);
048        this.projectKey = projectKey;
049        this.replicaMyCartDraft = replicaMyCartDraft;
050    }
051
052    public ByProjectKeyMeCartsReplicatePostString(ByProjectKeyMeCartsReplicatePostString t) {
053        super(t);
054        this.projectKey = t.projectKey;
055        this.replicaMyCartDraft = t.replicaMyCartDraft;
056    }
057
058    @Override
059    protected ApiHttpRequest buildHttpRequest() {
060        List<String> params = new ArrayList<>(getQueryParamUriStrings());
061        String httpRequestPath = String.format("%s/me/carts/replicate", this.projectKey);
062        if (!params.isEmpty()) {
063            httpRequestPath += "?" + String.join("&", params);
064        }
065        return new ApiHttpRequest(ApiHttpMethod.POST, URI.create(httpRequestPath), getHeaders(),
066            replicaMyCartDraft.getBytes(StandardCharsets.UTF_8));
067
068    }
069
070    @Override
071    public ApiHttpResponse<com.commercetools.api.models.cart.Cart> executeBlocking(final ApiHttpClient client,
072            final Duration timeout) {
073        return executeBlocking(client, timeout, com.commercetools.api.models.cart.Cart.class);
074    }
075
076    @Override
077    public CompletableFuture<ApiHttpResponse<com.commercetools.api.models.cart.Cart>> execute(
078            final ApiHttpClient client) {
079        return execute(client, com.commercetools.api.models.cart.Cart.class);
080    }
081
082    public String getProjectKey() {
083        return this.projectKey;
084    }
085
086    public void setProjectKey(final String projectKey) {
087        this.projectKey = projectKey;
088    }
089
090    public String getBody() {
091        return replicaMyCartDraft;
092    }
093
094    public ByProjectKeyMeCartsReplicatePostString withBody(String replicaMyCartDraft) {
095        ByProjectKeyMeCartsReplicatePostString t = copy();
096        t.replicaMyCartDraft = replicaMyCartDraft;
097        return t;
098    }
099
100    @Override
101    public boolean equals(Object o) {
102        if (this == o)
103            return true;
104
105        if (o == null || getClass() != o.getClass())
106            return false;
107
108        ByProjectKeyMeCartsReplicatePostString that = (ByProjectKeyMeCartsReplicatePostString) o;
109
110        return new EqualsBuilder().append(projectKey, that.projectKey)
111                .append(replicaMyCartDraft, that.replicaMyCartDraft)
112                .isEquals();
113    }
114
115    @Override
116    public int hashCode() {
117        return new HashCodeBuilder(17, 37).append(projectKey).append(replicaMyCartDraft).toHashCode();
118    }
119
120    @Override
121    protected ByProjectKeyMeCartsReplicatePostString copy() {
122        return new ByProjectKeyMeCartsReplicatePostString(this);
123    }
124}