Administrator
2025-12-17 fa7d25e98db0e6cb763d304b95e6a943f43ac240
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.xcong.excoin.modules.okxNewPrice.okxpi.config;
 
 
import com.xcong.excoin.modules.okxNewPrice.utils.FebsException;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.utils.JSONParser;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.utils.OkHttpUtils;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONException;
 
import java.io.IOException;
 
public final class ResponseHandler {
 
    private static final int HTTP_STATUS_CODE_400 = 400;
    private static final int HTTP_STATUS_CODE_499 = 499;
    private static final int HTTP_STATUS_CODE_500 = 500;
 
    private ResponseHandler() {
    }
 
    public static String handleResponse(Request request, boolean showLimitUsage) {
        try (Response response = OkHttpUtils.okHttpClient.newCall(request).execute()) {//OkHttpUtils.builder().okHttpClient
            String responseAsString = getResponseBodyAsString(response.body());
 
            if (response.code() >= HTTP_STATUS_CODE_400 && response.code() <= HTTP_STATUS_CODE_499) {
                throw handleErrorResponse(responseAsString, response.code());
            } else if (response.code() >= HTTP_STATUS_CODE_500) {
                System.out.println("handleResponse:"+response.code());
                throw new FebsException("responseAsString-"+responseAsString+";handleResponse-"+response.code());
            }
            return responseAsString;
//            if (showLimitUsage) {
//                return getlimitUsage(response, responseAsString);
//            } else {
//                return responseAsString;
//            }
        } catch (IOException | IllegalStateException e) {
            e.printStackTrace();
            throw new FebsException("[ResponseHandler] OKHTTP Error: " + e.getMessage());
        }
    }
 
 
    private static FebsException handleErrorResponse(String responseBody, int responseCode) {
        try {
            String errorMsg = JSONParser.getJSONStringValue(responseBody, "msg");
            int errorCode = JSONParser.getJSONIntValue(responseBody, "code");
            return new FebsException("responseBody-"+responseBody+";errorMsg-"+errorMsg+";responseCode-"+responseCode+";errorCode-"+errorCode);
        } catch (JSONException e) {
            throw new FebsException("responseBody-"+responseBody+";responseCode-"+responseCode);
        }
    }
 
    private static String getResponseBodyAsString(ResponseBody body) throws IOException {
        if (null != body) {
            return body.string();
        } else {
            return "";
        }
    }
}