Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added RetryAfter header handler #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class BinanceClientException extends RuntimeException {
private final int httpStatusCode;
private final int errorCode;
private String errMsg;
private int retryAfter;

public BinanceClientException(String fullErrMsg, int httpStatusCode) {
super(fullErrMsg);
Expand All @@ -19,6 +20,11 @@ public BinanceClientException(String fullErrMsg, String errMsg, int httpStatusCo
this.errorCode = errorCode;
this.errMsg = errMsg;
}

public BinanceClientException(String fullErrMsg, String errMsg, int httpStatusCode, int errorCode, int retryAfter) {
this(fullErrMsg, errMsg, httpStatusCode, errorCode);
this.retryAfter = retryAfter;
}

public int getErrorCode() {
return errorCode;
Expand All @@ -31,4 +37,8 @@ public int getHttpStatusCode() {
public String getErrMsg() {
return errMsg;
}

public int getRetryAfter() {
return retryAfter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.binance.connector.client.exceptions.BinanceServerException;
import com.binance.connector.client.utils.httpclient.HttpClientSingleton;

import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
Expand All @@ -36,7 +37,7 @@ public static String handleResponse(Request request, boolean showLimitUsage, Pro
String responseAsString = getResponseBodyAsString(response.body());

if (response.code() >= HTTP_STATUS_CODE_400 && response.code() <= HTTP_STATUS_CODE_499) {
throw handleErrorResponse(responseAsString, response.code());
throw handleErrorResponse(responseAsString, response.code(), response.headers());
} else if (response.code() >= HTTP_STATUS_CODE_500) {
throw new BinanceServerException(responseAsString, response.code());
}
Expand Down Expand Up @@ -70,15 +71,27 @@ private static String getLimitUsage(Response response, String resposeBodyAsStrin
return json.toString();
}

private static BinanceClientException handleErrorResponse(String responseBody, int responseCode) {
private static BinanceClientException handleErrorResponse(String responseBody, int responseCode, Headers headers) {
try {
String errorMsg = JSONParser.getJSONStringValue(responseBody, "msg");
int errorCode = JSONParser.getJSONIntValue(responseBody, "code");
if(responseCode == 429 || responseCode == 418) {
return handleLimitErrorResponse(responseBody, errorMsg, responseCode, errorCode, headers);
}
return new BinanceClientException(responseBody, errorMsg, responseCode, errorCode);
} catch (JSONException e) {
throw new BinanceClientException(responseBody, responseCode);
}
}

private static BinanceClientException handleLimitErrorResponse(String responseBody, String errorMsg, int responseCode, int errorCode, Headers headers) {
try {
int retryAfter = Integer.parseInt(headers.get("retry-after"));
return new BinanceClientException(responseBody, errorMsg, responseCode, errorCode, retryAfter);
} catch (Exception e) {
return new BinanceClientException(responseBody, errorMsg, responseCode, errorCode);
}
}

private static String getResponseBodyAsString(ResponseBody body) throws IOException {
if (null != body) {
Expand Down