|
|
|
package com.sigmaflare.binancej;
|
|
|
|
|
|
|
|
|
|
|
|
import com.codepoetics.ambivalence.LeftProjection;
|
|
|
|
import com.codepoetics.ambivalence.RightProjection;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
import static com.sigmaflare.binancej.Constant.BASE_ENDPOINT;
|
|
|
|
|
|
|
|
public final class Helpers {
|
|
|
|
private Helpers() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 Helpers.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 Helpers.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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Safely extracts the value from a LeftProjection of an Either
|
|
|
|
*
|
|
|
|
* @param val The LeftProjection to perform the extraction on
|
|
|
|
* @param <L> The Left type
|
|
|
|
* @param <R> The Right type
|
|
|
|
* @return The unwrapped L type object
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <L, R> L extractEitherValueSafely(LeftProjection<L, R> val) {
|
|
|
|
return (L) val.join(Function.identity(), Function.identity());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Safely extracts the value from a RightProjection of an Either
|
|
|
|
*
|
|
|
|
* @param val The RightProjection to perform the extraction on
|
|
|
|
* @param <L> The Left type
|
|
|
|
* @param <R> The Right type
|
|
|
|
* @return The unwrapped R type object
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <L, R> R extractEitherValueSafely(RightProjection<L, R> val) {
|
|
|
|
return (R) val.join(Function.identity(), Function.identity());
|
|
|
|
}
|
|
|
|
}
|