Example #1
0
 private void RequestExchangeRates(int attemptNumber)
 {
     if (attemptNumber > MAX_LOOKUP_ATTEMPTS)
     {
         return;
     }
     try
     {
         Logger.Log(this, "Looking up the latest currency exchange rates");
         var jsonObj = MiscTools.GetJson($"http://api.fixer.io/latest?base=EUR");
         exchangeRates = new Dictionary <string, decimal>();
         foreach (var prop in jsonObj["rates"].Children().Cast <JProperty>())
         {
             exchangeRates[prop.Name] = (decimal)prop.Value;
         }
         Logger.Log(this, $"Exchange rates for {exchangeRates.Count + 1} currencies have been updated.");
     }
     catch (WebException e) when(e.Message == "The request timed out" || e.Message == "The remote server returned an error: (500) Internal Server Error.")
     {
         Logger.Log(this, $"A server error occurred while updating the currency exhange rates. Retrying in {attemptNumber} seconds...");
         Task.Delay(TimeSpan.FromSeconds(attemptNumber)).ContinueWith(_ => RequestExchangeRates(++attemptNumber));
     }
     catch (Exception e)
     {
         Logger.LogException(this, e, "updating the currency exchange rates");
         RequestExchangeRates(++attemptNumber);
     }
 }