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

io.sphere.sdk.queries.MetaModelGetDslImpl Maven / Gradle / Ivy

The newest version!
package io.sphere.sdk.queries;

import com.fasterxml.jackson.databind.JavaType;
import io.sphere.sdk.client.HttpRequestIntent;
import io.sphere.sdk.client.JsonEndpoint;
import io.sphere.sdk.client.SphereRequestUtils;
import io.sphere.sdk.expansion.ExpansionDslUtils;
import io.sphere.sdk.expansion.ExpansionPath;
import io.sphere.sdk.expansion.ExpansionPathContainer;
import io.sphere.sdk.expansion.MetaModelExpansionDslExpansionModelRead;
import io.sphere.sdk.http.HttpMethod;
import io.sphere.sdk.http.NameValuePair;
import io.sphere.sdk.http.HttpResponse;
import io.sphere.sdk.http.UrlQueryBuilder;
import io.sphere.sdk.json.SphereJsonUtils;
import io.sphere.sdk.models.Base;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import static io.sphere.sdk.http.HttpStatusCode.NOT_FOUND_404;
import static io.sphere.sdk.queries.QueryParameterKeys.EXPAND;
import static io.sphere.sdk.utils.SphereInternalUtils.listOf;
import static java.util.Objects.requireNonNull;

/**
 * @param  result type, maybe directly {@code T} or sth. like {@code List}
 * @param  type of the query result
 * @param  type of the class implementing this class
 * @param  type of the expansion model
 */
public abstract class MetaModelGetDslImpl, E> extends Base implements MetaModelGetDsl, MetaModelExpansionDslExpansionModelRead {

    final JavaType javaType;
    final String endpoint;
    /**
     for example an ID, a key, slug, token
     */
    final String identifierToSearchFor;
    final List> expansionPaths;
    final List additionalParameters;
    final E expansionModel;
    final Function, C> builderFunction;

    protected MetaModelGetDslImpl(final JsonEndpoint endpoint, final String identifierToSearchFor, final E expansionModel, final Function, C> builderFunction, final List additionalParameters) {
        this(SphereJsonUtils.convertToJavaType(endpoint.typeReference()), endpoint.endpoint(), identifierToSearchFor, Collections.emptyList(), expansionModel, builderFunction, additionalParameters);
    }

    protected MetaModelGetDslImpl(final String identifierToSearchFor, final JsonEndpoint endpoint, final E expansionModel, final Function, C> builderFunction) {
        this(SphereJsonUtils.convertToJavaType(endpoint.typeReference()), endpoint.endpoint(), identifierToSearchFor, Collections.emptyList(), expansionModel, builderFunction, Collections.emptyList());
    }

    protected MetaModelGetDslImpl(final MetaModelGetDslBuilder builder) {
        this(builder.javaType, builder.endpoint, builder.identifierToSearchFor, builder.expansionPaths, builder.expansionModel, builder.builderFunction, builder.additionalParameters);
    }

    protected MetaModelGetDslImpl(final JavaType javaType, final String endpoint, final String identifierToSearchFor, final List> expansionPaths, final E expansionModel, final Function, C> builderFunction, final List additionalParameters) {
        this.javaType = requireNonNull(javaType);
        this.endpoint = requireNonNull(endpoint);
        this.identifierToSearchFor = requireNonNull(identifierToSearchFor);
        this.expansionPaths = requireNonNull(expansionPaths);
        this.expansionModel = requireNonNull(expansionModel);
        this.builderFunction = requireNonNull(builderFunction);
        this.additionalParameters = requireNonNull(additionalParameters);
    }

    @Nullable
    @Override
    public R deserialize(final HttpResponse httpResponse) {
        return Optional.of(httpResponse)
                .filter(r -> r.getStatusCode() != NOT_FOUND_404)
                .map(r -> SphereRequestUtils.deserialize(r, jacksonJavaType()))
                .orElse(null);
    }

    @Override
    public HttpRequestIntent httpRequestIntent() {
        if (!endpoint.startsWith("/")) {
            throw new RuntimeException("By convention the paths start with a slash, see baseEndpointWithoutId()");
        }
        final boolean urlEncoded = true;
        final UrlQueryBuilder builder = UrlQueryBuilder.of();
        expansionPaths().forEach(path -> builder.add(EXPAND, path.toSphereExpand(), urlEncoded));
        additionalQueryParameters().forEach(parameter -> builder.add(parameter.getName(), parameter.getValue(), urlEncoded));
        final String queryParameters = builder.toStringWithOptionalQuestionMark();
        final String path = endpoint + "/" + identifierToSearchFor + (queryParameters.length() > 1 ? queryParameters : "");
        return HttpRequestIntent.of(HttpMethod.GET, path);
    }

    protected JavaType jacksonJavaType() {
        return javaType;
    }


    protected List additionalQueryParameters() {
        return additionalParameters;
    }

    @Override
    public boolean canDeserialize(final HttpResponse httpResponse) {
        return httpResponse.hasSuccessResponseCode() || httpResponse.getStatusCode() == NOT_FOUND_404;
    }

    @Override
    public List> expansionPaths() {
        return expansionPaths;
    }

    @Override
    public final C withExpansionPaths(final List> expansionPaths) {
        return copyBuilder().expansionPaths(expansionPaths).build();
    }

    @Override
    public C withExpansionPaths(final ExpansionPath expansionPath) {
        return ExpansionDslUtils.withExpansionPaths(this, expansionPath);
    }

    @Override
    public C withExpansionPaths(final Function> m) {
        return ExpansionDslUtils.withExpansionPaths(this, m);
    }

    @Override
    public C plusExpansionPaths(final List> expansionPaths) {
        return withExpansionPaths(listOf(expansionPaths(), expansionPaths));
    }

    @Override
    public C plusExpansionPaths(final ExpansionPath expansionPath) {
        return ExpansionDslUtils.plusExpansionPaths(this, Collections.singletonList(expansionPath));
    }

    @Override
    public C plusExpansionPaths(final Function> m) {
        return ExpansionDslUtils.plusExpansionPaths(this, m);
    }

    @Override
    public C withExpansionPaths(final String expansionPath) {
        return withExpansionPaths(ExpansionPath.of(expansionPath));
    }

    @Override
    public C plusExpansionPaths(final String expansionPath) {
        return plusExpansionPaths(ExpansionPath.of(expansionPath));
    }

    @Override
    public E expansionModel() {
        return expansionModel;
    }

    protected MetaModelGetDslBuilder copyBuilder() {
        return new MetaModelGetDslBuilder<>(this);
    }

    protected C withAdditionalQueryParameters(final List pairs) {
        return copyBuilder().additionalQueryParameters(pairs).build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy