ninja.poepoe.basewrapper.rest.RestClient.java Source code

Java tutorial

Introduction

Here is the source code for ninja.poepoe.basewrapper.rest.RestClient.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Poe Poe Myint Swe
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package ninja.poepoe.basewrapper.rest;

import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;
import com.squareup.okhttp.logging.HttpLoggingInterceptor.Level;
import java.util.ArrayList;
import java.util.List;
import retrofit.Converter;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;

/**
 * Created by poepoe on 5/12/15.
 */
public abstract class RestClient {

    /**
     * return Retrofit
     */
    public Retrofit RestClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        if (isDebug()) {
            logging.setLevel(logLevel());
        } else {
            logging.setLevel(Level.NONE);
        }

        OkHttpClient client = new OkHttpClient();
        client.interceptors().add(logging);
        client.interceptors().addAll(getInterceptors());

        return new Retrofit.Builder().baseUrl(baseURL()).addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(getFactory()).client(client).build();
    }

    /**
     * override with BuildConfig.DEBUG of your project for logging
     */
    protected abstract boolean isDebug();

    /**
     * override with BaseURL of your project
     */
    protected abstract String baseURL();

    /**
     * override with Log Level for debugging
     */
    protected abstract Level logLevel();

    /**
     * override to add Interceptors
     */
    protected List<Interceptor> getInterceptors() {
        return new ArrayList<>();
    }

    /**
     * default convert factory, override with your choice of factory
     */
    protected Converter.Factory getFactory() {
        return GsonConverterFactory.create();
    }
}