// Path: app/src/main/java/moe/yukinoneko/diycode/bean/Token.java
// public class Token {
// 
//     @SerializedName("access_token") public String accessToken;      // 用户令牌(获取相关数据使用)
//     @SerializedName("token_type") public String tokenType;          // 令牌类型
//     @SerializedName("expires_in") public int expiresIn;             // 过期时间
//     @SerializedName("refresh_token") public String refreshToken;    // 刷新令牌(获取新的令牌)
//     @SerializedName("created_at") public int createdAt;             // 创建时间
// }
// 
// Path: app/src/main/java/moe/yukinoneko/diycode/tool/UserHelper.java
// public class UserHelper {
// 
//     private static final String TOKEN = "token";
//     private static final String USER = "user";
// 
//     private static SharedPreferences prefs;
// 
//     public static void init(Context context) {
//         prefs = new SecurePreferences(context.getApplicationContext());
//     }
// 
//     public static void saveToken(Token token) {
//         prefs.edit().putString(TOKEN, GSON.toJson(token)).apply();
//     }
// 
//     public static Token getToken() {
//         String tokenJson = prefs.getString(TOKEN, null);
//         if (tokenJson != null) {
//             return GSON.fromJson(tokenJson, Token.class);
//         }
//         return null;
//     }
// 
//     public static boolean isLogin() {
//         return null != getToken();
//     }
// 
//     public static boolean hasUser() {
//         return null != getUser();
//     }
// 
//     public static void saveUser(User user) {
//         prefs.edit().putString(USER, GSON.toJson(user)).apply();
//     }
// 
//     public static User getUser() {
//         String userJson = prefs.getString(USER, null);
//         if (userJson != null) {
//             return GSON.fromJson(userJson, User.class);
//         }
//         return null;
//     }
// 
//     public static void logout() {
//         prefs.edit().remove(USER).remove(TOKEN).apply();
//     }
// }


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import io.reactivex.schedulers.Schedulers;
import moe.yukinoneko.diycode.bean.Token;
import moe.yukinoneko.diycode.tool.UserHelper;
import okhttp3.Authenticator;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
/*
 * Copyright (c) 2017 SamuelGjk <samuel.alva@outlook.com>
 *
 * This file is part of DiyCode
 *
 * DiyCode is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DiyCode is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DiyCode. If not, see <http://www.gnu.org/licenses/>.
 */

package moe.yukinoneko.diycode.api;




/**
 * Created by SamuelGjk on 2017/3/23.
 */

public class DiyCodeRetrofit {

    public static final Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();

    private Retrofit retrofit;
    private DiyCodeService service;

    private static class HolderClass {
        private static final DiyCodeRetrofit INSTANCE = new DiyCodeRetrofit();
    }

    public static DiyCodeService api() {
        return HolderClass.INSTANCE.service;
    }

    private DiyCodeRetrofit() {

        // 为所有请求自动添加 token
        Interceptor tokenInterceptor = new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request originalRequest = chain.request();
                // 如果当前没有缓存 token 或者请求已经附带 token 了，就不再添加
// Path: app/src/main/java/moe/yukinoneko/diycode/bean/Token.java
// public class Token {
// 
//     @SerializedName("access_token") public String accessToken;      // 用户令牌(获取相关数据使用)
//     @SerializedName("token_type") public String tokenType;          // 令牌类型
//     @SerializedName("expires_in") public int expiresIn;             // 过期时间
//     @SerializedName("refresh_token") public String refreshToken;    // 刷新令牌(获取新的令牌)
//     @SerializedName("created_at") public int createdAt;             // 创建时间
// }
// 
// Path: app/src/main/java/moe/yukinoneko/diycode/tool/UserHelper.java
// public class UserHelper {
// 
//     private static final String TOKEN = "token";
//     private static final String USER = "user";
// 
//     private static SharedPreferences prefs;
// 
//     public static void init(Context context) {
//         prefs = new SecurePreferences(context.getApplicationContext());
//     }
// 
//     public static void saveToken(Token token) {
//         prefs.edit().putString(TOKEN, GSON.toJson(token)).apply();
//     }
// 
//     public static Token getToken() {
//         String tokenJson = prefs.getString(TOKEN, null);
//         if (tokenJson != null) {
//             return GSON.fromJson(tokenJson, Token.class);
//         }
//         return null;
//     }
// 
//     public static boolean isLogin() {
//         return null != getToken();
//     }
// 
//     public static boolean hasUser() {
//         return null != getUser();
//     }
// 
//     public static void saveUser(User user) {
//         prefs.edit().putString(USER, GSON.toJson(user)).apply();
//     }
// 
//     public static User getUser() {
//         String userJson = prefs.getString(USER, null);
//         if (userJson != null) {
//             return GSON.fromJson(userJson, User.class);
//         }
//         return null;
//     }
// 
//     public static void logout() {
//         prefs.edit().remove(USER).remove(TOKEN).apply();
//     }
// }


// Path: app/src/main/java/moe/yukinoneko/diycode/api/DiyCodeRetrofit.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import io.reactivex.schedulers.Schedulers;
import moe.yukinoneko.diycode.bean.Token;
import moe.yukinoneko.diycode.tool.UserHelper;
import okhttp3.Authenticator;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

/*
 * Copyright (c) 2017 SamuelGjk <samuel.alva@outlook.com>
 *
 * This file is part of DiyCode
 *
 * DiyCode is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DiyCode is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DiyCode. If not, see <http://www.gnu.org/licenses/>.
 */

package moe.yukinoneko.diycode.api;




/**
 * Created by SamuelGjk on 2017/3/23.
 */

public class DiyCodeRetrofit {

    public static final Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();

    private Retrofit retrofit;
    private DiyCodeService service;

    private static class HolderClass {
        private static final DiyCodeRetrofit INSTANCE = new DiyCodeRetrofit();
    }

    public static DiyCodeService api() {
        return HolderClass.INSTANCE.service;
    }

    private DiyCodeRetrofit() {

        // 为所有请求自动添加 token
        Interceptor tokenInterceptor = new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request originalRequest = chain.request();
                // 如果当前没有缓存 token 或者请求已经附带 token 了，就不再添加
                if (!UserHelper.isLogin() || alreadyHasAuthorizationHeader(originalRequest)) {