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>Execute a GraphQL query</p>
019 *
020 * <hr>
021 * <div class=code-example>
022 * <pre><code class='java'>{@code
023 *   CompletableFuture<ApiHttpResponse<com.commercetools.api.models.graph_ql.GraphQLResponse>> result = apiRoot
024 *            .withProjectKey("{projectKey}")
025 *            .graphql()
026 *            .post("")
027 *            .execute()
028 * }</code></pre>
029 * </div>
030 */
031@Generated(value = "io.vrap.rmf.codegen.rendering.CoreCodeGenerator", comments = "https://github.com/commercetools/rmf-codegen")
032public class ByProjectKeyGraphqlPostString extends
033        StringBodyApiMethod<ByProjectKeyGraphqlPostString, com.commercetools.api.models.graph_ql.GraphQLResponse>
034        implements com.commercetools.api.client.ErrorableTrait<ByProjectKeyGraphqlPostString> {
035
036    private String projectKey;
037
038    private String graphQLRequest;
039
040    public ByProjectKeyGraphqlPostString(final ApiHttpClient apiHttpClient, String projectKey, String graphQLRequest) {
041        super(apiHttpClient);
042        this.projectKey = projectKey;
043        this.graphQLRequest = graphQLRequest;
044    }
045
046    public ByProjectKeyGraphqlPostString(ByProjectKeyGraphqlPostString t) {
047        super(t);
048        this.projectKey = t.projectKey;
049        this.graphQLRequest = t.graphQLRequest;
050    }
051
052    @Override
053    protected ApiHttpRequest buildHttpRequest() {
054        List<String> params = new ArrayList<>(getQueryParamUriStrings());
055        String httpRequestPath = String.format("%s/graphql", this.projectKey);
056        if (!params.isEmpty()) {
057            httpRequestPath += "?" + String.join("&", params);
058        }
059        return new ApiHttpRequest(ApiHttpMethod.POST, URI.create(httpRequestPath), getHeaders(),
060            graphQLRequest.getBytes(StandardCharsets.UTF_8));
061
062    }
063
064    @Override
065    public ApiHttpResponse<com.commercetools.api.models.graph_ql.GraphQLResponse> executeBlocking(
066            final ApiHttpClient client, final Duration timeout) {
067        return executeBlocking(client, timeout, com.commercetools.api.models.graph_ql.GraphQLResponse.class);
068    }
069
070    @Override
071    public CompletableFuture<ApiHttpResponse<com.commercetools.api.models.graph_ql.GraphQLResponse>> execute(
072            final ApiHttpClient client) {
073        return execute(client, com.commercetools.api.models.graph_ql.GraphQLResponse.class);
074    }
075
076    public String getProjectKey() {
077        return this.projectKey;
078    }
079
080    public void setProjectKey(final String projectKey) {
081        this.projectKey = projectKey;
082    }
083
084    public String getBody() {
085        return graphQLRequest;
086    }
087
088    public ByProjectKeyGraphqlPostString withBody(String graphQLRequest) {
089        ByProjectKeyGraphqlPostString t = copy();
090        t.graphQLRequest = graphQLRequest;
091        return t;
092    }
093
094    @Override
095    public boolean equals(Object o) {
096        if (this == o)
097            return true;
098
099        if (o == null || getClass() != o.getClass())
100            return false;
101
102        ByProjectKeyGraphqlPostString that = (ByProjectKeyGraphqlPostString) o;
103
104        return new EqualsBuilder().append(projectKey, that.projectKey)
105                .append(graphQLRequest, that.graphQLRequest)
106                .isEquals();
107    }
108
109    @Override
110    public int hashCode() {
111        return new HashCodeBuilder(17, 37).append(projectKey).append(graphQLRequest).toHashCode();
112    }
113
114    @Override
115    protected ByProjectKeyGraphqlPostString copy() {
116        return new ByProjectKeyGraphqlPostString(this);
117    }
118}