
com.contentful.java.cda.CDAClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk Show documentation
Show all versions of java-sdk Show documentation
Java SDK for Contentful's Content Delivery API.
package com.contentful.java.cda;
import com.contentful.java.cda.interceptor.AuthorizationHeaderInterceptor;
import com.contentful.java.cda.interceptor.ErrorInterceptor;
import com.contentful.java.cda.interceptor.LogInterceptor;
import com.contentful.java.cda.interceptor.UserAgentHeaderInterceptor;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.functions.Func1;
import static com.contentful.java.cda.Constants.ENDPOINT_PROD;
import static com.contentful.java.cda.Constants.PATH_CONTENT_TYPES;
import static com.contentful.java.cda.Util.checkNotNull;
/**
* Client to be used when requesting information from the Delivery API. Every client is associated
* with exactly one Space, but there is no limit to the concurrent number of clients existing at
* any one time. Avoid creating multiple clients for the same Space. Use {@link #builder()}
* to create a new client instance.
*/
public class CDAClient {
final String spaceId;
final String token;
final CDAService service;
final Cache cache;
final Executor callbackExecutor;
final boolean preview;
private CDAClient(Builder builder) {
validate(builder);
this.spaceId = builder.space;
this.token = builder.token;
this.preview = builder.preview;
this.service = createService(builder);
this.cache = new Cache();
this.callbackExecutor = Platform.get().callbackExecutor();
}
private void validate(Builder builder) {
checkNotNull(builder.space, "Space ID must be provided.");
if (builder.callFactory == null) {
checkNotNull(builder.token, "A token must be provided, if no call factory is specified.");
}
}
private CDAService createService(Builder clientBuilder) {
String endpoint = clientBuilder.endpoint;
if (endpoint == null) {
endpoint = ENDPOINT_PROD;
}
Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(ResourceFactory.GSON))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.callFactory(createOrGetCallFactory(clientBuilder))
.baseUrl(endpoint);
return retrofitBuilder.build().create(CDAService.class);
}
private Call.Factory createOrGetCallFactory(Builder clientBuilder) {
final Call.Factory callFactory;
if (clientBuilder.callFactory == null) {
OkHttpClient.Builder okBuilder = new OkHttpClient.Builder()
.addInterceptor(new AuthorizationHeaderInterceptor(clientBuilder.token))
.addInterceptor(new UserAgentHeaderInterceptor(createUserAgent()))
.addInterceptor(new ErrorInterceptor());
okBuilder = setLogger(okBuilder, clientBuilder);
callFactory = okBuilder.build();
} else {
callFactory = clientBuilder.callFactory;
}
return callFactory;
}
private OkHttpClient.Builder setLogger(OkHttpClient.Builder okBuilder, Builder clientBuilder) {
if (clientBuilder.logger != null) {
switch (clientBuilder.logLevel) {
case NONE:
break;
case BASIC:
return okBuilder.addInterceptor(new LogInterceptor(clientBuilder.logger));
case FULL:
return okBuilder.addNetworkInterceptor(new LogInterceptor(clientBuilder.logger));
}
} else {
if (clientBuilder.logLevel != Logger.Level.NONE) {
throw new IllegalArgumentException("Cannot log to a null logger. Please set either logLevel to None, or do set a Logger");
}
}
return okBuilder;
}
/**
* Returns a {@link FetchQuery} for a given {@code type}, which can be used to fulfill the
* request synchronously or asynchronously when a callback is provided.
*
* @param type resource type.
* @param resource type.
* @return query instance.
*/
public FetchQuery fetch(Class type) {
return new FetchQuery(type, this);
}
/**
* Returns an {@link ObserveQuery} for a given {@code type}, which can be used to return
* an {@link Observable} that fetches the desired resources.
*
* @param type resource type.
* @param resource type.
* @return query instance.
*/
public ObserveQuery observe(Class type) {
return new ObserveQuery(type, this);
}
/**
* Returns a {@link SyncQuery} for initial synchronization via the Sync API.
*
* @return query instance.
* @throws UnsupportedOperationException if tried to sync with a preview token
*/
public SyncQuery sync() {
return sync(null, null);
}
/**
* Returns a {@link SyncQuery} for synchronization with the provided {@code syncToken} via
* the Sync API.
*
* @param syncToken sync token.
* @return query instance.
* @throws UnsupportedOperationException if tried to sync with a preview token
*/
public SyncQuery sync(String syncToken) {
return sync(syncToken, null);
}
/**
* Returns a {@link SyncQuery} for synchronization with an existing space.
*
* @param synchronizedSpace space to sync.
* @return query instance.
* @throws UnsupportedOperationException if tried to sync with a preview token
*/
public SyncQuery sync(SynchronizedSpace synchronizedSpace) {
return sync(null, synchronizedSpace);
}
private SyncQuery sync(String syncToken, SynchronizedSpace synchronizedSpace) {
if (preview) {
throw new UnsupportedOperationException("Syncing using a preview token is not supported. Please use a production token.");
}
SyncQuery.Builder builder = SyncQuery.builder().setClient(this);
if (synchronizedSpace != null) {
builder.setSpace(synchronizedSpace);
}
if (syncToken != null) {
builder.setSyncToken(syncToken);
}
return builder.build();
}
/**
* Fetches the space for this client (synchronously).
*/
public CDASpace fetchSpace() {
return observeSpace().toBlocking().first();
}
/**
* Fetches the space for this client (asynchronously).
*/
@SuppressWarnings("unchecked")
public > C fetchSpace(C callback) {
return (C) Callbacks.subscribeAsync(observeSpace(), callback, this);
}
/**
* Returns an {@link Observable} that fetches the space for this client.
*/
public Observable observeSpace() {
return cacheSpace(true);
}
/**
* Caching
*/
Observable cacheAll(final boolean invalidate) {
return cacheSpace(invalidate)
.flatMap(new Func1>>() {
@Override public Observable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy