package com.dahe.pass.net;


import com.dahe.mylibrary.CommonBaseLibrary;
import com.dahe.mylibrary.net.JsonInterceptor;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.core.ObservableTransformer;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2018/8/8 0008.
 */

public class DataManager {
    private volatile static DataManager mInstance;

    public DataManager() {

    }

    public static <T> ObservableTransformer<T, T> setThread() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    public static Api getInstance() {
        if (null == mInstance) {
            synchronized (DataManager.class) {
                if (null == mInstance) {
                    mInstance = new DataManager();
                }
            }
        }
        return api();
    }

    public static Api api() {
        return CommonBaseLibrary.getRetrofit().newBuilder()
                .client(new OkHttpClient.Builder()
                        .connectTimeout(30, TimeUnit.SECONDS)
                        .readTimeout(30, TimeUnit.SECONDS)
                        .writeTimeout(30, TimeUnit.SECONDS)
                        .addInterceptor(new JsonInterceptor())
                        .addInterceptor(new RequestHeadInterceptor())
                        .build())
//                .addNetworkInterceptor(new TokenInterceptor())
                .build().create(Api.class);
    }

}
