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.
88 lines
2.5 KiB
88 lines
2.5 KiB
6 years ago
|
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;
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Builds a configured HttpGet
|
||
|
*
|
||
|
* @param url The full URL to GET to
|
||
|
* @param apiKey The API key
|
||
|
* @return A configured HttpGet object
|
||
|
*/
|
||
|
public 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
|
||
|
*/
|
||
|
public 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());
|
||
|
}
|
||
|
}
|