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

io.sphere.sdk.customobjects.queries.CustomObjectByKeyGetImpl Maven / Gradle / Ivy

There is a newer version: 2.16.0
Show newest version
package io.sphere.sdk.customobjects.queries;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import io.sphere.sdk.client.JsonEndpoint;
import io.sphere.sdk.customobjects.CustomObject;
import io.sphere.sdk.customobjects.expansion.CustomObjectExpansionModel;
import io.sphere.sdk.http.HttpResponse;
import io.sphere.sdk.json.SphereJsonUtils;
import io.sphere.sdk.queries.MetaModelGetDslBuilder;
import io.sphere.sdk.queries.MetaModelGetDslImpl;
import java.util.Optional;

final class CustomObjectByKeyGetImpl extends MetaModelGetDslImpl, CustomObject, CustomObjectByKeyGet, CustomObjectExpansionModel>> implements CustomObjectByKeyGet {

    private final JavaType customObjectJavaType;

    public CustomObjectByKeyGetImpl(final String container, final String key, final JavaType javaType) {
        super(container + "/" + key, JsonEndpoint.of(new TypeReference>() {
            @Override
            public String toString() {
                return "TypeReference>";
            }
        }, "/custom-objects"), CustomObjectExpansionModel.of(), att -> new CustomObjectByKeyGetImpl(att, javaType));
        this.customObjectJavaType = SphereJsonUtils.createCustomObjectJavaType(CustomObject.class, javaType.getRawClass());

    }

    CustomObjectByKeyGetImpl(final MetaModelGetDslBuilder, CustomObject, CustomObjectByKeyGet, CustomObjectExpansionModel>> builder, JavaType javaType){
        super(builder);
        this.customObjectJavaType = SphereJsonUtils.createCustomObjectJavaType(CustomObject.class, javaType.getRawClass());
    }

    @Override
    public CustomObject deserialize(final HttpResponse httpResponse) {
        return deserializeCustomObject(httpResponse);
    }

    protected CustomObject deserializeCustomObject(final HttpResponse httpResponse) {

        if(!httpResponse.hasSuccessResponseCode()){
            return null;
        }

        return Optional.ofNullable(httpResponse)
                .filter(response -> response.getResponseBody() != null && response.getResponseBody().length > 0)
                .map(response -> response.getResponseBody())
                .map(responseBody -> SphereJsonUtils.>readObject(httpResponse.getResponseBody(), customObjectJavaType))
                .orElse(null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy