Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 98 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,32 +133,112 @@ wsClient.close();
##### Listen for changes to the order boock for ETH-BTC and KCS-BTC

```java
kucoinPublicWSClient.onTicker(response -> {
System.out.println(response);
}, "ETH-BTC", "KCS-BTC");
kucoinPublicWSClient.onLevel2Data(response -> {
System.out.println(response);
}, "ETH-BTC", "KCS-BTC");
kucoinPublicWSClient.onMatchExecutionData(response -> {
System.out.println(response);
});
kucoinPublicWSClient.onLevel3Data(response -> {
System.out.println(response);
}, "ETH-BTC", "KCS-BTC");
kucoinPublicWSClient.onTicker(new KucoinAPICallback<KucoinEvent<TickerChangeEvent>>() {

@Override
public void onResponse(KucoinEvent<TickerChangeEvent> response) throws KucoinApiException {
System.out.println(response);
}

@Override
public void onFailure(Throwable cause) {
System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage());
//reinitializeWSConnection(); //implement this method
}

}, "ETH-BTC", "KCS-BTC");

kucoinPublicWSClient.onLevel2Data(new KucoinAPICallback<KucoinEvent<Level2ChangeEvent>>() {

@Override
public void onResponse(KucoinEvent<Level2ChangeEvent> response) throws KucoinApiException {
System.out.println(response);
}

@Override
public void onFailure(Throwable cause) {
System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage());
//reinitializeWSConnection(); //implement this method
}

}, "ETH-BTC", "KCS-BTC");


kucoinPublicWSClient.onMatchExecutionData(new KucoinAPICallback<KucoinEvent<MatchExcutionChangeEvent>>() {

@Override
public void onResponse(KucoinEvent<MatchExcutionChangeEvent> response) throws KucoinApiException {
System.out.println(response);
}

@Override
public void onFailure(Throwable cause) {
System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage());
//reinitializeWSConnection(); //implement this method
}

}, "ETH-BTC", "KCS-BTC");


kucoinPublicWSClient.onLevel3Data(new KucoinAPICallback<KucoinEvent<Level3ChangeEvent>>() {

@Override
public void onResponse(KucoinEvent<Level3ChangeEvent> response) throws KucoinApiException {
System.out.println(response);
}

@Override
public void onFailure(Throwable cause) {
System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage());
//reinitializeWSConnection(); //implement this method
}

}, "ETH-BTC", "KCS-BTC");

```

#### Private Channels

##### Listen for account balance changes

```java
kucoinPrivateWSClient.onAccountBalance(response -> {
System.out.println(response);
});

kucoinPrivateWSClient.onAccountBalance(new KucoinAPICallback<KucoinEvent<AccountChangeEvent>>() {

@Override
public void onResponse(KucoinEvent<AccountChangeEvent> response) throws KucoinApiException {
System.out.println(response);
}

@Override
public void onFailure(Throwable cause) {
System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage());
//reinitializeWSConnection(); //implement this method
}

});

```

##### Listen for order activate message

```java
kucoinPrivateWSClient.onOrderActivate(response -> {
System.out.println(response);
}, "ETH-BTC", "KCS-BTC");

kucoinPrivateWSClient.onOrderActivate(new KucoinAPICallback<KucoinEvent<OrderActivateEvent>>() {

@Override
public void onResponse(KucoinEvent<OrderActivateEvent> response) throws KucoinApiException {
System.out.println(response);
}

@Override
public void onFailure(Throwable cause) {
System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage());
//reinitializeWSConnection(); //implement this method
}

}, "ETH-BTC", "KCS-BTC");

```

## Testing
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/kucoin/sdk/KucoinClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public KucoinRestClient buildRestClient() {
if (timeAPI == null) timeAPI = new TimeAPIAdapter(baseUrl);
if (commonAPI == null) commonAPI = new CommonAPIAdapter(baseUrl);
if (symbolAPI == null) symbolAPI = new SymbolAPIAdaptor(baseUrl);
if (orderBookAPI == null) orderBookAPI = new OrderBookAPIAdapter(baseUrl);
if (orderBookAPI == null) orderBookAPI = new OrderBookAPIAdapter(baseUrl, apiKey, secret, passPhrase, apiKeyVersion);
if (historyAPI == null) historyAPI = new HistoryAPIAdapter(baseUrl);
return new KucoinRestClientImpl(this);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/kucoin/sdk/factory/HttpClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
package com.kucoin.sdk.factory;

import java.util.concurrent.TimeUnit;

import com.kucoin.sdk.rest.interceptor.AuthenticationInterceptor;
import okhttp3.Dispatcher;
import okhttp3.Interceptor;
Expand All @@ -26,7 +28,7 @@ private static OkHttpClient buildHttpClient(Interceptor interceptor) {
dispatcher.setMaxRequestsPerHost(100);
dispatcher.setMaxRequests(100);
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.dispatcher(dispatcher);
.dispatcher(dispatcher).pingInterval(30, TimeUnit.SECONDS);
if (interceptor != null) {
builder.addInterceptor(interceptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
package com.kucoin.sdk.rest.adapter;

import com.kucoin.sdk.rest.impl.retrofit.PublicRetrofitAPIImpl;
import com.kucoin.sdk.rest.impl.retrofit.AuthRetrofitAPIImpl;
import com.kucoin.sdk.rest.interfaces.OrderBookAPI;
import com.kucoin.sdk.rest.interfaces.retrofit.OrderBookAPIRetrofit;
import com.kucoin.sdk.rest.response.Level3Response;
Expand All @@ -14,10 +14,14 @@
/**
* Created by chenshiwei on 2019/1/22.
*/
public class OrderBookAPIAdapter extends PublicRetrofitAPIImpl<OrderBookAPIRetrofit> implements OrderBookAPI {
public class OrderBookAPIAdapter extends AuthRetrofitAPIImpl<OrderBookAPIRetrofit> implements OrderBookAPI {

public OrderBookAPIAdapter(String baseUrl) {
public OrderBookAPIAdapter(String baseUrl, String apiKey, String secret, String passPhrase, Integer apiKeyVersion) {
this.baseUrl = baseUrl;
this.apiKey = apiKey;
this.secret = secret;
this.passPhrase = passPhrase;
this.apiKeyVersion = apiKeyVersion;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface OrderBookAPIRetrofit {
@GET("api/v1/market/orderbook/level2_20")
Call<KucoinResponse<OrderBookResponse>> getTop20Level2OrderBook(@Query("symbol") String symbol);

@GET("api/v2/market/orderbook/level2")
@GET("api/v3/market/orderbook/level2")
Call<KucoinResponse<OrderBookResponse>> getFullLevel2OrderBook(@Query("symbol") String symbol);

@GET("api/v2/market/orderbook/level3")
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/kucoin/sdk/websocket/KucoinAPICallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
public interface KucoinAPICallback<T> {

void onResponse(T response) throws KucoinApiException;

void onFailure(Throwable cause);

}
5 changes: 5 additions & 0 deletions src/main/java/com/kucoin/sdk/websocket/PrintCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public void onResponse(T response) throws KucoinApiException {
LOGGER.debug("Got response: {}", response);
}

@Override
public void onFailure(Throwable cause) {
LOGGER.debug("Got exception: {}", cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ public void onMessage(WebSocket webSocket, String text) {
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
LOGGER.error("Error on private socket", t);

if( orderActivateCallback != null ) {
orderActivateCallback.onFailure(t);
}
if( accountChangeCallback != null ) {
accountChangeCallback.onFailure(t);
}
if( orderChangeCallback != null ) {
orderChangeCallback.onFailure(t);
}
if( advancedOrderCallback != null ) {
advancedOrderCallback.onFailure(t);
}
}

private JsonNode tree(String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,32 @@ public void onMessage(WebSocket webSocket, String text) {

@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
LOGGER.error("Error on private socket", t);
LOGGER.error("Error on public socket", t);

if( tickerCallback != null ) {
tickerCallback.onFailure(t);
}
if( level2Callback != null ) {
level2Callback.onFailure(t);
}
if( level2Depth5Callback != null ) {
level2Depth5Callback.onFailure(t);
}
if( level2Depth50Callback != null ) {
level2Depth50Callback.onFailure(t);
}
if( matchDataCallback != null ) {
matchDataCallback.onFailure(t);
}
if( level3Callback != null ) {
level3Callback.onFailure(t);
}
if( level3V2Callback != null ) {
level3V2Callback.onFailure(t);
}
if( snapshotCallback != null ) {
snapshotCallback.onFailure(t);
}
}

private JsonNode tree(String text) {
Expand Down
Loading