Exemplo n.º 1
0
        public static async Task <ApiForexRates> GetRate(string key = null, string format = "proto", string from = "USD", string[] to = null, DateTime?date = null)
        {
            string CurrencyToConvert = string.Empty;

            if (to != null)
            {
                CurrencyToConvert = string.Join(",", to);
            }
            ApiForexRates ApiForexRates = new ApiForexRates();

            using (HttpClient client = new HttpClient())
                using (HttpResponseMessage response = await client.GetAsync(BaseUri + "daily-rates." + format + "?from=" + from + "&to=" + CurrencyToConvert + "&key=" + key))
                    using (HttpContent content = response.Content)
                        switch (format)
                        {
                        case "proto":
                            using (var memoryStream = new MemoryStream())
                            {
                                await content.CopyToAsync(memoryStream);

                                memoryStream.Seek(0, SeekOrigin.Begin);
                                ApiForexRates = Serializer.Deserialize <ApiForexRates>(memoryStream);
                            }
                            break;

                        case "json":
                            ApiForexRates = JsonConvert.DeserializeObject <ApiForexRates>(await content.ReadAsStringAsync());
                            break;
                        }
            return(ApiForexRates);
        }
Exemplo n.º 2
0
        public static decimal Convert(this ApiForexRates DailyRates, string from, string to, decimal ammount = 1)
        {
            decimal rateFrom = DailyRates.rates.Where(r => r.Key == from).Select(r => r.Value).First();
            decimal rateTo   = DailyRates.rates.Where(r => r.Key == to).Select(r => r.Value).First();

            return(rateFrom / rateTo * ammount);
        }
        private async Task GetRatesAsync()
        {
            if ((DateTime.UtcNow - _lastFetchedAt).TotalDays > Constants.DEFAULTDAYSPERRATEREFRESH)
            {
                _rates = await ApiForex.GetRate(_apiKey);

                _lastFetchedAt = DateTime.UtcNow;
            }
        }