Skip to content

Commit

Permalink
refactor: make code more async
Browse files Browse the repository at this point in the history
  • Loading branch information
DacoTaco committed Aug 15, 2021
1 parent 2c5caf0 commit 9734143
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
9 changes: 5 additions & 4 deletions Infrastructure/SciensanoCovidDownloader.cs
Original file line number Diff line number Diff line change
@@ -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<string> GetCovidData( Type sourceType )
{
var fileName = "";
switch (sourceType.Name)
Expand All @@ -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;
}
}
}
17 changes: 9 additions & 8 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
{
Expand All @@ -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))
Expand All @@ -60,7 +61,7 @@ private void OpenFile_Click(object sender, RoutedEventArgs e)

IList<ILocalModel> 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;
}
Expand All @@ -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
{
Expand All @@ -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<ILocalModel> 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;
}
Expand All @@ -105,15 +106,15 @@ private void DownloadFile_Click(object sender, RoutedEventArgs e)
}
}

private IList<ILocalModel> GetData(Stream stream, Type targetType)
private async Task<IList<ILocalModel>> GetData(Stream stream, Type targetType)
{
try
{
if (stream == null || targetType == null)
throw new ArgumentException("Invalid stream or targetType.");

var parser = SciensanoParserFactory.GetParser(targetType);
return parser.Parse(stream);
return await parser.ParseAsync(stream);
}
catch (Exception ex)
{
Expand Down
5 changes: 3 additions & 2 deletions SciensanoParsers/BaseSciensanoParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILocalModel> Parse(Stream stream);
Task<List<ILocalModel>> ParseAsync(Stream stream);
}

public abstract class BaseSciensanoParser<T,Y> : ISciensanoParser
Expand All @@ -19,7 +20,7 @@ public abstract class BaseSciensanoParser<T,Y> : ISciensanoParser
{
public BaseSciensanoParser() { }

public abstract IList<ILocalModel> Parse(Stream stream);
public abstract Task<List<ILocalModel>> ParseAsync(Stream stream);

protected static IEnumerable<Y> ParseJson(Stream stream)
{
Expand Down
5 changes: 3 additions & 2 deletions SciensanoParsers/CasesParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CasesModel, SciensanoCasesModel>
{
public override IList<ILocalModel> Parse(Stream stream)
public override Task<List<ILocalModel>> ParseAsync(Stream stream)
{
//string testJson = "[{\"DATE\":\"2020-03-01\",\"PROVINCE\":\"Antwerpen\",\"REGION\":\"Flanders\",\"AGEGROUP\":\"40-49\",\"SEX\":\"M\",\"CASES\":1}]";
var list = new List<CasesModel>();
Expand All @@ -36,7 +37,7 @@ public override IList<ILocalModel> Parse(Stream stream)
if (!list.Contains(casesModel))
list.Add(casesModel);
}
return list.Cast<ILocalModel>().ToList();
return Task.FromResult(list.Cast<ILocalModel>().ToList());
}
}
}
11 changes: 7 additions & 4 deletions SciensanoParsers/HospitalisationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Sciensano.CovidJson.Parser.SciensanoParsers
{
public class HospitalisationParser : BaseSciensanoParser<HospitalisationModel,SciensanoHospitalisationModel>
{
public HospitalisationParser() { }

public override IList<ILocalModel> Parse(Stream stream)
public override Task<List<ILocalModel>> 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}]";

Expand All @@ -29,15 +30,17 @@ public override IList<ILocalModel> 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<ILocalModel>().ToList();
return Task.FromResult(list.Cast<ILocalModel>().ToList());
}
}
}
5 changes: 3 additions & 2 deletions SciensanoParsers/TestsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestsModel, SciensanoTestsModel>
{
public override IList<ILocalModel> Parse(Stream stream)
public override Task<List<ILocalModel>> ParseAsync(Stream stream)
{
//string testJson = "[{\"DATE\":\"2020-03-01\",\"PROVINCE\":\"Antwerpen\",\"REGION\":\"Flanders\",\"TESTS_ALL\":18,\"TESTS_ALL_POS\":0}]";

Expand Down Expand Up @@ -36,7 +37,7 @@ public override IList<ILocalModel> Parse(Stream stream)
if (!list.Contains(testsModel))
list.Add(testsModel);
}
return list.Cast<ILocalModel>().ToList();
return Task.FromResult(list.Cast<ILocalModel>().ToList());
}
}
}

0 comments on commit 9734143

Please sign in to comment.