From dee2b2ed6ba29f85cd62f2bd9e8cd4269c077c74 Mon Sep 17 00:00:00 2001 From: Christoffer Hoel Date: Mon, 28 Oct 2024 21:27:57 +0100 Subject: [PATCH 1/2] Looking back and forward 24 hours in the Monta API --- TeslaMateAgile/Services/MontaService.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/TeslaMateAgile/Services/MontaService.cs b/TeslaMateAgile/Services/MontaService.cs index ea9e7a2..fae51eb 100644 --- a/TeslaMateAgile/Services/MontaService.cs +++ b/TeslaMateAgile/Services/MontaService.cs @@ -53,6 +53,9 @@ private async Task GetAccessToken() private async Task GetCharges(string accessToken, DateTimeOffset from, DateTimeOffset to) { + from = from.AddHours(-24); + to = to.AddHours(24); + var requestUri = $"{_options.BaseUrl}/charges?state=completed&fromDate={from.UtcDateTime:o}&toDate={to.UtcDateTime:o}"; if (_options.ChargePointId.HasValue) { From 47f305e06ac4ea4d9d9e5948f26ba23d9dcbf13d Mon Sep 17 00:00:00 2001 From: Christoffer Hoel Date: Mon, 28 Oct 2024 22:57:50 +0100 Subject: [PATCH 2/2] Moved the -24/24 hours into const fields --- TeslaMateAgile.Tests/Services/MontaServiceTests.cs | 8 ++++++-- TeslaMateAgile/Services/MontaService.cs | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/TeslaMateAgile.Tests/Services/MontaServiceTests.cs b/TeslaMateAgile.Tests/Services/MontaServiceTests.cs index 0359b69..9dfbd43 100644 --- a/TeslaMateAgile.Tests/Services/MontaServiceTests.cs +++ b/TeslaMateAgile.Tests/Services/MontaServiceTests.cs @@ -56,7 +56,9 @@ public async Task GetCharges_ShouldIncludeChargePointIdQueryParameter_WhenSetInM _handler.SetupRequest(HttpMethod.Post, "https://public-api.monta.com/api/v1/auth/token") .ReturnsResponse(JsonSerializer.Serialize(accessTokenResponse), "application/json"); - _handler.SetupRequest(HttpMethod.Get, $"https://public-api.monta.com/api/v1/charges?state=completed&fromDate={from.UtcDateTime:o}&toDate={to.UtcDateTime:o}&chargePointId=123") + var fromDate = from.AddHours(MontaService.FetchHoursBeforeFrom).UtcDateTime; + var toDate = to.AddHours(MontaService.FetchHoursAfterTo).UtcDateTime; + _handler.SetupRequest(HttpMethod.Get, $"https://public-api.monta.com/api/v1/charges?state=completed&fromDate={fromDate:o}&toDate={toDate:o}&chargePointId=123") .ReturnsResponse(JsonSerializer.Serialize(chargesResponse), "application/json"); var charges = await _subject.GetCharges(from, to); @@ -103,7 +105,9 @@ public async Task GetCharges_ShouldNotIncludeChargePointIdQueryParameter_WhenNot _handler.SetupRequest(HttpMethod.Post, "https://public-api.monta.com/api/v1/auth/token") .ReturnsResponse(JsonSerializer.Serialize(accessTokenResponse), "application/json"); - _handler.SetupRequest(HttpMethod.Get, $"https://public-api.monta.com/api/v1/charges?state=completed&fromDate={from.UtcDateTime:o}&toDate={to.UtcDateTime:o}") + var fromDate = from.AddHours(MontaService.FetchHoursBeforeFrom).UtcDateTime; + var toDate = to.AddHours(MontaService.FetchHoursAfterTo).UtcDateTime; + _handler.SetupRequest(HttpMethod.Get, $"https://public-api.monta.com/api/v1/charges?state=completed&fromDate={fromDate:o}&toDate={toDate:o}") .ReturnsResponse(JsonSerializer.Serialize(chargesResponse), "application/json"); var charges = await _subject.GetCharges(from, to); diff --git a/TeslaMateAgile/Services/MontaService.cs b/TeslaMateAgile/Services/MontaService.cs index fae51eb..b2039d8 100644 --- a/TeslaMateAgile/Services/MontaService.cs +++ b/TeslaMateAgile/Services/MontaService.cs @@ -13,6 +13,9 @@ public class MontaService : IWholePriceDataService private readonly HttpClient _client; private readonly MontaOptions _options; + public const int FetchHoursBeforeFrom = -24; + public const int FetchHoursAfterTo = 24; + public MontaService(HttpClient client, IOptions options) { _client = client; @@ -53,8 +56,8 @@ private async Task GetAccessToken() private async Task GetCharges(string accessToken, DateTimeOffset from, DateTimeOffset to) { - from = from.AddHours(-24); - to = to.AddHours(24); + from = from.AddHours(FetchHoursBeforeFrom); + to = to.AddHours(FetchHoursAfterTo); var requestUri = $"{_options.BaseUrl}/charges?state=completed&fromDate={from.UtcDateTime:o}&toDate={to.UtcDateTime:o}"; if (_options.ChargePointId.HasValue)