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