コード例 #1
0
        private async Task <T> HandleResponse <T>(HttpResponseMessage message, string fullCacheKey) where T : class
        {
            if (message.IsSuccessStatusCode)
            {
                var messageJson = await message.Content.ReadAsStringAsync();

                var messageObject = JsonConvert.DeserializeObject <T>(messageJson);
                _logger.Debug($"Successful Message Response={messageJson}");

                if (messageObject == null)
                {
                    throw new Exception("Unable to deserialize to provided type");
                }
                if (_apiCache.Contains(fullCacheKey))
                {
                    _apiCache.Remove(fullCacheKey);
                }
                _apiCache.Add(messageObject, fullCacheKey, _cacheTime);
                return(messageObject);
            }
            var errorJson = await message.Content.ReadAsStringAsync();

            var errorObject = JsonConvert.DeserializeObject <BinanceError>(errorJson);

            if (errorObject == null)
            {
                throw new BinanceException("Unexpected Error whilst handling the response", null);
            }
            _logger.Error($"Error Message Recevied", errorObject);
            throw CreateBinanceException(message.StatusCode, errorObject);
        }
コード例 #2
0
        private async Task <T> HandleResponse <T>(HttpResponseMessage message, string requestMessage, string fullCacheKey) where T : class
        {
            if (message.IsSuccessStatusCode)
            {
                var messageJson = await message.Content.ReadAsStringAsync();

                T messageObject = null;
                try
                {
                    messageObject = JsonConvert.DeserializeObject <T>(messageJson);
                }
                catch (Exception ex)
                {
                    string deserializeErrorMessage = $"Unable to deserialize message from: {requestMessage}. Exception: {ex.Message}";
                    _logger.Error(deserializeErrorMessage);
                    throw new BinanceException(deserializeErrorMessage, new BinanceError()
                    {
                        RequestMessage = requestMessage,
                        Message        = ex.Message
                    });
                }
                _logger.Debug($"Successful Message Response={messageJson}");

                if (messageObject == null)
                {
                    throw new Exception("Unable to deserialize to provided type");
                }
                if (_apiCache.Contains(fullCacheKey))
                {
                    _apiCache.Remove(fullCacheKey);
                }
                _apiCache.Add(messageObject, fullCacheKey, _cacheTime);
                return(messageObject);
            }
            var errorJson = await message.Content.ReadAsStringAsync();

            var errorObject = JsonConvert.DeserializeObject <BinanceError>(errorJson);

            if (errorObject == null)
            {
                throw new BinanceException("Unexpected Error whilst handling the response", null);
            }
            errorObject.RequestMessage = requestMessage;
            var exception = CreateBinanceException(message.StatusCode, errorObject);

            _logger.Error($"Error Message Received:{errorJson}", exception);
            throw exception;
        }
コード例 #3
0
        private async Task <T> HandleResponse <T>(HttpResponseMessage message, string requestMessage, string fullCacheKey) where T : class
        {
            if (message.IsSuccessStatusCode)
            {
                //try to catch the order limit
                foreach (var header in message.Headers)
                {
                    if (header.Key.Contains("x-mbx-order-count"))
                    {
                        lock (OrdersRates)
                            OrdersRates[header.Key] = string.Join(" ", header.Value);
                    }
                }

                //decode message
                var messageJson = await message.Content.ReadAsStringAsync();

                T messageObject = null;
                try
                {
                    messageObject = JsonConvert.DeserializeObject <T>(messageJson);
                }
                catch (Exception ex)
                {
                    string deserializeErrorMessage = $"Unable to deserialize message from: {requestMessage}. Exception: {ex.Message}";
                    _logger.Error(deserializeErrorMessage);
                    throw new BinanceException(deserializeErrorMessage, new BinanceError()
                    {
                        RequestMessage = requestMessage,
                        Message        = ex.Message
                    });
                }
                //_logger.Trace($"Successful Message Response={messageJson}");

                if (messageObject == null)
                {
                    throw new Exception("Unable to deserialize to provided type");
                }
                if (_cacheEnabled)
                {
                    if (_apiCache.Contains(fullCacheKey))
                    {
                        _apiCache.Remove(fullCacheKey);
                    }
                    _apiCache.Add(messageObject, fullCacheKey, _cacheTime);
                }
                return(messageObject);
            }
            var errorJson = await message.Content.ReadAsStringAsync();

            var errorObject = JsonConvert.DeserializeObject <BinanceError>(errorJson);

            if (errorObject == null)
            {
                throw new BinanceException("Unexpected Error whilst handling the response", null);
            }
            errorObject.RequestMessage = requestMessage;
            var exception = CreateBinanceException(message.StatusCode, errorObject);

            _logger.Error($"Error Message Received:{errorJson} - {errorObject}", exception);

            throw exception;
        }