@ -20,10 +20,10 @@ import org.apache.http.util.EntityUtils;
import java.io.IOException ;
import java.util.List ;
import static com.sigmaflare.binancej.Constant.BASE_ENDPOINT ;
import static com.sigmaflare.binancej.Constant.NO_RESPONSE_TEXT ;
import static com.sigmaflare.binancej.Constant.NO_RESPONSE_TEXT_FORMATTED ;
import static com.sigmaflare.binancej.Constant.SYMBOL_AND_INTERVAL_MUST_BE_SUPPLIED ;
import static com.sigmaflare.binancej.Helpers.buildGetRequestFromEndpoint ;
@Slf4j
public class MarketData extends BaseBinanceApi {
@ -62,9 +62,8 @@ public class MarketData extends BaseBinanceApi {
* /
public Either < ServiceError , OrderBookDepth > getOrderBookDepth ( String symbol , int limit )
throws BinanceServiceUnreachableException {
String url = String . format ( "%s%s?symbol=%s&limit=%d" , BASE_ENDPOINT , ORDER_BOOK_URL , symbol , limit ) ;
final HttpGet request = Helpers . getBuilder ( url , apiKey ) ;
String urlWithParams = String . format ( "%s?symbol=%s&limit=%d" , ORDER_BOOK_URL , symbol , limit ) ;
final HttpGet request = buildGetRequestFromEndpoint ( urlWithParams , apiKey ) ;
try {
try ( CloseableHttpResponse closeableHttpResponse = closeableHttpClient . execute ( request ) ) {
@ -73,8 +72,9 @@ public class MarketData extends BaseBinanceApi {
HttpEntity httpEntity = closeableHttpResponse . getEntity ( ) ;
if ( httpEntity = = null ) {
log . error ( NO_RESPONSE_TEXT , url ) ;
throw new BinanceServiceUnreachableException ( String . format ( NO_RESPONSE_TEXT_FORMATTED , url ) , null ) ;
log . error ( NO_RESPONSE_TEXT , request . getURI ( ) . toASCIIString ( ) ) ;
throw new BinanceServiceUnreachableException (
String . format ( NO_RESPONSE_TEXT_FORMATTED , request . getURI ( ) . toASCIIString ( ) ) , null ) ;
}
String response = EntityUtils . toString ( httpEntity ) ;
@ -107,8 +107,7 @@ public class MarketData extends BaseBinanceApi {
}
String url = String . format (
"%s%s?symbol=%s&interval=%s" ,
BASE_ENDPOINT ,
"%s?symbol=%s&interval=%s" ,
CANDLESTICK_URL ,
symbol ,
interval . toString ( ) ) ;
@ -134,8 +133,7 @@ public class MarketData extends BaseBinanceApi {
}
String url = String . format (
"%s%s?symbol=%s&interval=%s&limit=%d" ,
BASE_ENDPOINT ,
"%s?symbol=%s&interval=%s&limit=%d" ,
CANDLESTICK_URL ,
symbol ,
interval . toString ( ) ,
@ -167,16 +165,14 @@ public class MarketData extends BaseBinanceApi {
if ( isStartTime ) {
url = String . format (
"%s%s?symbol=%s&interval=%s&startTime=%d" ,
BASE_ENDPOINT ,
"%s?symbol=%s&interval=%s&startTime=%d" ,
CANDLESTICK_URL ,
symbol ,
interval . toString ( ) ,
time ) ;
} else {
url = String . format (
"%s%s?symbol=%s&interval=%s&endTime=%d" ,
BASE_ENDPOINT ,
"%s?symbol=%s&interval=%s&endTime=%d" ,
CANDLESTICK_URL ,
symbol ,
interval . toString ( ) ,
@ -210,8 +206,7 @@ public class MarketData extends BaseBinanceApi {
if ( isStartTime ) {
url = String . format (
"%s%s?symbol=%s&interval=%s&limit=%d&startTime=%d" ,
BASE_ENDPOINT ,
"%s?symbol=%s&interval=%s&limit=%d&startTime=%d" ,
CANDLESTICK_URL ,
symbol ,
interval . toString ( ) ,
@ -219,8 +214,7 @@ public class MarketData extends BaseBinanceApi {
time ) ;
} else {
url = String . format (
"%s%s?symbol=%s&interval=%s&limit=%d&endTime=%d" ,
BASE_ENDPOINT ,
"%s?symbol=%s&interval=%s&limit=%d&endTime=%d" ,
CANDLESTICK_URL ,
symbol ,
interval . toString ( ) ,
@ -254,8 +248,7 @@ public class MarketData extends BaseBinanceApi {
String url ;
url = String . format (
"%s%s?symbol=%s&interval=%s&limit=%d&startTime=%d&endTime=%d" ,
BASE_ENDPOINT ,
"%s?symbol=%s&interval=%s&limit=%d&startTime=%d&endTime=%d" ,
CANDLESTICK_URL ,
symbol ,
interval . toString ( ) ,
@ -278,9 +271,9 @@ public class MarketData extends BaseBinanceApi {
if ( symbol = = null ) {
throw new IllegalArgumentException ( "Symbol must not be null" ) ;
}
String url = String . format ( "%s%s?symbol=%s" , BASE_ENDPOINT , TICKER_PRICE_URL , symbol ) ;
final HttpGet request = Helpers . getBuilder ( url , apiKey ) ;
String urlWithParams = String . format ( "%s?symbol=%s" , TICKER_PRICE_URL , symbol ) ;
final HttpGet request = buildGetRequestFromEndpoint ( urlWithParams , apiKey ) ;
try {
try ( CloseableHttpResponse closeableHttpResponse = closeableHttpClient . execute ( request ) ) {
@ -289,8 +282,9 @@ public class MarketData extends BaseBinanceApi {
HttpEntity httpEntity = closeableHttpResponse . getEntity ( ) ;
if ( httpEntity = = null ) {
log . error ( NO_RESPONSE_TEXT , url ) ;
throw new BinanceServiceUnreachableException ( String . format ( NO_RESPONSE_TEXT_FORMATTED , url ) , null ) ;
log . error ( NO_RESPONSE_TEXT , request . getURI ( ) . toASCIIString ( ) ) ;
throw new BinanceServiceUnreachableException (
String . format ( NO_RESPONSE_TEXT_FORMATTED , request . getURI ( ) . toASCIIString ( ) ) , null ) ;
}
String response = EntityUtils . toString ( httpEntity ) ;
@ -314,9 +308,7 @@ public class MarketData extends BaseBinanceApi {
* @throws BinanceServiceUnreachableException If the service is unreachable
* /
public Either < ServiceError , List < TickerPrice > > getTickerPrices ( ) throws BinanceServiceUnreachableException {
String url = String . format ( "%s%s" , BASE_ENDPOINT , TICKER_PRICE_URL ) ;
final HttpGet request = Helpers . getBuilder ( url , apiKey ) ;
final HttpGet request = buildGetRequestFromEndpoint ( TICKER_PRICE_URL , apiKey ) ;
try {
try ( CloseableHttpResponse closeableHttpResponse = closeableHttpClient . execute ( request ) ) {
@ -325,8 +317,9 @@ public class MarketData extends BaseBinanceApi {
HttpEntity httpEntity = closeableHttpResponse . getEntity ( ) ;
if ( httpEntity = = null ) {
log . error ( NO_RESPONSE_TEXT , url ) ;
throw new BinanceServiceUnreachableException ( String . format ( NO_RESPONSE_TEXT_FORMATTED , url ) , null ) ;
log . error ( NO_RESPONSE_TEXT , request . getURI ( ) . toASCIIString ( ) ) ;
throw new BinanceServiceUnreachableException (
String . format ( NO_RESPONSE_TEXT_FORMATTED , request . getURI ( ) . toASCIIString ( ) ) , null ) ;
}
String response = EntityUtils . toString ( httpEntity ) ;
@ -347,13 +340,13 @@ public class MarketData extends BaseBinanceApi {
* Gets candlestick data from the provided URL , allowing all of the getCandleStickData functions to act
* as glorified URL builders .
*
* @param url The URL to use
* @param endpoint The endpoint to use that has the associated parameters populated
* @return A list of candlesticks if successful , otherwise an ServiceError
* @throws BinanceServiceUnreachableException If the service is unreachable
* /
private Either < ServiceError , List < Candlestick > >
getCandleStickDataFromUrl ( String url ) throws BinanceServiceUnreachableException {
final HttpGet request = Helpers . getBuilder ( url , apiKey ) ;
getCandleStickDataFromUrl ( String endpoint ) throws BinanceServiceUnreachableException {
final HttpGet request = buildGetRequestFromEndpoint ( endpoint , apiKey ) ;
try {
try ( CloseableHttpResponse closeableHttpResponse = closeableHttpClient . execute ( request ) ) {
@ -362,8 +355,9 @@ public class MarketData extends BaseBinanceApi {
HttpEntity httpEntity = closeableHttpResponse . getEntity ( ) ;
if ( httpEntity = = null ) {
log . error ( NO_RESPONSE_TEXT , url ) ;
throw new BinanceServiceUnreachableException ( String . format ( NO_RESPONSE_TEXT_FORMATTED , url ) , null ) ;
log . error ( NO_RESPONSE_TEXT , request . getURI ( ) . toASCIIString ( ) ) ;
throw new BinanceServiceUnreachableException (
String . format ( NO_RESPONSE_TEXT_FORMATTED , request . getURI ( ) . toASCIIString ( ) ) , null ) ;
}
String response = EntityUtils . toString ( httpEntity ) ;