package com.littlesparkle.growler.library.http.api;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.littlesparkle.growler.library.http.ErrorResponse;

import org.json.JSONException;

import retrofit2.adapter.rxjava.HttpException;

public class Api {
    public static final String BASE_URL = "http://172.16.97.81/api/";

    public static final int DEFAULT_TIMEOUT = 15;
    public static final int CONNECT_TIMEOUT = DEFAULT_TIMEOUT;
    public static final int READ_TIMEOUT = DEFAULT_TIMEOUT;
    public static final int WRITE_TIMEOUT = DEFAULT_TIMEOUT;

    public static final int CODE_SUCCESS = 0;
    public static final int CODE_ERROR = 1;

    public static ErrorResponse handleError(Throwable e) {
        ErrorResponse res;
        if (e instanceof ApiException) {
            Gson gson = new Gson();
            res = gson.fromJson(((ApiException) e).message, ErrorResponse.class);
        } else if (e instanceof HttpException) {
            HttpException he = (HttpException) e;
            res = new ErrorResponse();
            res.data.err_msg = he.message();
            res.data.err_no = String.valueOf(he.code());
        } else {
            res = new ErrorResponse();
            res.data.err_msg = e.getMessage();
            res.data.err_no = e.getLocalizedMessage();
        }
        return res;
    }
}

@@ -1354,11 +1354,402 @@
 s;%0A    %7D
+%0A%0A    public static final String USER_TOKEN_ERROR = %22user_token_error%22;%0A    public static final String%5B%5D ERRPR_NEED_SIGN_IN = %7B%0A            USER_TOKEN_ERROR%0A    %7D;%0A%0A    public static final boolean needReSignIn(String error) %7B%0A        for (String str : ERRPR_NEED_SIGN_IN) %7B%0A            if (str.equals(error)) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D
 %0A%7D%0A

add testing for re-siginin