All Downloads are FREE. Search and download functionalities are using the official Maven repository.

estonlabs.cxtl.exchanges.gateio.api.v4.GateIODeserializer Maven / Gradle / Ivy

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 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 getType(JsonNode root, ObjectMapper mapper) throws IOException {
            return matchedType;
        }
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy