Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopsilva authored Sep 13, 2018
2 parents 9a6cd16 + 8f2bff1 commit 15489f0
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 61 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.0.0</version>

<properties>
<com.squareup.retrofit2.version>2.3.0</com.squareup.retrofit2.version>
<com.squareup.retrofit2.version>2.4.0</com.squareup.retrofit2.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.binance.api.client.domain.account.WithdrawResult;
import com.binance.api.client.domain.account.request.AllOrdersRequest;
import com.binance.api.client.domain.account.request.CancelOrderRequest;
import com.binance.api.client.domain.account.request.CancelOrderResponse;
import com.binance.api.client.domain.account.request.OrderRequest;
import com.binance.api.client.domain.account.request.OrderStatusRequest;
import com.binance.api.client.domain.event.ListenKey;
Expand Down Expand Up @@ -197,7 +198,7 @@ public interface BinanceApiAsyncRestClient {
* @param cancelOrderRequest order status request parameters
* @param callback the callback that handles the response
*/
void cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback<Void> callback);
void cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback<CancelOrderResponse> callback);

/**
* Get all open orders on a symbol (asynchronous).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.binance.api.client.impl.BinanceApiRestClientImpl;
import com.binance.api.client.impl.BinanceApiWebSocketClientImpl;

import static com.binance.api.client.impl.BinanceApiServiceGenerator.getSharedClient;

/**
* A factory for creating BinanceApi client objects.
*/
Expand Down Expand Up @@ -68,6 +70,6 @@ public BinanceApiRestClient newRestClient() {
* Creates a new web socket client used for handling data streams.
*/
public BinanceApiWebSocketClient newWebSocketClient() {
return new BinanceApiWebSocketClientImpl();
return new BinanceApiWebSocketClientImpl(getSharedClient());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.binance.api.client.domain.account.WithdrawResult;
import com.binance.api.client.domain.account.request.AllOrdersRequest;
import com.binance.api.client.domain.account.request.CancelOrderRequest;
import com.binance.api.client.domain.account.request.CancelOrderResponse;
import com.binance.api.client.domain.account.request.OrderRequest;
import com.binance.api.client.domain.account.request.OrderStatusRequest;
import com.binance.api.client.domain.general.ExchangeInfo;
Expand Down Expand Up @@ -183,7 +184,7 @@ public interface BinanceApiRestClient {
*
* @param cancelOrderRequest order status request parameters
*/
void cancelOrder(CancelOrderRequest cancelOrderRequest);
CancelOrderResponse cancelOrder(CancelOrderRequest cancelOrderRequest);

/**
* Get all open orders on a symbol.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,9 @@ public interface BinanceApiWebSocketClient extends Closeable {
*/
Closeable onAllMarketTickersEvent(BinanceApiCallback<List<AllMarketTickersEvent>> callback);

/**
* @deprecated This method is no longer functional. Please use the returned {@link Closeable} from any of the other methods to close the web socket.
*/
@Deprecated
void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public enum OrderRejectReason {
UNKNOWN_ACCOUNT,
INSUFFICIENT_BALANCE,
ACCOUNT_INACTIVE,
ACCOUNT_CANNOT_SETTLE
ACCOUNT_CANNOT_SETTLE,
ORDER_WOULD_TRIGGER_IMMEDIATELY
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.binance.api.client.domain.account.request;

import com.binance.api.client.constant.BinanceApiConstants;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
* Response object returned when an order is canceled.
*
* @see CancelOrderRequest for the request
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class CancelOrderResponse {

private String symbol;

private String origClientOrderId;

private String orderId;

private String clientOrderId;

public String getSymbol() {
return symbol;
}

public CancelOrderResponse setSymbol(String symbol) {
this.symbol = symbol;
return this;
}

public String getOrigClientOrderId() {
return origClientOrderId;
}

public CancelOrderResponse setOrigClientOrderId(String origClientOrderId) {
this.origClientOrderId = origClientOrderId;
return this;
}

public String getOrderId() {
return orderId;
}

public CancelOrderResponse setOrderId(String orderId) {
this.orderId = orderId;
return this;
}

public String getClientOrderId() {
return clientOrderId;
}

public CancelOrderResponse setClientOrderId(String clientOrderId) {
this.clientOrderId = clientOrderId;
return this;
}

@Override
public String toString() {
return new ToStringBuilder(this, BinanceApiConstants.TO_STRING_BUILDER_STYLE)
.append("symbol", symbol)
.append("origClientOrderId", origClientOrderId)
.append("orderId", orderId)
.append("clientOrderId", clientOrderId)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.binance.api.client.domain.account.WithdrawResult;
import com.binance.api.client.domain.account.request.AllOrdersRequest;
import com.binance.api.client.domain.account.request.CancelOrderRequest;
import com.binance.api.client.domain.account.request.CancelOrderResponse;
import com.binance.api.client.domain.account.request.OrderRequest;
import com.binance.api.client.domain.account.request.OrderStatusRequest;
import com.binance.api.client.domain.event.ListenKey;
Expand Down Expand Up @@ -153,7 +154,7 @@ public void getOrderStatus(OrderStatusRequest orderStatusRequest, BinanceApiCall
}

@Override
public void cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback<Void> callback) {
public void cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback<CancelOrderResponse> callback) {
binanceApiService.cancelOrder(cancelOrderRequest.getSymbol(),
cancelOrderRequest.getOrderId(), cancelOrderRequest.getOrigClientOrderId(), cancelOrderRequest.getNewClientOrderId(),
cancelOrderRequest.getRecvWindow(), cancelOrderRequest.getTimestamp()).enqueue(new BinanceApiCallbackAdapter<>(callback));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.binance.api.client.domain.account.WithdrawResult;
import com.binance.api.client.domain.account.request.AllOrdersRequest;
import com.binance.api.client.domain.account.request.CancelOrderRequest;
import com.binance.api.client.domain.account.request.CancelOrderResponse;
import com.binance.api.client.domain.account.request.OrderRequest;
import com.binance.api.client.domain.account.request.OrderStatusRequest;
import com.binance.api.client.domain.general.Asset;
Expand Down Expand Up @@ -150,8 +151,8 @@ public Order getOrderStatus(OrderStatusRequest orderStatusRequest) {
}

@Override
public void cancelOrder(CancelOrderRequest cancelOrderRequest) {
executeSync(binanceApiService.cancelOrder(cancelOrderRequest.getSymbol(),
public CancelOrderResponse cancelOrder(CancelOrderRequest cancelOrderRequest) {
return executeSync(binanceApiService.cancelOrder(cancelOrderRequest.getSymbol(),
cancelOrderRequest.getOrderId(), cancelOrderRequest.getOrigClientOrderId(), cancelOrderRequest.getNewClientOrderId(),
cancelOrderRequest.getRecvWindow(), cancelOrderRequest.getTimestamp()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.binance.api.client.domain.account.TradeHistoryItem;
import com.binance.api.client.domain.account.WithdrawHistory;
import com.binance.api.client.domain.account.WithdrawResult;
import com.binance.api.client.domain.account.request.CancelOrderResponse;
import com.binance.api.client.domain.event.ListenKey;
import com.binance.api.client.domain.general.Asset;
import com.binance.api.client.domain.general.ExchangeInfo;
Expand Down Expand Up @@ -115,9 +116,9 @@ Call<Order> getOrderStatus(@Query("symbol") String symbol, @Query("orderId") Lon

@Headers(BinanceApiConstants.ENDPOINT_SECURITY_TYPE_SIGNED_HEADER)
@DELETE("/api/v3/order")
Call<Void> cancelOrder(@Query("symbol") String symbol, @Query("orderId") Long orderId,
@Query("origClientOrderId") String origClientOrderId, @Query("newClientOrderId") String newClientOrderId,
@Query("recvWindow") Long recvWindow, @Query("timestamp") Long timestamp);
Call<CancelOrderResponse> cancelOrder(@Query("symbol") String symbol, @Query("orderId") Long orderId,
@Query("origClientOrderId") String origClientOrderId, @Query("newClientOrderId") String newClientOrderId,
@Query("recvWindow") Long recvWindow, @Query("timestamp") Long timestamp);

@Headers(BinanceApiConstants.ENDPOINT_SECURITY_TYPE_SIGNED_HEADER)
@GET("/api/v3/openOrders")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,53 @@
import com.binance.api.client.exception.BinanceApiException;
import com.binance.api.client.security.AuthenticationInterceptor;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.apache.commons.lang3.StringUtils;
import retrofit2.Call;
import retrofit2.Converter;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.concurrent.TimeUnit;

/**
* Generates a Binance API implementation based on @see {@link BinanceApiService}.
*/
public class BinanceApiServiceGenerator {
private static final OkHttpClient sharedClient = new OkHttpClient.Builder()
.pingInterval(20, TimeUnit.SECONDS)
.build();

static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static final Converter.Factory converterFactory = JacksonConverterFactory.create();

private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(BinanceApiConstants.API_BASE_URL)
.addConverterFactory(JacksonConverterFactory.create());

private static Retrofit retrofit = builder.build();
@SuppressWarnings("unchecked")
private static final Converter<ResponseBody, BinanceApiError> errorBodyConverter =
(Converter<ResponseBody, BinanceApiError>)converterFactory.responseBodyConverter(
BinanceApiError.class, new Annotation[0], null);

public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null, null);
}

public static <S> S createService(Class<S> serviceClass, String apiKey, String secret) {
if (!StringUtils.isEmpty(apiKey) && !StringUtils.isEmpty(secret)) {
Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
.baseUrl(BinanceApiConstants.API_BASE_URL)
.addConverterFactory(converterFactory);

if (StringUtils.isEmpty(apiKey) || StringUtils.isEmpty(secret)) {
retrofitBuilder.client(sharedClient);
} else {
// `adaptedClient` will use its own interceptor, but share thread pool etc with the 'parent' client
AuthenticationInterceptor interceptor = new AuthenticationInterceptor(apiKey, secret);
if (!httpClient.interceptors().contains(interceptor)) {
httpClient.addInterceptor(interceptor);
builder.client(httpClient.build());
retrofit = builder.build();
}
OkHttpClient adaptedClient = sharedClient.newBuilder().addInterceptor(interceptor).build();
retrofitBuilder.client(adaptedClient);
}

Retrofit retrofit = retrofitBuilder.build();
return retrofit.create(serviceClass);
}

Expand All @@ -65,7 +76,13 @@ public static <T> T executeSync(Call<T> call) {
* Extracts and converts the response error body into an object.
*/
public static BinanceApiError getBinanceApiError(Response<?> response) throws IOException, BinanceApiException {
return (BinanceApiError)retrofit.responseBodyConverter(BinanceApiError.class, new Annotation[0])
.convert(response.errorBody());
return errorBodyConverter.convert(response.errorBody());
}

/**
* Returns the shared OkHttpClient instance.
*/
public static OkHttpClient getSharedClient() {
return sharedClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.binance.api.client.domain.event.UserDataUpdateEvent;
import com.binance.api.client.domain.market.CandlestickInterval;
import com.fasterxml.jackson.core.type.TypeReference;
import okhttp3.Dispatcher;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;
Expand All @@ -23,12 +23,10 @@
*/
public class BinanceApiWebSocketClientImpl implements BinanceApiWebSocketClient, Closeable {

private OkHttpClient client;
private final OkHttpClient client;

public BinanceApiWebSocketClientImpl() {
Dispatcher d = new Dispatcher();
d.setMaxRequestsPerHost(100);
this.client = new OkHttpClient.Builder().dispatcher(d).build();
public BinanceApiWebSocketClientImpl(OkHttpClient client) {
this.client = client;
}

public Closeable onDepthEvent(String symbol, BinanceApiCallback<DepthEvent> callback) {
Expand Down Expand Up @@ -56,10 +54,11 @@ public Closeable onAllMarketTickersEvent(BinanceApiCallback<List<AllMarketTicker
return createNewWebSocket(channel, new BinanceApiWebSocketListener<>(callback, new TypeReference<List<AllMarketTickersEvent>>() {}));
}

/**
* @deprecated This method is no longer functional. Please use the returned {@link Closeable} from any of the other methods to close the web socket.
*/
@Override
public void close() {
client.dispatcher().executorService().shutdown();
}
public void close() { }

private Closeable createNewWebSocket(String channel, BinanceApiWebSocketListener<?> listener) {
String streamingUrl = String.format("%s/%s", BinanceApiConstants.WS_API_BASE_URL, channel);
Expand Down

This file was deleted.

4 changes: 3 additions & 1 deletion src/test/java/com/binance/api/examples/OrdersExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.binance.api.client.domain.account.Order;
import com.binance.api.client.domain.account.request.AllOrdersRequest;
import com.binance.api.client.domain.account.request.CancelOrderRequest;
import com.binance.api.client.domain.account.request.CancelOrderResponse;
import com.binance.api.client.domain.account.request.OrderRequest;
import com.binance.api.client.domain.account.request.OrderStatusRequest;
import com.binance.api.client.exception.BinanceApiException;
Expand Down Expand Up @@ -40,7 +41,8 @@ public static void main(String[] args) {

// Canceling an order
try {
client.cancelOrder(new CancelOrderRequest("LINKETH", 756762l));
CancelOrderResponse cancelOrderResponse = client.cancelOrder(new CancelOrderRequest("LINKETH", 756762l));
System.out.println(cancelOrderResponse);
} catch (BinanceApiException e) {
System.out.println(e.getError().getMsg());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void main(String[] args) {

// Canceling an order
client.cancelOrder(new CancelOrderRequest("LINKETH", 756703L),
response -> System.out.println("Order has been canceled."));
response -> System.out.println(response));

// Placing a test LIMIT order
client.newOrderTest(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001"),
Expand Down

0 comments on commit 15489f0

Please sign in to comment.