示例#1
0
        public async Task <string> GetRates()
        {
            ER = new ExchangeRates();

            await ER.GetRates();

            string News = "";

            if (ER.Rates == null)
            {
                return(News = "Возникли проблемы при поиске указанных новостей по запросу \"Курс валют\"");
            }
            else
            {
                News = "Доллар - " + ER.Rates[0] + Environment.NewLine + "Евро - " + ER.Rates[1];
            }

            return(News);
        }
        public void DisposeSessionTest()
        {
            // Arrange
            var session = new Mock<ISession>();
            var sessionFacotry = Mock.Of<ISessionFactory>(t => t.New() == session.Object);
            var repository =
                Mock.Of<IRateRepository>(
                    t =>
                        t.GetCached(session.Object, It.IsAny<Currency>(), It.IsAny<DateTime>(), It.IsAny<DateTime>()) == new Rate[0]);
            var service =
                Mock.Of<IRateService>(
                    t => t.GetRates(Currency.RUB, It.IsAny<IEnumerable<DateTime>>()) == new Rate[0]);
            var exchangeRates = new ExchangeRates(sessionFacotry, repository, service);

            // Act
            exchangeRates.GetRates(Currency.RUB, DateTime.Today, DateTime.Today);

            // Assert
            session.Verify(t => t.Dispose(), Times.Once);
        }
        /// <summary>
        /// Should return exchange rates among the specified currencies that are defined by the source. But only those defined
        /// by the source, do not return calculated exchange rates. E.g. if the source contains "EUR/USD" but not "USD/EUR",
        /// do not return exchange rate "USD/EUR" with value calculated as 1 / "EUR/USD". If the source does not provide
        /// some of the currencies, ignore them.
        /// </summary>
        public IEnumerable <ExchangeRate> GetExchangeRates(IEnumerable <Currency> currencies)
        {
            List <ExchangeRate> toReturn = new List <ExchangeRate>();
            ExchangeRates       ER       = new ExchangeRates("OBsfZ4UDsAa3IVBByphPgTVY");

            foreach (var sourceCurr in currencies)
            {
                foreach (var targetCurr in currencies)
                {
                    if (sourceCurr == targetCurr)                   // faster then ask to server
                    {
                        toReturn.Add(new ExchangeRate(sourceCurr, targetCurr, 1));
                        continue;
                    }
                    var response = ER.GetRates(sourceCurr.Code, targetCurr.Code, ExchangeRates.RatesFields.Averages);
                    if (response.IsSuccessful)                                                                                        // ignoring rates, which are not specified by source
                    {
                        decimal middlePrice = ((decimal)(response.Quotes.First().Value.Ask + response.Quotes.First().Value.Bid)) / 2; // to get average
                        toReturn.Add(new ExchangeRate(sourceCurr, targetCurr, middlePrice));
                    }
                }
            }
            return(toReturn);
        }
示例#4
0
    public static void Main(string[] args)
    {
        // Initializing the API
        var api = new ExchangeRates("<API KEY>");

        // Example of listing currencies

        Console.WriteLine("\n********** Listing Currencies:");
        var responseCurrencies = api.GetCurrencies();

        if (responseCurrencies.IsSuccessful)
        {
            foreach (var currency in responseCurrencies.Currencies)
            {
                Console.WriteLine("{0}:{1}", currency.Code, currency.Description);
            }
        }
        else
        {
            Console.WriteLine("There was an error with the request: {0}", responseCurrencies.ErrorMessage);
        }

        // Example of checking RemainingQuotes

        var responseRemaingQuotes = api.GetRemainingQuotes();

        Console.WriteLine("\n********** Remaining Quotes:");
        if (responseRemaingQuotes.IsSuccessful)
        {
            Console.WriteLine("Remaining quotes: {0}", responseRemaingQuotes.RemainingQuotes);
        }
        else
        {
            Console.WriteLine("There was an error with the request: {0}", responseRemaingQuotes.ErrorMessage);
        }

        // Example of retrieving rates by specifying multiple fields.

        var responseGetRatesWithFields = api.GetRates("USD",
                quote: "EUR",
                start: "2014-01-01",
                end: "2014-01-05",
                fields: new List<ExchangeRates.RatesFields> { ExchangeRates.RatesFields.Averages,
                                                              ExchangeRates.RatesFields.Midpoint });

        if (responseGetRatesWithFields.IsSuccessful)
        {
            Console.WriteLine("\n********** Rates - Averages and Midpoints:");
            foreach (var quote in responseGetRatesWithFields.Quotes)
            {
                Console.WriteLine("From {0} to {1}\nQuote:{2} Ask:{3,10} MidPoint:{4,10} Bid:{5,10}",
                        responseGetRatesWithFields.Meta.EffectiveParams.StartDate.Value.ToShortDateString(),
                        responseGetRatesWithFields.Meta.EffectiveParams.EndDate.Value.ToShortDateString(),
                        quote.Key,
                        quote.Value.Ask,
                        quote.Value.Midpoint,
                        quote.Value.Bid);

            }
        }
        else
        {
            Console.WriteLine("There was an error with the request: {0}", responseGetRatesWithFields.ErrorMessage);
        }

        // Example of retrieving rates by specifying multiple quotes.

        var responseGetRatesForSeveralQuotes = api.GetRates("USD",
                quote: new List<string> {"EUR", "CHF"} ,
                date: "2014-01-01",
                fields: ExchangeRates.RatesFields.Averages);

        if (responseGetRatesForSeveralQuotes.IsSuccessful)
        {
            Console.WriteLine("\n********** Rates for USD/EUR and USD/CHF:");
            foreach (var quote in responseGetRatesForSeveralQuotes.Quotes)
            {
                Console.WriteLine("{0} Quote:{1} Ask:{2,10} Bid:{3,10}",
                        quote.Value.Date,
                        quote.Key,
                        quote.Value.Ask,
                        quote.Value.Bid);

            }
        }
        else
        {
            Console.WriteLine("There was an error with the request: {0}", responseGetRatesWithFields.ErrorMessage);
        }

        Console.ReadKey();
    }
        public void GetRates_CacheIsEnough_Test()
        {
            // Arrange
            var session = Mock.Of<ISession>();
            var sessionFacotry = Mock.Of<ISessionFactory>(t => t.New() == session);
            var rate1 = Mock.Of<Rate>(t => t.Stamp == DateTime.Today.AddDays(-1));
            var rate2 = Mock.Of<Rate>(t => t.Stamp == DateTime.Today);
            var repository =
                Mock.Of<IRateRepository>(
                    t =>
                        t.GetCached(session, It.IsAny<Currency>(), DateTime.Today.AddDays(-1), DateTime.Today) == new[] { rate2, rate1 });
            var service = new Mock<IRateService>();
            var exchangeRates = new ExchangeRates(sessionFacotry, repository, service.Object);

            // Act
            var rates = exchangeRates.GetRates(Currency.RUB, DateTime.Today.AddDays(-1), DateTime.Today);

            // Assert
            Assert.AreEqual(rates.First(), rate1);
            Assert.AreEqual(rates.Last(), rate2);
            service.Verify(t => t.GetRates(It.IsAny<Currency>(), It.IsAny<IEnumerable<DateTime>>()), Times.Never);
        }
        public void GetRates_ApplicationException_Test()
        {
            // Arrange
            var exception = new ApplicationException();
            var sessionFacotry = new Mock<ISessionFactory>();
            sessionFacotry.Setup(t => t.New()).Throws(exception);
            var repository = Mock.Of<IRateRepository>();
            var service = Mock.Of<IRateService>();
            var exchangeRates = new ExchangeRates(sessionFacotry.Object, repository, service);

            // Assert
            var ex = Assert.Throws<ApplicationException>(() => exchangeRates.GetRates(Currency.RUB, DateTime.Today.AddDays(-1), DateTime.Today));
            Assert.AreEqual(ex, exception);
        }
        public void GetRate_ProvideErrorMessage_WhenRepositoryGetCachedFails_Test()
        {
            // Arrange
            var exception = Mock.Of<Exception>(t => t.Message == "Error message");
            var session = Mock.Of<ISession>();
            var sessionFacotry = Mock.Of<ISessionFactory>(t => t.New() == session);
            var repository = new Mock<IRateRepository>();
            repository.Setup(t => t.GetCached(It.IsAny<ISession>(), It.IsAny<Currency>(), It.IsAny<DateTime>(), It.IsAny<DateTime>()))
                      .Throws(exception);
            var service =
                Mock.Of<IRateService>(
                    t => t.GetRates(Currency.RUB, It.Is<IEnumerable<DateTime>>(e => e.Contains(DateTime.Today.AddDays(-1)) && e.Contains(DateTime.Today))) == new Rate[0]);
            var exchangeRates = new ExchangeRates(sessionFacotry, repository.Object, service);

            // Act
            exchangeRates.GetRates(Currency.RUB, DateTime.Today.AddDays(-1), DateTime.Today);

            // Assert
            Assert.AreEqual(exchangeRates.ErrorMessage, "Error message");
        }
        public void GetRate_NotThrows_WhenRepositoryGetCachedFails_Test()
        {
            // Arrange
            var session = Mock.Of<ISession>();
            var sessionFacotry = Mock.Of<ISessionFactory>(t => t.New() == session);
            var repository = new Mock<IRateRepository>();
            repository.Setup(t => t.GetCached(It.IsAny<ISession>(), It.IsAny<Currency>(), It.IsAny<DateTime>(), It.IsAny<DateTime>()))
                      .Throws<Exception>();
            var rate = Mock.Of<Rate>();
            var service =
                Mock.Of<IRateService>(
                    t => t.GetRates(Currency.RUB, It.Is<IEnumerable<DateTime>>(e => e.Contains(DateTime.Today.AddDays(-1)) && e.Contains(DateTime.Today))) == new[] { rate });
            var exchangeRates = new ExchangeRates(sessionFacotry, repository.Object, service);

            // Act
            var rates = exchangeRates.GetRates(Currency.RUB, DateTime.Today.AddDays(-1), DateTime.Today);

            // Assert
            Assert.AreEqual(rates.Single(), rate);
        }
        public void GetRates_SelectedPeriodExceedTwoMonths_Test()
        {
            // Arrange
            var sessionFacotry = Mock.Of<ISessionFactory>();
            var repository = Mock.Of<IRateRepository>();
            var service = Mock.Of<IRateService>();
            var exchangeRates = new ExchangeRates(sessionFacotry, repository, service);

            // Assert
            Assert.Throws<SelectedPeriodExceedTwoMonthsException>(() => exchangeRates.GetRates(Currency.RUB, DateTime.Today.AddDays(-100), DateTime.Today));
        }
        public void GetRates_EndDateIsLaterThanToday_Test()
        {
            // Arrange
            var sessionFacotry = Mock.Of<ISessionFactory>();
            var repository = Mock.Of<IRateRepository>();
            var service = Mock.Of<IRateService>();
            var exchangeRates = new ExchangeRates(sessionFacotry, repository, service);

            // Assert
            Assert.Throws<EndDateIsLaterThanTodayException>(() => exchangeRates.GetRates(Currency.RUB, DateTime.Today, DateTime.Today.AddDays(1)));
        }
        public void GetRates_EmprtyCache_Test()
        {
            // Arrange
            var session = Mock.Of<ISession>();
            var sessionFacotry = Mock.Of<ISessionFactory>(t => t.New() == session);
            var repository =
                Mock.Of<IRateRepository>(
                    t =>
                        t.GetCached(session, It.IsAny<Currency>(), It.IsAny<DateTime>(), It.IsAny<DateTime>()) == new Rate[0]);
            var rate = Mock.Of<Rate>();
            var service =
                Mock.Of<IRateService>(
                    t => t.GetRates(Currency.RUB, It.Is<IEnumerable<DateTime>>(e => e.Contains(DateTime.Today.AddDays(-1)) && e.Contains(DateTime.Today))) == new[] { rate });
            var exchangeRates = new ExchangeRates(sessionFacotry, repository, service);

            // Act
            var rates = exchangeRates.GetRates(Currency.RUB, DateTime.Today.AddDays(-1), DateTime.Today);

            // Assert
            Assert.AreEqual(rates.Single(), rate);
        }
        public void GetRates_EmprtyCache_CacheRates_Test()
        {
            // Arrange
            var session = Mock.Of<ISession>();
            var sessionFacotry = Mock.Of<ISessionFactory>(t => t.New() == session);
            var repository =
                new Mock<IRateRepository>();
            repository.Setup(t => t.GetCached(session, It.IsAny<Currency>(), It.IsAny<DateTime>(), It.IsAny<DateTime>())).Returns(new Rate[0]);
            var rate = Mock.Of<Rate>();
            var service =
                Mock.Of<IRateService>(
                    t => t.GetRates(Currency.RUB, It.Is<IEnumerable<DateTime>>(e => e.Contains(DateTime.Today.AddDays(-1)) && e.Contains(DateTime.Today))) == new[] { rate });
            var exchangeRates = new ExchangeRates(sessionFacotry, repository.Object, service);

            // Act
            exchangeRates.GetRates(Currency.RUB, DateTime.Today.AddDays(-1), DateTime.Today);

            // Assert
            repository.Verify(t => t.Cache(session, It.Is<IEnumerable<Rate>>(e => e.Single() == rate)));
        }
        public void GetRates_CacheNotEnough_Test()
        {
            // Arrange
            var session = Mock.Of<ISession>();
            var sessionFacotry = Mock.Of<ISessionFactory>(t => t.New() == session);
            var rate1 = Mock.Of<Rate>(t => t.Stamp == DateTime.Today.AddDays(-2));
            var rate2 = Mock.Of<Rate>(t => t.Stamp == DateTime.Today.AddDays(-1));
            var rate3 = Mock.Of<Rate>(t => t.Stamp == DateTime.Today);
            var repository =
                Mock.Of<IRateRepository>(
                    t =>
                        t.GetCached(session, It.IsAny<Currency>(), DateTime.Today.AddDays(-2), DateTime.Today) == new[] { rate1, rate3 });
            var service = Mock.Of<IRateService>(t => t.GetRates(Currency.RUB, It.Is<IEnumerable<DateTime>>(e => e.Single() == DateTime.Today.AddDays(-1))) == new[] { rate2});
            var exchangeRates = new ExchangeRates(sessionFacotry, repository, service);

            // Act
            var rates = exchangeRates.GetRates(Currency.RUB, DateTime.Today.AddDays(-2), DateTime.Today);

            // Assert
            Assert.AreEqual(rates[0], rate1);
            Assert.AreEqual(rates[1], rate2);
            Assert.AreEqual(rates[2], rate3);
        }
示例#14
0
    public static void Main(string[] args)
    {
        // Initializing the API
        var api = new ExchangeRates("<API KEY>");

        // Example of listing currencies

        Console.WriteLine("\n********** Listing Currencies:");
        var responseCurrencies = api.GetCurrencies();

        if (responseCurrencies.IsSuccessful)
        {
            foreach (var currency in responseCurrencies.Currencies)
            {
                Console.WriteLine("{0}:{1}", currency.Code, currency.Description);
            }
        }
        else
        {
            Console.WriteLine("There was an error with the request: {0}", responseCurrencies.ErrorMessage);
        }


        // Example of checking RemainingQuotes

        var responseRemaingQuotes = api.GetRemainingQuotes();

        Console.WriteLine("\n********** Remaining Quotes:");
        if (responseRemaingQuotes.IsSuccessful)
        {
            Console.WriteLine("Remaining quotes: {0}", responseRemaingQuotes.RemainingQuotes);
        }
        else
        {
            Console.WriteLine("There was an error with the request: {0}", responseRemaingQuotes.ErrorMessage);
        }


        // Example of retrieving rates by specifying multiple fields.

        var responseGetRatesWithFields = api.GetRates("USD",
                                                      quote: "EUR",
                                                      start: "2014-01-01",
                                                      end: "2014-01-05",
                                                      fields: new List <ExchangeRates.RatesFields> {
            ExchangeRates.RatesFields.Averages,
            ExchangeRates.RatesFields.Midpoint
        });

        if (responseGetRatesWithFields.IsSuccessful)
        {
            Console.WriteLine("\n********** Rates - Averages and Midpoints:");
            foreach (var quote in responseGetRatesWithFields.Quotes)
            {
                Console.WriteLine("From {0} to {1}\nQuote:{2} Ask:{3,10} MidPoint:{4,10} Bid:{5,10}",
                                  responseGetRatesWithFields.Meta.EffectiveParams.StartDate.Value.ToShortDateString(),
                                  responseGetRatesWithFields.Meta.EffectiveParams.EndDate.Value.ToShortDateString(),
                                  quote.Key,
                                  quote.Value.Ask,
                                  quote.Value.Midpoint,
                                  quote.Value.Bid);
            }
        }
        else
        {
            Console.WriteLine("There was an error with the request: {0}", responseGetRatesWithFields.ErrorMessage);
        }

        // Example of retrieving rates by specifying multiple quotes.

        var responseGetRatesForSeveralQuotes = api.GetRates("USD",
                                                            quote: new List <string> {
            "EUR", "CHF"
        },
                                                            date: "2014-01-01",
                                                            fields: ExchangeRates.RatesFields.Averages);

        if (responseGetRatesForSeveralQuotes.IsSuccessful)
        {
            Console.WriteLine("\n********** Rates for USD/EUR and USD/CHF:");
            foreach (var quote in responseGetRatesForSeveralQuotes.Quotes)
            {
                Console.WriteLine("{0} Quote:{1} Ask:{2,10} Bid:{3,10}",
                                  quote.Value.Date,
                                  quote.Key,
                                  quote.Value.Ask,
                                  quote.Value.Bid);
            }
        }
        else
        {
            Console.WriteLine("There was an error with the request: {0}", responseGetRatesWithFields.ErrorMessage);
        }

        Console.ReadKey();
    }