Example #1
0
        private bool HandleObjectMessage(string msg)
        {
            var response = BitmexJsonSerializer.Deserialize <JObject>(msg);

            // ********************
            // ADD OBJECT HANDLERS BELOW
            // ********************

            return

                (TradeResponse.TryHandle(response, Streams.TradesSubject) ||
                 TradeBinResponse.TryHandle(response, Streams.TradeBinSubject) ||
                 BookResponse.TryHandle(response, Streams.BookSubject) ||
                 QuoteResponse.TryHandle(response, Streams.QuoteSubject) ||
                 LiquidationResponse.TryHandle(response, Streams.LiquidationSubject) ||
                 PositionResponse.TryHandle(response, Streams.PositionSubject) ||
                 OrderResponse.TryHandle(response, Streams.OrderSubject) ||
                 WalletResponse.TryHandle(response, Streams.WalletSubject) ||


                 ErrorResponse.TryHandle(response, Streams.ErrorSubject) ||
                 SubscribeResponse.TryHandle(response, Streams.SubscribeSubject) ||
                 InfoResponse.TryHandle(response, Streams.InfoSubject) ||
                 AuthenticationResponse.TryHandle(response, Streams.AuthenticationSubject));
        }
        public async Task Send(BitmexWebsocketChannel toChannel, MultiplexingMessageBase multiplexingMessage)
        {
            var message = new List <object>
            {
                (int)multiplexingMessage.MessageType,
                toChannel.ChannelId,
                toChannel.ChannelName
            };

            if (multiplexingMessage.Payload != null)
            {
                message.Add(multiplexingMessage.Payload);
            }

            var serialized = BitmexJsonSerializer.Serialize(message);

            try
            {
                await _communicator.Send(serialized).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Log.Error(e, L($"Exception while sending message '{serialized}'. Error: {e.Message}"));
                throw;
            }
        }
        /// <summary>
        /// Handles object messages sent by bitmex.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <param name="streams">The streams.</param>
        /// <returns></returns>
        public static bool HandleObjectMessage(string msg, BitmexClientStreams streams)
        {
            var response = BitmexJsonSerializer.Deserialize <JObject>(msg);

            // ********************
            // ADD OBJECT HANDLERS BELOW
            // ********************

            return
                (TradeResponse.TryHandle(response, streams.TradesSubject) ||
                 TradeBinResponse.TryHandle(response, streams.TradeBinSubject) ||
                 BookResponse.TryHandle(response, streams.BookSubject) ||
                 QuoteResponse.TryHandle(response, streams.QuoteSubject) ||
                 LiquidationResponse.TryHandle(response, streams.LiquidationSubject) ||
                 PositionResponse.TryHandle(response, streams.PositionSubject) ||
                 MarginResponse.TryHandle(response, streams.MarginSubject) ||
                 OrderResponse.TryHandle(response, streams.OrderSubject) ||
                 WalletResponse.TryHandle(response, streams.WalletSubject) ||
                 InstrumentResponse.TryHandle(response, streams.InstrumentSubject) ||
                 ExecutionResponse.TryHandle(response, streams.ExecutionSubject) ||
                 FundingResponse.TryHandle(response, streams.FundingsSubject) ||


                 ErrorResponse.TryHandle(response, streams.ErrorSubject) ||
                 SubscribeResponse.TryHandle(response, streams.SubscribeSubject) ||
                 InfoResponse.TryHandle(response, streams.InfoSubject) ||
                 AuthenticationResponse.TryHandle(response, streams.AuthenticationSubject));
        }
        private void HandleMessage(ResponseMessage message)
        {
            try
            {
                var messageSafe = (message.Text ?? string.Empty).Trim();

                // index 0 = MessageType
                // index 1 = ChannelId
                // index 2 = ChannelName
                // index 3 = Payload (nullable)
                var deserialized = BitmexJsonSerializer.Deserialize <List <object> >(messageSafe);
                if (deserialized.Count == 4 &&
                    Guid.TryParse(deserialized[1].ToString(), out Guid channelGuid) &&
                    _channels.TryGetValue(channelGuid, out BitmexWebsocketChannel channel))
                {
                    var payload = deserialized[3].ToString();
                    BitmexResponseHandler.HandleObjectMessage(payload, channel.Streams);
                    return;
                }
                Debug.WriteLine($"Unhandled response:  '{messageSafe}'");
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while receiving message");
            }
        }
        public void String_Enum_CamelCase_Test()
        {
            var str = "{\"myEnum\":\"testValue\"}";

            var cls = BitmexJsonSerializer.Deserialize <MyClass>(str);

            cls.MyEnum.Should().Be(MyEnum.TestValue);
        }
Example #6
0
        public Task Send <T>(T request) where T : RequestBase
        {
            BmxValidations.ValidateInput(request, nameof(request));

            var serialized = request.IsRaw ?
                             request.OperationString :
                             BitmexJsonSerializer.Serialize(request);

            return(_communicator.Send(serialized));
        }
 internal static bool TryParse(JsonElement response, Action <UnsubscribeResponse> action)
 {
     if (response.TryGetProperty("unsubscribe", out _))
     {
         var unsubscribeResponse = BitmexJsonSerializer.Deserialize <UnsubscribeResponse>(response);
         action?.Invoke(unsubscribeResponse);
         return(true);
     }
     return(false);
 }
 internal static bool TryParse(JsonElement response, Action <WelcomeInfoResponse> action)
 {
     if (response.TryGetProperty("info", out _) && response.TryGetProperty("version", out _))
     {
         var welcomeInfoResponse = BitmexJsonSerializer.Deserialize <WelcomeInfoResponse>(response);
         action?.Invoke(welcomeInfoResponse);
         return(true);
     }
     return(false);
 }
        public void Enum_String_CamelCase_Test()
        {
            var c = new MyClass()
            {
                MyEnum = MyEnum.TestValue
            };

            var str = BitmexJsonSerializer.Serialize(c);

            str.Should().Be("{\"myEnum\":\"testValue\"}");
        }
Example #10
0
 internal static bool TryParse(JsonElement response, Action <AuthenticationResponse> action)
 {
     if (response.TryGetProperty("request", out var request) &&
         request.TryGetProperty("op", out var op) &&
         op.GetString() == "authKeyExpires")
     {
         var authenticationResponse = BitmexJsonSerializer.Deserialize <AuthenticationResponse>(response);
         action?.Invoke(authenticationResponse);
         return(true);
     }
     return(false);
 }
Example #11
0
        internal static bool TryHandle(string response, ISubject <InfoResponse> subject)
        {
            if (!BitmexJsonSerializer.ContainsProperty(response, "info"))
            {
                return(false);
            }

            var parsed = BitmexJsonSerializer.Deserialize <InfoResponse>(response);

            subject.OnNext(parsed);
            return(true);
        }
 internal static bool TryHandle(JsonElement response, Action <CancelAllAfterResponse> action)
 {
     if (response.TryGetProperty("request", out var request) &&
         request.TryGetProperty("op", out var op) &&
         op.GetString() == "cancelAllAfter")
     {
         var cancelAllAfterResponse = BitmexJsonSerializer.Deserialize <CancelAllAfterResponse>(response);
         action?.Invoke(cancelAllAfterResponse);
         return(true);
     }
     return(false);
 }
Example #13
0
 public static bool TryHandle(JsonElement response, Action <TableResponse <T> > action, SubscriptionType subscriptionType)
 {
     if (response.TryGetProperty("table", out var table) &&
         Enum.TryParse(table.GetString(), true, result: out SubscriptionType subscription) &&
         subscription == subscriptionType)
     {
         var tableResponse = BitmexJsonSerializer.Deserialize <TableResponse <T> >(response);
         action?.Invoke(tableResponse);
         return(true);
     }
     return(false);
 }
Example #14
0
        internal static bool TryHandle(string response, ISubject <AuthenticationResponse> subject)
        {
            if (!BitmexJsonSerializer.ContainsValue(response, "authKey"))
            {
                return(false);
            }

            var parsed = BitmexJsonSerializer.Deserialize <AuthenticationResponse>(response);

            subject.OnNext(parsed);
            return(true);
        }
        internal static bool TryHandle(string response, ISubject <BookResponse> subject)
        {
            if (!BitmexJsonSerializer.ContainsValue(response, "orderBookL2"))
            {
                return(false);
            }

            var parsed = BitmexJsonSerializer.Deserialize <BookResponse>(response);

            subject.OnNext(parsed);

            return(true);
        }
Example #16
0
        internal static bool TryHandle(string response, ISubject <TradeBinResponse> subject)
        {
            if (!BitmexJsonSerializer.ContainsRaw(response, "tradeBin"))
            {
                return(false);
            }

            var parsed = BitmexJsonSerializer.Deserialize <TradeBinResponse>(response);

            parsed.Size = parsed.Table?.Replace("tradeBin", string.Empty).Trim();
            subject.OnNext(parsed);

            return(true);
        }
Example #17
0
        /// <summary>
        /// Serializes request and sends message via websocket communicator.
        /// It logs and re-throws every exception.
        /// </summary>
        /// <param name="request">Request/message to be sent</param>
        public async Task Send <T>(T request) where T : RequestBase
        {
            try
            {
                BmxValidations.ValidateInput(request, nameof(request));

                var serialized = request.IsRaw ?
                                 request.OperationString :
                                 BitmexJsonSerializer.Serialize(request);
                await _communicator.Send(serialized).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Log.Error(e, L($"Exception while sending message '{request}'. Error: {e.Message}"));
                throw;
            }
        }
        public override string ToQueryString()
        {
            var baseQuery    = base.ToQueryString();
            var filterValues = Filter == null
                ? string.Empty
                : $"filter={HttpUtility.UrlEncode(BitmexJsonSerializer.Serialize(Filter))}";
            var currentQuery = $"{string.Join("&", filterValues)}";

            if (string.IsNullOrWhiteSpace(baseQuery))
            {
                return(currentQuery);
            }
            if (string.IsNullOrWhiteSpace(currentQuery))
            {
                return(baseQuery);
            }
            if (!string.IsNullOrWhiteSpace(currentQuery) && !string.IsNullOrWhiteSpace(baseQuery))
            {
                return(baseQuery + "&" + currentQuery);
            }
            return(string.Empty);
        }
Example #19
0
 public override string GetContent()
 {
     return(BitmexJsonSerializer.Serialize(this, GetType()));
 }
Example #20
0
 public string ToJson()
 {
     return(BitmexJsonSerializer.Serialize(this, GetType()));
 }
 public SubscriptionRequest(SubscriptionType subscriptionType, string symbol)
     : base(OperationType.subscribe, $"{BitmexJsonSerializer.Serialize(subscriptionType).Trim('"')}:{symbol}")
 {
     SubscriptionType = subscriptionType;
     Symbol = symbol;
 }