All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sphere.sdk.commands.CreateCommandImpl Maven / Gradle / Ivy

There is a newer version: 1.0.0-M26
Show newest version
package io.sphere.sdk.commands;

import com.fasterxml.jackson.core.type.TypeReference;
import io.sphere.sdk.client.HttpRequestIntent;
import io.sphere.sdk.http.HttpMethod;
import io.sphere.sdk.client.JsonEndpoint;

import static io.sphere.sdk.json.SphereJsonUtils.toJsonString;
import static java.util.Objects.requireNonNull;

/**
 * Base class to implement commands which create an entity in SPHERE.IO.
 *
 * @param  the type of the result of the command, most likely the updated entity without expanded references
 * @param  class which will serialized as JSON command body, most likely a template
 */
public abstract class CreateCommandImpl extends CommandImpl implements CreateCommand{

    private final C body;
    private final JsonEndpoint endpoint;

    public CreateCommandImpl(final C draft, final JsonEndpoint endpoint) {
        this.body = requireNonNull(draft);
        this.endpoint = requireNonNull(endpoint);
    }

    @Override
    public HttpRequestIntent httpRequestIntent() {
        return HttpRequestIntent.of(httpMethod(), endpoint.endpoint(), httpBody());
    }

    protected HttpMethod httpMethod() {
        return HttpMethod.POST;
    }

    protected String httpBody() {
        return toJsonString(body);
    }

    @Override
    protected TypeReference typeReference() {
        return endpoint.typeReference();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy