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

io.sphere.internal.request.FetchRequestWithErrorHandling Maven / Gradle / Ivy

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

import com.google.common.base.Optional;
import io.sphere.internal.util.Util;
import io.sphere.client.FetchRequest;

import com.google.common.util.concurrent.ListenableFuture;
import org.codehaus.jackson.type.TypeReference;

public class FetchRequestWithErrorHandling implements FetchRequest, TestableRequest {
    RequestHolder requestHolder;
    int handledErrorStatus;
    TypeReference jsonParserTypeRef;

    public FetchRequestWithErrorHandling(RequestHolder requestHolder, int handledErrorStatus, TypeReference jsonParserTypeRef) {
        if (requestHolder == null) throw new NullPointerException("requestHolder");
        if (jsonParserTypeRef == null) throw new NullPointerException("jsonParserTypeRef");
        this.requestHolder = requestHolder;
        this.handledErrorStatus = handledErrorStatus;
        this.jsonParserTypeRef = jsonParserTypeRef;
    }

    @Override public Optional fetch() {
        return Util.sync(fetchAsync());
    }

    @Override public ListenableFuture> fetchAsync() {
        return RequestExecutor.executeAndHandleError(requestHolder, handledErrorStatus, jsonParserTypeRef);
    }

    @Override public FetchRequest expand(String... paths) {
        for (String path: paths) {
            requestHolder.addQueryParameter("expand", path);
        }
        return this;
    }

    // testing purposes
    @Override public TestableRequestHolder getRequestHolder() {
        return requestHolder;
    }

    // logging and debugging purposes
    @Override public String toString() {
        return getRequestHolder().toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy