Example #1
0
        private RateResult GetLatest()
        {
            RateResult rates;
            var        nextCallTime = _memoryCacheService.GetTimeForNextCall();

            if (nextCallTime > DateTime.UtcNow)
            {
                rates = _memoryCacheService.GetLatestFromCache();
            }
            else
            {
                try
                {
                    rates = _rateClient.GetLatest();
                    _memoryCacheService.SaveLatestToCache(rates);
                    _memoryCacheService.UpdateTimeForNextCall();
                }
                catch (Exception ex)
                {
                    rates = _memoryCacheService.GetLatestFromCache();
                }
            }
            //save to cashe
            return(rates);
        }
        public void GetLatestForFreePlan_WithInvalidApiKey_ThrowsException()
        {
            var sut = new RateClient("invalid");

            var ex = Assert.Throws <Exception>(() => sut.GetLatest());

            Assert.IsTrue(ex.Message.Contains("Invalid API key"));
        }
        public void GetLatestForFreePlan_WithValidApiKey_ReturnsRates()
        {
            var sut = new RateClient(ApiKey);

            var result = sut.GetLatest();

            Assert.NotNull(result);
            Assert.IsTrue(result.Rates.Any());
        }