European Central Bank- part 8
Because I have had last time a flop, now I decide to get the European Central Bank rates.
Going to https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html show the XML – but not the XSD.
So now it is about how to transform from XML to C# classes – and I remembered a discussion with https://siderite.dev/ – Visual Studio does now how to paste from XML to classes. ( Edit=>PasteSpecial +> Paste XML as classes. Ensure that you have not space on the first line…)
Copy and paste fron BNR class, modify the URL to https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
Now the program.cs looks like this
var nbr = new GetNBRExchange(); var list = nbr.GetActualRates(); await foreach (var e in list) { Console.WriteLine($"1 {e.ExchangeFrom} = {e.ExchangeValue} {e.ExchangeTo}"); } var ecb = new GetECBExchange(); list = ecb.GetActualRates(); await foreach (var e in list) { Console.WriteLine($"1 {e.ExchangeFrom} = {e.ExchangeValue} {e.ExchangeTo}"); }
See the similarities ?
It is time for some interfaces . The simplest one is
public interface BankGetExchange { IAsyncEnumerable<ExchangeRates> GetActualRates(); }
And now the Program.cs looks like
static async Task Main(string[] args) { await ShowValues(new GetNBRExchange()); await ShowValues(new GetECBExchange()); } public static async Task ShowValues(BankGetExchange bank) { var list = bank.GetActualRates(); await foreach (var e in list) { Console.WriteLine($"1 {e.ExchangeFrom} = {e.ExchangeValue} {e.ExchangeTo}"); } }
and seems better now!
What can be improved ? Well , to process both in parallel ( now they are processed one after another)
Another condition is not to display intermixed. So the code could stay like
So I ended with the following:
using InfoValutarShared; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using System.Xml.Serialization; namespace InfoValutarDOS { class Program { static async Task Main() { var l = new List<Task<ExchangeRates[]>> { Rates(new GetNBRExchange()), Rates(new GetECBExchange()), }; while (l.Count > 0) { var x = l.ToArray(); var data = await Task.WhenAny(x); ShowValues(await data); l.Remove(data); } } public static async Task<ExchangeRates[]> Rates(BankGetExchange bank) { var list = bank.GetActualRates(); return await list.ToArrayAsync(); } public static void ShowValues(ExchangeRates[] list) { foreach (var e in list) { Console.WriteLine($"1 {e.ExchangeFrom} = {e.ExchangeValue} {e.ExchangeTo}"); } } } }
Infovalutar
And one hour passes...(This is the result of 1 hour per day auto-challenge as a full cycle developer for an exchange rates application)
( You can see the sources at https://github.com/ignatandrei/InfoValutar/ )