
io.sphere.internal.request.RequestFactoryImpl Maven / Gradle / Ivy
package io.sphere.internal.request;
import com.google.common.base.Optional;
import io.sphere.client.*;
import io.sphere.internal.command.Command;
import io.sphere.client.filters.expressions.FilterExpression;
import io.sphere.client.model.QueryResult;
import io.sphere.client.model.SearchResult;
import io.sphere.client.shop.ApiMode;
import net.jcip.annotations.Immutable;
import org.codehaus.jackson.type.TypeReference;
import java.util.Locale;
/** Creates specialized requests based on basic GET and POST requests. */
@Immutable
public class RequestFactoryImpl implements RequestFactory {
private final BasicRequestFactory basic;
public RequestFactoryImpl(BasicRequestFactory basic) {
this.basic = basic;
}
// -----------------
// Read
// -----------------
@Override public FetchRequest createFetchRequest(
String url, Optional apiMode, TypeReference jsonParserTypeRef) {
return new FetchRequestImpl(withApiMode(basic.createGet(url), apiMode), jsonParserTypeRef);
}
@Override public FetchRequest createFetchRequestWithErrorHandling(
String url, Optional apiMode, int handledErrorStatus, TypeReference jsonParserTypeRef) {
return new FetchRequestWithErrorHandling(withApiMode(basic.createGet(url), apiMode), handledErrorStatus, jsonParserTypeRef);
}
@Override public FetchRequest createFetchRequestBasedOnQuery(
String url, Optional apiMode, TypeReference> jsonParserTypeRef) {
return new FetchRequestBasedOnQuery(createQueryRequest(url, apiMode, jsonParserTypeRef));
}
@Override public QueryRequest createQueryRequest(
String url, Optional apiMode, TypeReference> jsonParserTypeRef) {
return new QueryRequestImpl(withApiMode(basic.>createGet(url), apiMode), jsonParserTypeRef);
}
@Override public SearchRequest createSearchRequest(
String url, Optional apiMode, Iterable filters, TypeReference> jsonParserTypeRef, Locale locale) {
return new SearchRequestImpl(withApiMode(basic.>createGet(url), apiMode), jsonParserTypeRef, locale).filter(filters);
}
// -----------------
// Write
// -----------------
@Override public CommandRequest createCommandRequest(
String url, Command command, TypeReference jsonParserTypeRef) {
return new CommandRequestImpl(basic.createPost(url), command, jsonParserTypeRef);
}
// -----------------
// Delete
// -----------------
@Override
public DeleteRequest createDeleteRequest(String url, TypeReference jsonParserTypeRef) {
return new DeleteRequestImpl(basic.createDelete(url), jsonParserTypeRef);
}
// -----------------------------------
// API mode helper (staging / live)
// -----------------------------------
private RequestHolder withApiMode(RequestHolder requestHolder, Optional apiMode) {
return apiMode.isPresent() ?
requestHolder.addQueryParameter("staged", apiMode.get() == ApiMode.Staged ? "true" : "false") :
requestHolder;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy