Skip to content

Commit

Permalink
Merge pull request #66 from tougher/feature/monta-24-hours
Browse files Browse the repository at this point in the history
  • Loading branch information
MattJeanes authored Oct 28, 2024
2 parents 6fb4d1d + 47f305e commit 448b461
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions TeslaMateAgile.Tests/Services/MontaServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions TeslaMateAgile/Services/MontaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MontaOptions> options)
{
_client = client;
Expand Down Expand Up @@ -53,6 +56,9 @@ private async Task<string> GetAccessToken()

private async Task<Charge[]> GetCharges(string accessToken, DateTimeOffset from, DateTimeOffset to)
{
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)
{
Expand Down

0 comments on commit 448b461

Please sign in to comment.