Skip to content

Commit

Permalink
Merge pull request #41 from SP-SoftFuzz/Api
Browse files Browse the repository at this point in the history
Api repository implemented
  • Loading branch information
RespectMathias authored May 30, 2024
2 parents 9842ec6 + 96258a3 commit 199f5e3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/ProfHeat.Core/Interfaces/ISourceDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public interface ISourceDataManager
{
List<MarketCondition> LoadSourceData(string filePath);
void SaveSourceData(List<MarketCondition> data, string filePath);
List<MarketCondition> FetchData(DateTime from, DateTime to);
}
28 changes: 28 additions & 0 deletions src/ProfHeat.Core/Repositories/ApiRepository.cs
Original file line number Diff line number Diff line change
@@ -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;

Check warning on line 10 in src/ProfHeat.Core/Repositories/ApiRepository.cs

View workflow job for this annotation

GitHub Actions / build

The using directive for 'ProfHeat.Core.Interfaces' appeared previously in this namespace

Check warning on line 10 in src/ProfHeat.Core/Repositories/ApiRepository.cs

View workflow job for this annotation

GitHub Actions / build

The using directive for 'ProfHeat.Core.Interfaces' appeared previously in this namespace
using System.Text.Json;

Check warning on line 11 in src/ProfHeat.Core/Repositories/ApiRepository.cs

View workflow job for this annotation

GitHub Actions / build

The using directive for 'System.Text.Json' appeared previously in this namespace

Check warning on line 11 in src/ProfHeat.Core/Repositories/ApiRepository.cs

View workflow job for this annotation

GitHub Actions / build

The using directive for 'System.Text.Json' appeared previously in this namespace

namespace ProfHeat.Core.Repositories;

public class ApiRepository : IRepository
{
private readonly HttpClient _httpClient = new();

public T Load<T>(string url)
{
var response = _httpClient.GetAsync(url).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
var data = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
return JsonSerializer.Deserialize<T>(data)!;
}

public void Save<T>(T data, string filePath) => throw new NotImplementedException();
}
5 changes: 3 additions & 2 deletions src/ProfHeat.Core/Repositories/SourceDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ namespace ProfHeat.Core.Repositories;

public class SourceDataManager(IRepository repository) : ISourceDataManager
{
public List<MarketCondition> LoadSourceData(string filePath) => repository.Load<List<MarketCondition>>(filePath);
public List<MarketCondition> LoadSourceData(string filePath) => repository.Load<List<MarketCondition>>(filePath).ToList();

public void SaveSourceData(List<MarketCondition> data, string filePath) => repository.Save(data, filePath);

// Note Use API repository here
public List<MarketCondition> FetchData(DateTime from, DateTime to) =>
repository.Load<List<MarketCondition>>($"https://api.energidataservice.dk/dataset/Elspotprices?start={from:yyyy-MM-dd}&end={to:yyyy-MM-dd}");
}

0 comments on commit 199f5e3

Please sign in to comment.