public async Task <IBankResult> GetRateAsync(string currency, DateTime date)
        {
            IBankResult bankResult = new BankResult()
            {
                ExchangeRate = -1m,
                RateDate     = date,
                HomeCurrency = homeCurrency,
                BankName     = bankName
            };
            //https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D.USD.EUR.SP00.A?startPeriod=2019-11-01&endPeriod=2019-11-04
            string startDate = date.ToString("yyyy-MM-dd");
            Uri    methodUri = new Uri($"{baseUrl}/{resource}/{flowRef}/D.{currency}.EUR.SP00.A?startPeriod={startDate}&endPeriod={startDate}");

            using (WebClient client = new WebClient())
            {
                try
                {
                    decimal result = 0;
                    string  xml    = await client.DownloadStringTaskAsync(methodUri);

                    if (!string.IsNullOrEmpty(xml))
                    {
                        XmlDocument xmlDocument = new XmlDocument();
                        xmlDocument.LoadXml(xml);

                        XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
                        namespaceManager.AddNamespace("message", "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message");
                        namespaceManager.AddNamespace("generic", "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic");
                        namespaceManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                        namespaceManager.AddNamespace("common", "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common");

                        string xmlPath = $"/message:GenericData/message:DataSet/generic:Series/generic:Obs/generic:ObsValue";
                        string rate    = xmlDocument.SelectSingleNode(xmlPath, namespaceManager).Attributes[0].Value;

                        decimal.TryParse(rate.ToString(), out result);
                        bankResult.ExchangeRate = result;
                    }
                    else
                    {
                        return(await GetRateAsync(currency, date.AddDays(-1)));
                    }
                }
                catch
                {
                    return(bankResult);
                }
                return(bankResult);
            }
        }
        public async Task <IBankResult> GetRateAsync(string currency, DateTime date)
        {
            IBankResult bankResult = new BankResult()
            {
                ExchangeRate = -1m,
                RateDate     = date,
                HomeCurrency = homeCurrency,
                BankName     = bankName
            };

            if (SupportedCurrencies.ContainsKey(currency))
            {
                using (WebClient client = new WebClient())
                {
                    try
                    {
                        RateDate = date;
                        string startDate = date.ToString("yyyyMMdd");
                        Uri    methodUri = new Uri($"{baseUrl}{currency}&{startDate}&json");
                        string json      = await client.DownloadStringTaskAsync(methodUri);

                        JArray ja = (JArray)JsonConvert.DeserializeObject(json);
                        if (ja.Count != 0)
                        {
                            decimal result = decimal.Zero;
                            decimal.TryParse(ja.SelectToken("$[0].rate").ToString(), out result);

                            bankResult.ExchangeRate = result;
                            return(bankResult);
                        }
                        else
                        {
                            return(await GetRateAsync(currency, date.AddDays(-1)));
                        }
                    }
                    catch
                    {
                        return(bankResult);
                    }
                };
            }
            else
            {
                return(bankResult);
            }
        }
示例#3
0
        public Task <IBankResult> GetRateAsync(string currency, DateTime date)
        {
            IBankResult bankResult = new BankResult()
            {
                ExchangeRate = -1m,
                RateDate     = date,
                HomeCurrency = homeCurrency,
                BankName     = bankName
            };


            DailyInfoSoap          client  = new DailyInfoSoapClient(DailyInfoSoapClient.EndpointConfiguration.DailyInfoSoap);
            Task <ArrayOfXElement> fxRates = client.GetCursOnDateAsync(date);

            List <XElement> Results = fxRates.Result.Nodes;

            foreach (XElement result in Results)
            {
                if (result.Name.LocalName == "diffgram")
                {
                    XElement ValuteData = result.Element("ValuteData");
                    IEnumerable <XElement> ValueOnDate = ValuteData.Elements();
                    foreach (XElement ValuteCursOnDate in ValueOnDate)
                    {
                        IEnumerable <XElement> Properties = ValuteCursOnDate.Elements();

                        string Currency = ValuteCursOnDate.Element("VchCode").Value;

                        decimal FxRate = decimal.Zero;
                        decimal.TryParse(ValuteCursOnDate.Element("Vcurs").Value, out FxRate);

                        decimal multiplier = decimal.Zero;
                        decimal.TryParse(ValuteCursOnDate.Element("Vnom").Value, out multiplier);

                        if (Currency == currency)
                        {
                            FxRate /= multiplier;
                            bankResult.ExchangeRate = FxRate;
                            return(Task.FromResult(bankResult));
                        }
                    }
                }
            }
            return(Task.FromResult(bankResult));
        }
示例#4
0
        public BankResult ExecuteGet(int bankId, string date, string currency)
        {
            //http://k_krylov_nb:8060/0/rest/ExchangeRate_WS/ExecuteGet?bankId=0&date=2019-11-20&currency="RUB"

            DateTime.TryParse(date, out DateTime dt);
            IBank bank = BankFactory.GetBank((BankFactory.SupportedBanks)bankId);

            IBankResult bankResult = Task.Run(() => bank.GetRateAsync(currency.ToUpper(), dt)).Result;
            BankResult  result     = new BankResult
            {
                ExchangeRate = bankResult.ExchangeRate,
                RateDate     = bankResult.RateDate,
                HomeCurrency = bankResult.HomeCurrency,
                BankName     = bankResult.BankName
            };

            return(result);
        }
        public async Task <IBankResult> GetRateAsync(string currency, DateTime date)
        {
            IBankResult bankResult = new BankResult()
            {
                ExchangeRate = -1m,
                RateDate     = date,
                HomeCurrency = homeCurrency,
                BankName     = bankName
            };

            //https://www.bankofcanada.ca/valet/observations/FXUSDCAD?start_date=2019-11-05&end_date=2019-11-05
            string startDate = date.ToString("yyyy-MM-dd");
            Uri    methodUri = new Uri($"{baseUrl}/observations/FX{currency}CAD?start_date={startDate}&end_date={startDate}");

            using (WebClient client = new WebClient())
            {
                try
                {
                    decimal result = 0;
                    string  json;
                    json = await client.DownloadStringTaskAsync(methodUri);

                    JObject jo = JsonConvert.DeserializeObject <JObject>(json);

                    JArray ja = (JArray)jo.SelectToken("$.observations");
                    if (ja.Count != 0)
                    {
                        string v = ja.SelectToken($"[0].FX{currency}CAD.v").ToString();
                        decimal.TryParse(v, out result);
                        bankResult.ExchangeRate = result;
                        return(bankResult);
                    }
                    else
                    {
                        return(await GetRateAsync(currency, date.AddDays(-1)));
                    }
                }
                catch
                {
                    return(bankResult);
                }
            }
        }