Skip to content

Commit

Permalink
[joaopsilva#26] UserDataStream new event.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel committed Aug 21, 2019
1 parent f51ee15 commit 976d2c8
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 12 deletions.
151 changes: 151 additions & 0 deletions src/main/java/com/binance/api/client/domain/event/ListStatusEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.binance.api.client.domain.event;

import com.binance.api.client.domain.account.Order;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ListStatusEvent {

@JsonProperty("e")
private String eventType;

@JsonProperty("E")
private long eventTime;

@JsonProperty("s")
private String symbol;

@JsonProperty("g")
private long orderListId;

@JsonProperty("c")
private String contingencyType;

@JsonProperty("l")
private String listStatusType;

@JsonProperty("L")
private String listOrderStatus;

@JsonProperty("r")
private String listRejectReason;

@JsonProperty("C")
private String listClientOrderId;

@JsonProperty("T")
private long transactionTime;

@JsonProperty("O")
@JsonDeserialize(contentUsing = OrderDeserializer.class)
private List<Order> orders;

public String getEventType() {
return eventType;
}

public void setEventType(String eventType) {
this.eventType = eventType;
}

public long getEventTime() {
return eventTime;
}

public void setEventTime(long eventTime) {
this.eventTime = eventTime;
}

public String getSymbol() {
return symbol;
}

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

public long getOrderListId() {
return orderListId;
}

public void setOrderListId(long orderListId) {
this.orderListId = orderListId;
}

public String getContingencyType() {
return contingencyType;
}

public void setContingencyType(String contingencyType) {
this.contingencyType = contingencyType;
}

public String getListStatusType() {
return listStatusType;
}

public void setListStatusType(String listStatusType) {
this.listStatusType = listStatusType;
}

public String getListOrderStatus() {
return listOrderStatus;
}

public void setListOrderStatus(String listOrderStatus) {
this.listOrderStatus = listOrderStatus;
}

public String getListRejectReason() {
return listRejectReason;
}

public void setListRejectReason(String listRejectReason) {
this.listRejectReason = listRejectReason;
}

public String getListClientOrderId() {
return listClientOrderId;
}

public void setListClientOrderId(String listClientOrderId) {
this.listClientOrderId = listClientOrderId;
}

public long getTransactionTime() {
return transactionTime;
}

public void setTransactionTime(long transactionTime) {
this.transactionTime = transactionTime;
}

public List<Order> getOrders() {
return orders;
}

public void setOrders(List<Order> orders) {
this.orders = orders;
}

@Override
public String toString() {
return "ListStatusEvent{" +
"eventType='" + eventType + '\'' +
", eventTime=" + eventTime +
", symbol='" + symbol + '\'' +
", orderListId=" + orderListId +
", contingencyType='" + contingencyType + '\'' +
", listStatusType='" + listStatusType + '\'' +
", listOrderStatus='" + listOrderStatus + '\'' +
", listRejectReason='" + listRejectReason + '\'' +
", listClientOrderId='" + listClientOrderId + '\'' +
", transactionTime=" + transactionTime +
", orders=" + orders +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.binance.api.client.domain.event;

import com.binance.api.client.domain.account.Order;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;

public class OrderDeserializer extends JsonDeserializer<Order> {

@Override
public Order deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
ObjectCodec oc = jp.getCodec();
JsonNode node = oc.readTree(jp);
final String symbol = node.get("s").asText();
final long orderId = node.get("i").asLong();
final String clientOrderId = node.get("c").asText();

Order order = new Order();
order.setSymbol(symbol);
order.setOrderId(orderId);
order.setClientOrderId(clientOrderId);
return order;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.binance.api.client.domain.event;

import com.binance.api.client.domain.account.AssetBalance;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class OutboundAccountPositionEvent {

@JsonProperty("e")
private String eventType;

@JsonProperty("E")
private long eventTime;

@JsonProperty("u")
private long lastAccountUpdate;

@JsonProperty("B")
@JsonDeserialize(contentUsing = AssetBalanceDeserializer.class)
private List<AssetBalance> balances;

public String getEventType() {
return eventType;
}

public void setEventType(String eventType) {
this.eventType = eventType;
}

public long getEventTime() {
return eventTime;
}

public void setEventTime(long eventTime) {
this.eventTime = eventTime;
}

public long getLastAccountUpdate() {
return lastAccountUpdate;
}

public void setLastAccountUpdate(long lastAccountUpdate) {
this.lastAccountUpdate = lastAccountUpdate;
}

public List<AssetBalance> getBalances() {
return balances;
}

public void setBalances(List<AssetBalance> balances) {
this.balances = balances;
}

@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
.append("eventType", eventType)
.append("eventTime", eventTime)
.append("lastAccountUpdate", lastAccountUpdate)
.append("balances", balances)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class UserDataUpdateEvent {

private OrderTradeUpdateEvent orderTradeUpdateEvent;

private OutboundAccountPositionEvent outboundAccountPositionEvent;

private ListStatusEvent listStatusEvent;

public UserDataUpdateEventType getEventType() {
return eventType;
}
Expand Down Expand Up @@ -55,22 +59,44 @@ public void setOrderTradeUpdateEvent(OrderTradeUpdateEvent orderTradeUpdateEvent
this.orderTradeUpdateEvent = orderTradeUpdateEvent;
}

public OutboundAccountPositionEvent getOutboundAccountPositionEvent() {
return outboundAccountPositionEvent;
}

public void setOutboundAccountPositionEvent(OutboundAccountPositionEvent outboundAccountPositionEvent) {
this.outboundAccountPositionEvent = outboundAccountPositionEvent;
}

public ListStatusEvent getListStatusEvent() {
return listStatusEvent;
}

public void setListStatusEvent(ListStatusEvent listStatusEvent) {
this.listStatusEvent = listStatusEvent;
}

@Override
public String toString() {
ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
.append("eventType", eventType)
.append("eventTime", eventTime);
.append("eventType", eventType)
.append("eventTime", eventTime);
if (eventType == UserDataUpdateEventType.ACCOUNT_UPDATE) {
sb.append("accountUpdateEvent", accountUpdateEvent);
} else {
} else if (eventType == UserDataUpdateEventType.ORDER_TRADE_UPDATE) {
sb.append("orderTradeUpdateEvent", orderTradeUpdateEvent);
} else if (eventType == UserDataUpdateEventType.OUTBOUND_ACCOUNT_POSITION) {
sb.append("outboundAccountPositionEvent", outboundAccountPositionEvent);
} else if (eventType == UserDataUpdateEventType.LIST_STATUS) {
sb.append("listStatusEvent", listStatusEvent);
}
return sb.toString();
}

public enum UserDataUpdateEventType {
ACCOUNT_UPDATE("outboundAccountInfo"),
ORDER_TRADE_UPDATE("executionReport");
ORDER_TRADE_UPDATE("executionReport"),
OUTBOUND_ACCOUNT_POSITION("outboundAccountPosition"),
LIST_STATUS("listStatus");

private final String eventTypeId;

Expand All @@ -87,7 +113,12 @@ public static UserDataUpdateEventType fromEventTypeId(String eventTypeId) {
return ACCOUNT_UPDATE;
} else if (ORDER_TRADE_UPDATE.eventTypeId.equals(eventTypeId)) {
return ORDER_TRADE_UPDATE;
} else if (OUTBOUND_ACCOUNT_POSITION.eventTypeId.equals(eventTypeId)) {
return OUTBOUND_ACCOUNT_POSITION;
} else if (LIST_STATUS.eventTypeId.equals(eventTypeId)) {
return LIST_STATUS;
}

throw new IllegalArgumentException("Unrecognized user data update event type id: " + eventTypeId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,36 @@ public UserDataUpdateEvent deserialize(JsonParser jp, DeserializationContext ctx
String json = node.toString();

final String eventTypeId = node.get("e").asText();
final Long eventTime = node.get("E").asLong();
final long eventTime = node.get("E").asLong();
UserDataUpdateEventType userDataUpdateEventType = UserDataUpdateEventType.fromEventTypeId(eventTypeId);

UserDataUpdateEvent userDataUpdateEvent = new UserDataUpdateEvent();
userDataUpdateEvent.setEventType(userDataUpdateEventType);
userDataUpdateEvent.setEventTime(eventTime);

if (userDataUpdateEventType == UserDataUpdateEventType.ACCOUNT_UPDATE) {
AccountUpdateEvent accountUpdateEvent = getUserDataUpdateEventDetail(json, AccountUpdateEvent.class);
userDataUpdateEvent.setAccountUpdateEvent(accountUpdateEvent);
} else { // userDataUpdateEventType == UserDataUpdateEventType.ORDER_TRADE_UPDATE
OrderTradeUpdateEvent orderTradeUpdateEvent = getUserDataUpdateEventDetail(json, OrderTradeUpdateEvent.class);
userDataUpdateEvent.setOrderTradeUpdateEvent(orderTradeUpdateEvent);
switch (userDataUpdateEventType) {
case ACCOUNT_UPDATE:
AccountUpdateEvent accountUpdateEvent = getUserDataUpdateEventDetail(json, AccountUpdateEvent.class);
userDataUpdateEvent.setAccountUpdateEvent(accountUpdateEvent);
break;
case ORDER_TRADE_UPDATE:
OrderTradeUpdateEvent orderTradeUpdateEvent = getUserDataUpdateEventDetail(json, OrderTradeUpdateEvent.class);
userDataUpdateEvent.setOrderTradeUpdateEvent(orderTradeUpdateEvent);
break;
case OUTBOUND_ACCOUNT_POSITION:
OutboundAccountPositionEvent outboundAccountPositionEvent = getUserDataUpdateEventDetail(json, OutboundAccountPositionEvent.class);
userDataUpdateEvent.setOutboundAccountPositionEvent(outboundAccountPositionEvent);
break;
case LIST_STATUS:
ListStatusEvent listStatusEvent = getUserDataUpdateEventDetail(json, ListStatusEvent.class);
userDataUpdateEvent.setListStatusEvent(listStatusEvent);
break;
}

return userDataUpdateEvent;
}

public <T> T getUserDataUpdateEventDetail(String json, Class<T> clazz) {
private <T> T getUserDataUpdateEventDetail(String json, Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, clazz);
Expand Down

0 comments on commit 976d2c8

Please sign in to comment.