You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
4.2 KiB
116 lines
4.2 KiB
package com.sigmaflare.binancej; |
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
import com.sigmaflare.binancej.entities.ServiceError; |
|
import com.sigmaflare.binancej.exceptions.BinanceServiceException; |
|
import com.sigmaflare.binancej.exceptions.InternalServiceErrorException; |
|
import com.sigmaflare.binancej.exceptions.IpBannedException; |
|
import com.sigmaflare.binancej.exceptions.MalformedRequestException; |
|
import com.sigmaflare.binancej.exceptions.RateLimitExceededException; |
|
import org.apache.http.client.methods.HttpGet; |
|
import org.apache.http.client.methods.HttpPost; |
|
|
|
|
|
import static com.sigmaflare.binancej.Constant.BASE_ENDPOINT; |
|
|
|
public final class HttpRequests { |
|
private HttpRequests() { |
|
} |
|
|
|
/** |
|
* Checks to insure the status code is ok. |
|
* |
|
* @param statusCode The status code |
|
* @return True of the status code is acceptable (200-399), and false otherwise |
|
*/ |
|
public static boolean statusCodeIsOk(int statusCode) { |
|
return statusCode >= 200 && statusCode < 400; |
|
} |
|
|
|
|
|
/** |
|
* Utilizes getBuilder to build a fully functional HttpGet request |
|
* |
|
* @param endpoint The endpoint AFTER the base endpoint (e.g. /api/v1/xyz) |
|
* @param apiKey The API key to use |
|
* @return A configured HttpGet object |
|
*/ |
|
public static HttpGet buildGetRequestFromEndpoint(String endpoint, String apiKey) { |
|
final String url = String.format("%s%s", BASE_ENDPOINT, endpoint); |
|
return HttpRequests.getBuilder(url, apiKey); |
|
} |
|
|
|
/** |
|
* Utilizes postBuilder to build a fully functional HttpPost request |
|
* |
|
* @param endpoint The endpoint AFTER the base endpoint (e.g. /api/v1/xyz) |
|
* @param apiKey The API key to use |
|
* @return A configured HttpPost object |
|
*/ |
|
public static HttpPost buildPostRequestFromEndpoint(String endpoint, String apiKey) { |
|
final String url = String.format("%s%s", BASE_ENDPOINT, endpoint); |
|
return HttpRequests.postBuilder(url, apiKey); |
|
} |
|
|
|
/** |
|
* Builds a configured HttpGet |
|
* |
|
* @param url The full URL to GET to |
|
* @param apiKey The API key |
|
* @return A configured HttpGet object |
|
*/ |
|
private static HttpGet getBuilder(String url, String apiKey) { |
|
HttpGet httpGet = new HttpGet(url); |
|
httpGet.setHeader("X-MBX-APIKEY", apiKey); |
|
return httpGet; |
|
} |
|
|
|
/** |
|
* Builds a configured HttpPost |
|
* |
|
* @param url The full URL to POST to |
|
* @param apiKey The API key |
|
* @return A configured HttpPost object |
|
*/ |
|
private static HttpPost postBuilder(String url, String apiKey) { |
|
HttpPost httpPost = new HttpPost(url); |
|
httpPost.setHeader("X-MBX-APIKEY", apiKey); |
|
return httpPost; |
|
} |
|
|
|
/** |
|
* Builds a fully configured ObjectMapper |
|
* |
|
* @return A fully configured ObjectMapper |
|
*/ |
|
public static ObjectMapper objectMapperBuilder() { |
|
return new ObjectMapper().findAndRegisterModules(); |
|
} |
|
|
|
/** |
|
* A simple function to consolidate the creation of common HTTP exceptions |
|
* @param statusCode The status code to build the exception for |
|
* @param error The ServiceError object returned from Binance to use to build the exception |
|
* @return A BinanceServiceException based on the provided statusCode argument |
|
* @throws IllegalArgumentException If the statusCode is below 400 |
|
*/ |
|
public static BinanceServiceException buildHttpException(int statusCode, ServiceError error) { |
|
if(statusCode < 400) { |
|
throw new IllegalArgumentException( |
|
String.format("Status codes below 400 (statusCode = %d) do not have an exception", statusCode)); |
|
} |
|
|
|
if (statusCode == 418) { |
|
return new IpBannedException(error.getMessage()); |
|
} else if (statusCode == 429) { |
|
return new RateLimitExceededException(error.getMessage()); |
|
} else if (statusCode == 504) { |
|
return new InternalServiceErrorException(error.getCode(), error.getMessage()); |
|
} else if (statusCode < 500) { |
|
return new MalformedRequestException(error.getCode(), error.getMessage()); |
|
} else { |
|
return new InternalServiceErrorException(error.getCode(), error.getMessage()); |
|
} |
|
} |
|
}
|
|
|