001
002package com.commercetools.api.client;
003
004import java.net.URI;
005import java.time.Duration;
006import java.util.ArrayList;
007import java.util.Collection;
008import java.util.List;
009import java.util.concurrent.CompletableFuture;
010import java.util.function.Function;
011import java.util.function.Supplier;
012import java.util.stream.Collectors;
013
014import io.vrap.rmf.base.client.*;
015import io.vrap.rmf.base.client.utils.Generated;
016
017import org.apache.commons.lang3.builder.EqualsBuilder;
018import org.apache.commons.lang3.builder.HashCodeBuilder;
019
020/**
021 *  <p>If an object with the given container/key exists, the object will be replaced with the new value and the version is incremented. If the request contains a version and an object with the given container/key, then the version must match the version of the existing object. Concurrent updates to the same Custom Object returns a ConcurrentModification error even if the version is not provided.</p>
022 *  <p>Fields with <code>null</code> values will <strong>not be saved</strong>.</p>
023 *
024 * <hr>
025 * <div class=code-example>
026 * <pre><code class='java'>{@code
027 *   CompletableFuture<ApiHttpResponse<com.commercetools.api.models.custom_object.CustomObject>> result = apiRoot
028 *            .withProjectKey("{projectKey}")
029 *            .customObjects()
030 *            .post(null)
031 *            .execute()
032 * }</code></pre>
033 * </div>
034 */
035@Generated(value = "io.vrap.rmf.codegen.rendering.CoreCodeGenerator", comments = "https://github.com/commercetools/rmf-codegen")
036public class ByProjectKeyCustomObjectsPost extends
037        BodyApiMethod<ByProjectKeyCustomObjectsPost, com.commercetools.api.models.custom_object.CustomObject, com.commercetools.api.models.custom_object.CustomObjectDraft>
038        implements com.commercetools.api.client.ExpandableTrait<ByProjectKeyCustomObjectsPost>,
039        com.commercetools.api.client.Deprecatable201Trait<ByProjectKeyCustomObjectsPost>,
040        com.commercetools.api.client.ErrorableTrait<ByProjectKeyCustomObjectsPost> {
041
042    private String projectKey;
043
044    private com.commercetools.api.models.custom_object.CustomObjectDraft customObjectDraft;
045
046    public ByProjectKeyCustomObjectsPost(final ApiHttpClient apiHttpClient, String projectKey,
047            com.commercetools.api.models.custom_object.CustomObjectDraft customObjectDraft) {
048        super(apiHttpClient);
049        this.projectKey = projectKey;
050        this.customObjectDraft = customObjectDraft;
051    }
052
053    public ByProjectKeyCustomObjectsPost(ByProjectKeyCustomObjectsPost t) {
054        super(t);
055        this.projectKey = t.projectKey;
056        this.customObjectDraft = t.customObjectDraft;
057    }
058
059    @Override
060    protected ApiHttpRequest buildHttpRequest() {
061        List<String> params = new ArrayList<>(getQueryParamUriStrings());
062        String httpRequestPath = String.format("%s/custom-objects", this.projectKey);
063        if (!params.isEmpty()) {
064            httpRequestPath += "?" + String.join("&", params);
065        }
066        return new ApiHttpRequest(ApiHttpMethod.POST, URI.create(httpRequestPath), getHeaders(),
067            io.vrap.rmf.base.client.utils.json.JsonUtils
068                    .executing(() -> apiHttpClient().getSerializerService().toJsonByteArray(customObjectDraft)));
069
070    }
071
072    @Override
073    public ApiHttpResponse<com.commercetools.api.models.custom_object.CustomObject> executeBlocking(
074            final ApiHttpClient client, final Duration timeout) {
075        return executeBlocking(client, timeout, com.commercetools.api.models.custom_object.CustomObject.class);
076    }
077
078    @Override
079    public CompletableFuture<ApiHttpResponse<com.commercetools.api.models.custom_object.CustomObject>> execute(
080            final ApiHttpClient client) {
081        return execute(client, com.commercetools.api.models.custom_object.CustomObject.class);
082    }
083
084    public String getProjectKey() {
085        return this.projectKey;
086    }
087
088    public List<String> getExpand() {
089        return this.getQueryParam("expand");
090    }
091
092    public void setProjectKey(final String projectKey) {
093        this.projectKey = projectKey;
094    }
095
096    /**
097     * set expand with the specified value
098     * @param expand value to be set
099     * @param <TValue> value type
100     * @return ByProjectKeyCustomObjectsPost
101     */
102    public <TValue> ByProjectKeyCustomObjectsPost withExpand(final TValue expand) {
103        return copy().withQueryParam("expand", expand);
104    }
105
106    /**
107     * add additional expand query parameter
108     * @param expand value to be added
109     * @param <TValue> value type
110     * @return ByProjectKeyCustomObjectsPost
111     */
112    public <TValue> ByProjectKeyCustomObjectsPost addExpand(final TValue expand) {
113        return copy().addQueryParam("expand", expand);
114    }
115
116    /**
117     * set expand with the specified value
118     * @param supplier supplier for the value to be set
119     * @return ByProjectKeyCustomObjectsPost
120     */
121    public ByProjectKeyCustomObjectsPost withExpand(final Supplier<String> supplier) {
122        return copy().withQueryParam("expand", supplier.get());
123    }
124
125    /**
126     * add additional expand query parameter
127     * @param supplier supplier for the value to be added
128     * @return ByProjectKeyCustomObjectsPost
129     */
130    public ByProjectKeyCustomObjectsPost addExpand(final Supplier<String> supplier) {
131        return copy().addQueryParam("expand", supplier.get());
132    }
133
134    /**
135     * set expand with the specified value
136     * @param op builder for the value to be set
137     * @return ByProjectKeyCustomObjectsPost
138     */
139    public ByProjectKeyCustomObjectsPost withExpand(final Function<StringBuilder, StringBuilder> op) {
140        return copy().withQueryParam("expand", op.apply(new StringBuilder()));
141    }
142
143    /**
144     * add additional expand query parameter
145     * @param op builder for the value to be added
146     * @return ByProjectKeyCustomObjectsPost
147     */
148    public ByProjectKeyCustomObjectsPost addExpand(final Function<StringBuilder, StringBuilder> op) {
149        return copy().addQueryParam("expand", op.apply(new StringBuilder()));
150    }
151
152    /**
153     * set expand with the specified values
154     * @param expand values to be set
155     * @param <TValue> value type
156     * @return ByProjectKeyCustomObjectsPost
157     */
158    public <TValue> ByProjectKeyCustomObjectsPost withExpand(final Collection<TValue> expand) {
159        return copy().withoutQueryParam("expand")
160                .addQueryParams(
161                    expand.stream().map(s -> new ParamEntry<>("expand", s.toString())).collect(Collectors.toList()));
162    }
163
164    /**
165     * add additional expand query parameters
166     * @param expand values to be added
167     * @param <TValue> value type
168     * @return ByProjectKeyCustomObjectsPost
169     */
170    public <TValue> ByProjectKeyCustomObjectsPost addExpand(final Collection<TValue> expand) {
171        return copy().addQueryParams(
172            expand.stream().map(s -> new ParamEntry<>("expand", s.toString())).collect(Collectors.toList()));
173    }
174
175    public com.commercetools.api.models.custom_object.CustomObjectDraft getBody() {
176        return customObjectDraft;
177    }
178
179    public ByProjectKeyCustomObjectsPost withBody(
180            com.commercetools.api.models.custom_object.CustomObjectDraft customObjectDraft) {
181        ByProjectKeyCustomObjectsPost t = copy();
182        t.customObjectDraft = customObjectDraft;
183        return t;
184    }
185
186    @Override
187    public boolean equals(Object o) {
188        if (this == o)
189            return true;
190
191        if (o == null || getClass() != o.getClass())
192            return false;
193
194        ByProjectKeyCustomObjectsPost that = (ByProjectKeyCustomObjectsPost) o;
195
196        return new EqualsBuilder().append(projectKey, that.projectKey)
197                .append(customObjectDraft, that.customObjectDraft)
198                .isEquals();
199    }
200
201    @Override
202    public int hashCode() {
203        return new HashCodeBuilder(17, 37).append(projectKey).append(customObjectDraft).toHashCode();
204    }
205
206    @Override
207    protected ByProjectKeyCustomObjectsPost copy() {
208        return new ByProjectKeyCustomObjectsPost(this);
209    }
210}