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

io.sphere.internal.CustomObjectServiceImpl Maven / Gradle / Ivy

There is a newer version: 0.72.1
Show newest version
package io.sphere.internal;

import com.google.common.base.Optional;
import io.sphere.client.CommandRequest;
import io.sphere.client.DeleteRequest;
import io.sphere.client.FetchRequest;
import io.sphere.client.ProjectEndpoints;
import io.sphere.client.shop.CustomObjectService;
import io.sphere.client.model.CustomObject;
import io.sphere.client.shop.ApiMode;
import io.sphere.internal.request.RequestFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class CustomObjectServiceImpl extends ProjectScopedAPI implements CustomObjectService {
    private final RequestFactory requestFactory;
    private final TypeReference typeReference = new TypeReference() {};

    public CustomObjectServiceImpl(RequestFactory requestFactory, ProjectEndpoints endpoints) {
        super(endpoints);
        this.requestFactory = requestFactory;
    }

    @Override
    public FetchRequest get(String container, String key) {
        return requestFactory.createFetchRequestWithErrorHandling(
                endpoints.customObjects.get(container, key),
                Optional.absent(), 404, typeReference);
    }

    @Override
    public  CommandRequest set(String container, String key, T value) {
        final String url = endpoints.customObjects.post();
        ObjectMapper mapper = new ObjectMapper();
        final CustomObject command = new CustomObject(container, key, mapper.valueToTree(value));
        return requestFactory.createCommandRequest(url, command, typeReference);
    }

    @Override
    public DeleteRequest delete(String container, String key) {
        final String url = endpoints.customObjects.get(container, key);
        return requestFactory.createDeleteRequest(url, typeReference);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy