Add another national day source since DOTY is broken

This commit is contained in:
2026-04-01 11:58:51 -04:00
parent 8de424087f
commit c088ab8292
11 changed files with 155 additions and 17 deletions

View File

@@ -39,3 +39,13 @@ GET {{Calendar_HostAddress}}/national-days/today?timezone=America/New_York
Accept: application/json
###
GET {{Calendar_HostAddress}}/national-days/today?timezone=America/New_York&provider=DaysOfTheYear
Accept: application/json
###
GET {{Calendar_HostAddress}}/national-days/today?timezone=America/New_York&provider=HolidayCalendar
Accept: application/json
###

View File

@@ -1,4 +1,6 @@
using ChrisKaczor.HomeMonitor.Calendar.Service.Models.NationalDays;
using ChrisKaczor.HomeMonitor.Calendar.Service.Models;
using DaysOfTheYear = ChrisKaczor.HomeMonitor.Calendar.Service.Models.DaysOfTheYear;
using HolidayCalendar = ChrisKaczor.HomeMonitor.Calendar.Service.Models.HolidayCalendar;
using Microsoft.AspNetCore.Mvc;
using RestSharp;
@@ -9,17 +11,58 @@ namespace ChrisKaczor.HomeMonitor.Calendar.Service.Controllers;
public class NationalDaysController(IConfiguration configuration, RestClient restClient) : ControllerBase
{
[HttpGet("today")]
public async Task<ActionResult<Response>> GetToday([FromQuery] string timezone = "Etc/UTC")
public async Task<ActionResult<IEnumerable<NationalDay>>> GetToday([FromQuery] string timezone = "Etc/UTC", [FromQuery] string provider = "")
{
if (string.IsNullOrEmpty(provider))
{
provider = configuration["Calendar:NationalDays:Provider"] ?? string.Empty;
}
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timezone);
var timeZoneOffset = timeZoneInfo.GetUtcOffset(DateTimeOffset.Now).TotalHours;
var timeZoneOffset = timeZoneInfo.GetUtcOffset(DateTimeOffset.Now);
var restRequest = new RestRequest(configuration["Calendar:NationalDays:Url"]);
restRequest.AddHeader("X-Api-Key", configuration["Calendar:NationalDays:Key"] ?? string.Empty);
restRequest.AddQueryParameter("timezone_offset", timeZoneOffset);
if (provider == "DaysOfTheYear")
{
return Ok(await GetFromDaysOfTheYear(timeZoneOffset));
}
var response = await restClient.GetAsync<Response>(restRequest);
return Ok(await GetFromHolidayCalendar(timeZoneOffset));
}
return Ok(response?.Data.Where(d => d.Type == "day"));
private async Task<IEnumerable<NationalDay>?> GetFromDaysOfTheYear(TimeSpan timeZoneOffset)
{
var timeZoneOffsetHours = timeZoneOffset.TotalHours;
var restRequest = new RestRequest(configuration["Calendar:DaysOfTheYear:Url"]);
restRequest.AddHeader("X-Api-Key", configuration["Calendar:DaysOfTheYear:Key"] ?? string.Empty);
restRequest.AddQueryParameter("timezone_offset", timeZoneOffsetHours);
var response = await restClient.GetAsync<DaysOfTheYear.Response>(restRequest);
var items = response?.Data.Where(d => d.Type == "day");
var nationalDays = items?.Select(i => new NationalDay(i));
return nationalDays;
}
private async Task<IEnumerable<NationalDay>?> GetFromHolidayCalendar(TimeSpan timeZoneOffset)
{
var now = DateTimeOffset.UtcNow.ToOffset(timeZoneOffset);
var dateString = now.ToString("yyyy-MM-dd");
var restRequest = new RestRequest(configuration["Calendar:HolidayCalendar:Url"]);
restRequest.AddHeader("Authorization", $"Bearer {configuration["Calendar:HolidayCalendar:Key"] ?? string.Empty}");
restRequest.AddQueryParameter("limit", 100);
restRequest.AddQueryParameter("country", configuration["Calendar:HolidayCalendar:Country"]);
restRequest.AddQueryParameter("type", "Day");
restRequest.AddQueryParameter("startDate", dateString);
restRequest.AddQueryParameter("endDate", dateString);
var response = await restClient.GetAsync<HolidayCalendar.Response>(restRequest);
var nationalDays = response?.Data.Items.Select(i => new NationalDay(i));
return nationalDays;
}
}

View File

@@ -1,7 +1,7 @@
using JetBrains.Annotations;
using System.Text.Json.Serialization;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.NationalDays;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.DaysOfTheYear;
[PublicAPI]
public class Entry

View File

@@ -1,7 +1,7 @@
using JetBrains.Annotations;
using System.Text.Json.Serialization;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.NationalDays;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.DaysOfTheYear;
[PublicAPI]
public class Meta

View File

@@ -1,11 +1,11 @@
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.NationalDays;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.DaysOfTheYear;
[PublicAPI]
public class Response
{
public int Code { get; set; }
public Meta Meta { get; set; } = new();
public IEnumerable<Entry> Data { get; set; } = Array.Empty<Entry>();
public IEnumerable<Entry> Data { get; set; } = [];
}

View File

@@ -0,0 +1,13 @@
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.HolidayCalendar;
[PublicAPI]
public class Data
{
public IEnumerable<Item> Items { get; set; } = [];
public int Total { get; set; }
public int Page { get; set; }
public int Limit { get; set; }
public int TotalPages { get; set; }
}

View File

@@ -0,0 +1,13 @@
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.HolidayCalendar;
[PublicAPI]
public class Item
{
public required string Id { get; set; }
public required string Name { get; set; }
public required string Excerpt { get; set; }
public required string Url { get; set; }
public required string Type { get; set; }
}

View File

@@ -0,0 +1,10 @@
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.HolidayCalendar;
[PublicAPI]
public class Response
{
public bool Success { get; set; }
public Data Data { get; set; } = new();
}

View File

@@ -0,0 +1,31 @@
using JetBrains.Annotations;
using System.Diagnostics.CodeAnalysis;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models;
[PublicAPI]
public class NationalDay
{
public required string Name { get; set; }
public required string Url { get; set; }
public required string Excerpt { get; set; }
public required string Type { get; set; }
[SetsRequiredMembers]
public NationalDay(HolidayCalendar.Item item)
{
Name = item.Name;
Url = item.Url;
Excerpt = item.Excerpt;
Type = item.Type;
}
[SetsRequiredMembers]
public NationalDay(DaysOfTheYear.Entry entry)
{
Name = entry.Name;
Url = entry.Url;
Excerpt = entry.Excerpt;
Type = entry.Type;
}
}

View File

@@ -12,9 +12,17 @@
"Calendar": {
"PersonalUrl": "",
"HolidayUrl": "",
"NationalDays": {
"DaysOfTheYear": {
"Url": "",
"Key": ""
},
"HolidayCalendar": {
"Url": "",
"Key": "",
"Country": "recYxvMVBeTBnsPrJ"
},
"NationalDays": {
"Provider": "HolidayCalendar"
}
}
}

View File

@@ -35,16 +35,26 @@ spec:
secretKeyRef:
name: calendar-config
key: HOLIDAYS_URL
- name: Calendar__NationalDays__Url
- name: Calendar__DaysOfTheYear__Url
valueFrom:
secretKeyRef:
name: calendar-config
key: NATIONAL_DAYS_URL
- name: Calendar__NationalDays__Key
key: DAYS_OF_THE_YEAR_URL
- name: Calendar__DaysOfTheYear__Key
valueFrom:
secretKeyRef:
name: calendar-config
key: NATIONAL_DAYS_KEY
key: DAYS_OF_THE_YEAR_KEY
- name: Calendar__HolidayCalendar__Url
valueFrom:
secretKeyRef:
name: calendar-config
key: HOLIDAY_CALENDAR_URL
- name: Calendar__HolidayCalendar__Key
valueFrom:
secretKeyRef:
name: calendar-config
key: HOLIDAY_CALENDAR_KEY
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst