From 9734143154493517e3f689f7a99e6074f5df3039 Mon Sep 17 00:00:00 2001 From: DacoTaco Date: Sun, 15 Aug 2021 12:16:23 +0200 Subject: [PATCH] refactor: make code more async --- Infrastructure/SciensanoCovidDownloader.cs | 9 +++++---- MainWindow.xaml.cs | 17 +++++++++-------- SciensanoParsers/BaseSciensanoParser.cs | 5 +++-- SciensanoParsers/CasesParser.cs | 5 +++-- SciensanoParsers/HospitalisationParser.cs | 11 +++++++---- SciensanoParsers/TestsParser.cs | 5 +++-- 6 files changed, 30 insertions(+), 22 deletions(-) diff --git a/Infrastructure/SciensanoCovidDownloader.cs b/Infrastructure/SciensanoCovidDownloader.cs index d023d20..3bd33a2 100644 --- a/Infrastructure/SciensanoCovidDownloader.cs +++ b/Infrastructure/SciensanoCovidDownloader.cs @@ -1,12 +1,13 @@ using Sciensano.CovidJson.Parser.SciensanoModels; using System; using System.Net; +using System.Threading.Tasks; namespace Sciensano.CovidJson.Parser.Infrastructure { public static class SciensanoCovidDownloader { - public static string GetCovidData( Type sourceType ) + public async static Task GetCovidData( Type sourceType ) { var fileName = ""; switch (sourceType.Name) @@ -30,13 +31,13 @@ public static string GetCovidData( Type sourceType ) var data = ""; using (var client = new WebClient()) { - data = client.DownloadString($"https://epistat.sciensano.be/Data/{fileName}"); + data = await client.DownloadStringTaskAsync($"https://epistat.sciensano.be/Data/{fileName}"); } if (string.IsNullOrWhiteSpace(data)) throw new Exception($"failed to download {fileName}. Data is empty"); - return data; - } + return data; + } } } diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index f1874bb..efd9be4 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO; using System.Text; +using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -24,7 +25,7 @@ public MainWindow() private void ExitMenu_Click(object sender, RoutedEventArgs e) => Application.Current.Shutdown(); - private void OpenFile_Click(object sender, RoutedEventArgs e) + private async void OpenFile_Click(object sender, RoutedEventArgs e) { try { @@ -34,7 +35,7 @@ private void OpenFile_Click(object sender, RoutedEventArgs e) DefaultExt = ".json", Filter = "JSON Data (.json)|*.json", InitialDirectory = Directory.GetCurrentDirectory(), - Title = $"Open {((TabItem)Tabs.SelectedItem).Header} File" + Title = $"Open {((TabItem)Tabs.SelectedItem).Header} File" }; if (!(openDialog.ShowDialog(this) ?? false)) @@ -60,7 +61,7 @@ private void OpenFile_Click(object sender, RoutedEventArgs e) IList list; using (var stream = File.Open(openDialog.FileName, FileMode.Open)) - list = GetData(stream, targetType); + list = await GetData(stream, targetType); dataGrid.ItemsSource = list.Count > 0 ? list : null; } @@ -70,7 +71,7 @@ private void OpenFile_Click(object sender, RoutedEventArgs e) } } - private void DownloadFile_Click(object sender, RoutedEventArgs e) + private async void DownloadFile_Click(object sender, RoutedEventArgs e) { try { @@ -92,10 +93,10 @@ private void DownloadFile_Click(object sender, RoutedEventArgs e) sourceType = typeof(SciensanoHospitalisationModel); } - var data = SciensanoCovidDownloader.GetCovidData(sourceType); + var data = await SciensanoCovidDownloader.GetCovidData(sourceType); IList list; using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(data))) - list = GetData(stream, sourceType); + list = await GetData(stream, sourceType); dataGrid.ItemsSource = list.Count > 0 ? list : null; } @@ -105,7 +106,7 @@ private void DownloadFile_Click(object sender, RoutedEventArgs e) } } - private IList GetData(Stream stream, Type targetType) + private async Task> GetData(Stream stream, Type targetType) { try { @@ -113,7 +114,7 @@ private IList GetData(Stream stream, Type targetType) throw new ArgumentException("Invalid stream or targetType."); var parser = SciensanoParserFactory.GetParser(targetType); - return parser.Parse(stream); + return await parser.ParseAsync(stream); } catch (Exception ex) { diff --git a/SciensanoParsers/BaseSciensanoParser.cs b/SciensanoParsers/BaseSciensanoParser.cs index 0f431ed..9248a06 100644 --- a/SciensanoParsers/BaseSciensanoParser.cs +++ b/SciensanoParsers/BaseSciensanoParser.cs @@ -5,12 +5,13 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading.Tasks; namespace Sciensano.CovidJson.Parser.SciensanoParsers { public interface ISciensanoParser { - IList Parse(Stream stream); + Task> ParseAsync(Stream stream); } public abstract class BaseSciensanoParser : ISciensanoParser @@ -19,7 +20,7 @@ public abstract class BaseSciensanoParser : ISciensanoParser { public BaseSciensanoParser() { } - public abstract IList Parse(Stream stream); + public abstract Task> ParseAsync(Stream stream); protected static IEnumerable ParseJson(Stream stream) { diff --git a/SciensanoParsers/CasesParser.cs b/SciensanoParsers/CasesParser.cs index 577903d..a5aa473 100644 --- a/SciensanoParsers/CasesParser.cs +++ b/SciensanoParsers/CasesParser.cs @@ -4,12 +4,13 @@ using System.IO; using System.Linq; using System; +using System.Threading.Tasks; namespace Sciensano.CovidJson.Parser.SciensanoParsers { public class CasesParser : BaseSciensanoParser { - public override IList Parse(Stream stream) + public override Task> ParseAsync(Stream stream) { //string testJson = "[{\"DATE\":\"2020-03-01\",\"PROVINCE\":\"Antwerpen\",\"REGION\":\"Flanders\",\"AGEGROUP\":\"40-49\",\"SEX\":\"M\",\"CASES\":1}]"; var list = new List(); @@ -36,7 +37,7 @@ public override IList Parse(Stream stream) if (!list.Contains(casesModel)) list.Add(casesModel); } - return list.Cast().ToList(); + return Task.FromResult(list.Cast().ToList()); } } } diff --git a/SciensanoParsers/HospitalisationParser.cs b/SciensanoParsers/HospitalisationParser.cs index fd249af..39657f1 100644 --- a/SciensanoParsers/HospitalisationParser.cs +++ b/SciensanoParsers/HospitalisationParser.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading.Tasks; namespace Sciensano.CovidJson.Parser.SciensanoParsers { @@ -10,7 +11,7 @@ public class HospitalisationParser : BaseSciensanoParser Parse(Stream stream) + public override Task> ParseAsync(Stream stream) { //string testJson = "[{\"DATE\":\"2020-03-15\",\"PROVINCE\":\"Antwerpen\",\"REGION\":\"Flanders\",\"NR_REPORTING\":14,\"TOTAL_IN\":50,\"TOTAL_IN_ICU\":9,\"TOTAL_IN_RESP\":4,\"TOTAL_IN_ECMO\":0,\"NEW_IN\":8,\"NEW_OUT\":8}]"; @@ -29,15 +30,17 @@ public override IList Parse(Stream stream) } else { - hospitalisationModel = new HospitalisationModel(model); - hospitalisationModel.PreviousTotalHospitalisations = list.LastOrDefault()?.TotalHospitalisations ?? 0; + hospitalisationModel = new HospitalisationModel(model) + { + PreviousTotalHospitalisations = list.LastOrDefault()?.TotalHospitalisations ?? 0 + }; } if (!list.Contains(hospitalisationModel)) list.Add(hospitalisationModel); } - return list.Cast().ToList(); + return Task.FromResult(list.Cast().ToList()); } } } diff --git a/SciensanoParsers/TestsParser.cs b/SciensanoParsers/TestsParser.cs index e3e89d8..7a764e3 100644 --- a/SciensanoParsers/TestsParser.cs +++ b/SciensanoParsers/TestsParser.cs @@ -3,12 +3,13 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading.Tasks; namespace Sciensano.CovidJson.Parser.SciensanoParsers { public class TestsParser : BaseSciensanoParser { - public override IList Parse(Stream stream) + public override Task> ParseAsync(Stream stream) { //string testJson = "[{\"DATE\":\"2020-03-01\",\"PROVINCE\":\"Antwerpen\",\"REGION\":\"Flanders\",\"TESTS_ALL\":18,\"TESTS_ALL_POS\":0}]"; @@ -36,7 +37,7 @@ public override IList Parse(Stream stream) if (!list.Contains(testsModel)) list.Add(testsModel); } - return list.Cast().ToList(); + return Task.FromResult(list.Cast().ToList()); } } }