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: 1.0.0-M26
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.customobjects.CustomObject;
import io.sphere.sdk.customobjects.CustomObjectUtils;
import io.sphere.sdk.http.HttpResponse;
import io.sphere.sdk.json.SphereJsonUtils;

import java.util.Optional;

final class CustomObjectByKeyGetImpl extends CustomObjectCustomJsonMappingByKeyGet implements CustomObjectByKeyGet {
    private final JavaType javaType;

    public CustomObjectByKeyGetImpl(final String container, final String key, final JavaType javaType) {
        super(container, key);
        this.javaType = CustomObjectUtils.getCustomObjectJavaTypeForValue(javaType);
    }

    public CustomObjectByKeyGetImpl(final String container, final String key, final Class clazz) {
        this(container, key, SphereJsonUtils.convertToJavaType(clazz));
    }

    public CustomObjectByKeyGetImpl(final String container, final String key, final TypeReference typeReference) {
        this(container, key, SphereJsonUtils.convertToJavaType(typeReference));
    }

    @Override
    protected CustomObject deserializeCustomObject(final HttpResponse httpResponse) {
        return Optional.ofNullable(httpResponse)
                .filter(response -> response.getResponseBody() != null && response.getResponseBody().length > 0)
                .map(response -> response.getResponseBody())
                .map(responseBody -> SphereJsonUtils.>readObject(httpResponse.getResponseBody(), javaType))
                .orElse(null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy