示例#1
0
        private void ApiReader_OnSuccessfulRead(CryptonatorResult result)
        {
            string key = result.Ticker;

            if (this.gagues.ContainsKey(key) == false)
            {
                throw new KeyNotFoundException($"Can not find ticker '{key}; in gagues");
            }

            this.gagues[key].Set(result.Price);
        }
        public static IList <string> Validate(this CryptonatorResult result)
        {
            List <string> errors = new List <string>();

            if (string.IsNullOrWhiteSpace(result.BaseCurrency))
            {
                errors.Add($"{nameof( result.BaseCurrency )} null, empty, or whitespace");
            }
            else if (string.IsNullOrWhiteSpace(result.TargetCurrency))
            {
                errors.Add($"{nameof( result.TargetCurrency )} null, empty, or whitespace");
            }
            else if (result.Price < 0)
            {
                errors.Add($"{nameof( result.Price )} negative");
            }

            return(errors);
        }
示例#3
0
        private void QueryTicker(string ticker)
        {
            string url = $"https://api.cryptonator.com/api/ticker/{ticker}";
            Task <HttpResponseMessage> responseToken = this.client.GetAsync(
                url
                );

            responseToken.Wait();

            HttpResponseMessage response = responseToken.Result;
            string str = response.Content.ReadAsStringAsync().Result;

            if (response.IsSuccessStatusCode == false)
            {
                throw new HttpRequestException(
                          $"Got response code {response.StatusCode} from {url}." + Environment.NewLine + str
                          );
            }

            CryptonatorResult result = new CryptonatorResult();

            result.FromJson(str);
            if (result.Success == false)
            {
                if (notFoundRegex.IsMatch(str))
                {
                    // If a ticker is not found, just remove it.
                    this.tickers.Remove(ticker);
                    throw new ArgumentException(
                              $"Invalid ticker pair, removing from future queries: {ticker}",
                              nameof(settings.Tickers)
                              );
                }
                else
                {
                    throw new ApplicationException(
                              $"Error when trying to parse {nameof( CryptonatorResult )}:" + Environment.NewLine + result.Error
                              );
                }
            }

            this.OnSuccessfulRead?.Invoke(result);
        }
        public static void FromJson(this CryptonatorResult result, string jsonString)
        {
            // Example success JSON:
            // {
            //     "ticker":
            //      {
            //          "base":"DOGE"
            //          "target":"USD",
            //          "price":"0.02788101",
            //          "volume":"3604478983.83879995",
            //          "change":"-0.00297482"
            //      },
            //      "timestamp":1612025582,
            //      "success":true,
            //      "error":""
            // }
            //
            // Example failure JSON:
            // {
            //     "success":false,
            //     "error":"Pair not found"
            // }

            JObject json = JObject.Parse(jsonString);

            bool   success;
            JValue successToken = (JValue)json["success"];

            if (bool.TryParse(successToken.Value.ToString(), out success) == false)
            {
                throw new ApplicationException(
                          "Could not parse 'success' from response: " + jsonString
                          );
            }

            result.Success = success;
            if (result.Success == false)
            {
                JValue errorToken = (JValue)json["error"];
                result.Error = errorToken.Value.ToString();
            }
            else
            {
                JObject tickerObject = (JObject)json["ticker"];
                foreach (JProperty property in tickerObject.Properties())
                {
                    if ("base".EqualsIgnoreCase(property.Name))
                    {
                        result.BaseCurrency = property.Value.ToString();
                    }
                    else if ("target".EqualsIgnoreCase(property.Name))
                    {
                        result.TargetCurrency = property.Value.ToString();
                    }
                    else if ("price".EqualsIgnoreCase(property.Name))
                    {
                        if (double.TryParse(property.Value.ToString(), out double price))
                        {
                            result.Price = price;
                        }
                        else
                        {
                            throw new ApplicationException(
                                      "Could not parse 'price' from response: " + jsonString
                                      );
                        }
                    }
                }

                IList <string> errors = result.Validate();
                if (errors.IsEmpty() == false)
                {
                    throw new ListedValidationException(
                              $"Errors when parsing response: {jsonString}",
                              errors
                              );
                }
            }
        }