
estonlabs.cxtl.exchanges.gateio.api.v4.GateIODeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cxtl Show documentation
Show all versions of cxtl Show documentation
CXTL – Crypto eXchange Trading Library
The newest version!
package estonlabs.cxtl.exchanges.gateio.api.v4;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import estonlabs.cxtl.common.stream.managed.AbstractInboundDeserializer;
import estonlabs.cxtl.common.stream.managed.InboundMessage;
import estonlabs.cxtl.exchanges.gateio.api.v4.domain.*;
import estonlabs.cxtl.exchanges.gateio.api.v4.domain.spot.*;
import lombok.NonNull;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class GateIODeserializer extends AbstractInboundDeserializer {
public GateIODeserializer() {
super(List.of(new GateIODeserializerTarget()));
}
@Override
protected GateIOInboundContainer wrap(InboundMessage message) {
return new GateIOInboundContainer(message);
}
static final class GateIODeserializerTarget implements Target {
private Class extends InboundMessage> matchedType;
private static final Map> byChannel = Map.ofEntries(
Map.entry("spot.book_ticker", WSTicker.class),
Map.entry("spot.order_book_update", OrderBook.class),
Map.entry("futures.book_ticker", WSTicker.class),
Map.entry("futures.order_book_update", OrderBook.class),
Map.entry("spot.orders", SpotWSOrder.class),
Map.entry("futures.orders", FuturesWSOrder.class),
Map.entry("spot.usertrades", SpotUserTrade.class),
Map.entry("futures.usertrades", FuturesUserTrade.class),
Map.entry("spot.balances", SpotBalanceUpdate.class),
Map.entry("futures.balances", FuturesBalanceUpdate.class),
Map.entry("futures.positions", FuturesPositionUpdate.class)
);
private static final Map> byEvent = Map.of(
"subscribe", SubscriptionConfirmation.class,
"unsubscribe", UnsubscriptionConfirmation.class
);
private boolean matchesAgainstMap(JsonNode root, String fieldName, Map> map){
var fieldVal = root.get(fieldName).asText();
if (map.containsKey(fieldVal)){
matchedType = map.get(fieldVal);
return true;
}
return false;
}
@Override
public boolean matches(JsonNode root, ObjectMapper mapper) {
var channel = root.get("channel").asText();
if (channel.contains("pong")){
matchedType = GateIOPong.class;
return true;
}
// The order is important!
return matchesAgainstMap(root, "event", byEvent) || matchesAgainstMap(root, "channel", byChannel);
}
@Override
public @NonNull Class extends InboundMessage> getType(JsonNode root, ObjectMapper mapper) throws IOException {
return matchedType;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy