Java tutorial
/* * Copyright (C) 2014 IZITEQ B.V. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package travel.izi.sdk; import com.google.gson.GsonBuilder; import com.squareup.okhttp.Cache; import com.squareup.okhttp.OkHttpClient; import retrofit.RequestInterceptor; import retrofit.RestAdapter; import retrofit.client.Client; import retrofit.client.OkClient; import retrofit.converter.Converter; import retrofit.converter.GsonConverter; import travel.izi.sdk.service.*; import travel.izi.sdk.util.GsonHelper; import travel.izi.sdk.util.MediaHelper; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; @SuppressWarnings("unused") public class IZITravel { public enum Server { Production("http://api.izi.travel", "http://media.izi.travel"), Staging("http://api.stage.izi.travel", "http://media.stage.izi.travel"), Development("http://api.dev.izi.travel", "http://media.dev.izi.travel"); private final String apiUrl; private final String mediaUrl; private Server(String apiUrl, String mediaUrl) { this.apiUrl = apiUrl; this.mediaUrl = mediaUrl; } public String getApiUrl() { return apiUrl; } public String getMediaUrl() { return mediaUrl; } } /** * The default connect timeout for new connections. */ private static final int CONNECT_TIMEOUT_MILLIS = 15 * 1000; // 15s /** * The default read timeout for new connections. */ private static final int READ_TIMEOUT_MILLIS = 20 * 1000; // 20s /** * API version by default. */ public static final String API_VERSION = "1.2"; /** * API version. */ private String mApiVersion = API_VERSION; /** * API key. */ private String mApiKey; /** * API password. Using for getting access to "limited" content. By default available only * public content. */ private String mApiPassword; /** * User agent. */ private String mUserAgent; /** * Whether to return more detailed log output. */ private boolean mIsDebug; /** * Server (by default {@link Server#Development}). */ private Server mServer = Server.Production; /** * HTTP response cache. */ private Cache mCache; /** * Currently valid instance of RestAdapter. */ private RestAdapter mRestAdapter; /** * Currently valid instance of OkHttpClient. */ private OkHttpClient mOkHttpClient; /** * Constructor. */ public IZITravel() { } /** * Set API version. By default used {@link #API_VERSION}. * * @param apiVersion API version value. */ public IZITravel setApiVersion(String apiVersion) { mApiVersion = apiVersion; mRestAdapter = null; return this; } /** * Set API key. All API methods require a valid API key. * * @param apiKey API key value. */ public IZITravel setApiKey(String apiKey) { mApiKey = apiKey; mRestAdapter = null; return this; } /** * Set API password. Using for getting access to "limited" content. By default available only * public content. * * @param apiPassword API password value. */ public IZITravel setApiPassword(String apiPassword) { mApiPassword = apiPassword; mRestAdapter = null; return this; } /** * Set user agent. By default user agent is null. * * @param userAgent User agent value. */ public IZITravel setUserAgent(String userAgent) { mUserAgent = userAgent; mRestAdapter = null; return this; } /** * Whether to return more detailed log output. By default debug is false. * * @param isDebug If true then show full log outputs. */ public IZITravel setDebug(boolean isDebug) { mIsDebug = isDebug; mRestAdapter = null; return this; } /** * Set {@link Server}. By default using {@link Server#Production}. * * @param server Server value. */ public IZITravel setServer(Server server) { mServer = server; mRestAdapter = null; return this; } /** * Set HTTP response cache. * * @param cacheDirectory Cache directory. * @param cacheSize Cache size in bytes. * @throws java.io.IOException */ public void setCache(File cacheDirectory, int cacheSize) throws IOException { mCache = new Cache(cacheDirectory, cacheSize); mOkHttpClient = null; mRestAdapter = null; } protected Client getClient() { if (mOkHttpClient == null) { mOkHttpClient = new OkHttpClient(); mOkHttpClient.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); mOkHttpClient.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); if (mCache != null) { mOkHttpClient.setCache(mCache); } } return new OkClient(mOkHttpClient); } protected RequestInterceptor getRequestInterceptor() { return new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addHeader("Accept", String.format("application/izi-api-v%s+json", mApiVersion)); request.addHeader("X-IZI-API-KEY", mApiKey); if (mApiPassword != null) { request.addHeader("X-IZI-API-PASSWORD", mApiPassword); } if (mUserAgent != null) { request.addHeader("User-Agent", mUserAgent); } request.addHeader("Cache-Control", "max-stale=3600"); } }; } protected Converter getConverter() { GsonBuilder builder = GsonHelper.getGsonBuilder(); return new GsonConverter(builder.create()); } protected RestAdapter buildRestAdapter() { if (mRestAdapter == null) { RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(mServer.getApiUrl()) .setErrorHandler(new IZITravelErrorHandler()).setClient(getClient()) .setRequestInterceptor(getRequestInterceptor()).setConverter(getConverter()); if (mIsDebug) { builder.setLogLevel(RestAdapter.LogLevel.FULL); } mRestAdapter = builder.build(); } return mRestAdapter; } public MtgObjectService mtgObjectService() { return buildRestAdapter().create(MtgObjectService.class); } public CityService cityService() { return buildRestAdapter().create(CityService.class); } public CountryService countryService() { return buildRestAdapter().create(CountryService.class); } public PublisherService publisherService() { return buildRestAdapter().create(PublisherService.class); } public ReviewService reviewService() { return buildRestAdapter().create(ReviewService.class); } public SearchService searchService() { return buildRestAdapter().create(SearchService.class); } public MediaHelper mediaHelper() { return new MediaHelper(mServer.getMediaUrl()); } }