package com.casic.dcms.utils;

import com.casic.dcms.utils.callback.IHttpRequestListener;
import com.pengxh.androidx.lite.callback.OnHttpRequestListener;
import com.pengxh.androidx.lite.hub.RequestHub;
import com.pengxh.androidx.lite.utils.SaveKeyValues;

import okhttp3.Request;
import okhttp3.Response;

public class HttpRequestHelper {
    private static final String TAG = "HttpRequestHelper";

    public static void doRequest(String function, String requestValue, IHttpRequestListener listener) {
        String ip = (String) SaveKeyValues.getValue(LocaleConstant.IP_KEY, "http://111.198.10.15:11409");
//        String ip = (String) SaveKeyValues.getValue(Constant.IP_KEY, "http://223.82.47.232:11401");
        Request request = new Request.Builder()
                .addHeader("token", AuthenticationHelper.getToken())
                .url(ip + function + requestValue)
                .get()
                .build();//创建Request 对象
        RequestHub.doRequest(request, new OnHttpRequestListener() {
            @Override
            public void onSuccess(Response response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Throwable throwable) {
                listener.onFailure(throwable);
            }
        });
    }

    public static void doRequest(String url, IHttpRequestListener listener) {
        Request request = new Request.Builder()
                .addHeader("token", AuthenticationHelper.getToken())
                .url(url)
                .get()
                .build();//创建Request 对象
        RequestHub.doRequest(request, new OnHttpRequestListener() {
            @Override
            public void onSuccess(Response response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Throwable throwable) {
                listener.onFailure(throwable);
            }
        });
    }
}
