From 96258a3bd41e960c58c9b5a6efb04bf0eec236b0 Mon Sep 17 00:00:00 2001 From: Karlitos911 Date: Thu, 30 May 2024 14:58:31 +0200 Subject: [PATCH] Api repository implemented Only backend implementation, no UI --- .../Interfaces/ISourceDataManager.cs | 1 + .../Repositories/ApiRepository.cs | 28 +++++++++++++++++++ .../Repositories/SourceDataManager.cs | 5 ++-- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/ProfHeat.Core/Repositories/ApiRepository.cs diff --git a/src/ProfHeat.Core/Interfaces/ISourceDataManager.cs b/src/ProfHeat.Core/Interfaces/ISourceDataManager.cs index abbb52c..f00e173 100644 --- a/src/ProfHeat.Core/Interfaces/ISourceDataManager.cs +++ b/src/ProfHeat.Core/Interfaces/ISourceDataManager.cs @@ -20,4 +20,5 @@ public interface ISourceDataManager { List LoadSourceData(string filePath); void SaveSourceData(List data, string filePath); + List FetchData(DateTime from, DateTime to); } diff --git a/src/ProfHeat.Core/Repositories/ApiRepository.cs b/src/ProfHeat.Core/Repositories/ApiRepository.cs new file mode 100644 index 0000000..908723d --- /dev/null +++ b/src/ProfHeat.Core/Repositories/ApiRepository.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Threading.Tasks; +using ProfHeat.Core.Interfaces; +using ProfHeat.Core.Models; + +using ProfHeat.Core.Interfaces; +using System.Text.Json; + +namespace ProfHeat.Core.Repositories; + +public class ApiRepository : IRepository +{ + private readonly HttpClient _httpClient = new(); + + public T Load(string url) + { + var response = _httpClient.GetAsync(url).GetAwaiter().GetResult(); + response.EnsureSuccessStatusCode(); + var data = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + return JsonSerializer.Deserialize(data)!; + } + + public void Save(T data, string filePath) => throw new NotImplementedException(); +} diff --git a/src/ProfHeat.Core/Repositories/SourceDataManager.cs b/src/ProfHeat.Core/Repositories/SourceDataManager.cs index 73c5898..6e9bd7b 100644 --- a/src/ProfHeat.Core/Repositories/SourceDataManager.cs +++ b/src/ProfHeat.Core/Repositories/SourceDataManager.cs @@ -19,9 +19,10 @@ namespace ProfHeat.Core.Repositories; public class SourceDataManager(IRepository repository) : ISourceDataManager { - public List LoadSourceData(string filePath) => repository.Load>(filePath); + public List LoadSourceData(string filePath) => repository.Load>(filePath).ToList(); public void SaveSourceData(List data, string filePath) => repository.Save(data, filePath); - // Note Use API repository here + public List FetchData(DateTime from, DateTime to) => + repository.Load>($"https://api.energidataservice.dk/dataset/Elspotprices?start={from:yyyy-MM-dd}&end={to:yyyy-MM-dd}"); }