2 Commits

12 changed files with 168 additions and 17 deletions

View File

@@ -39,3 +39,13 @@ GET {{Calendar_HostAddress}}/national-days/today?timezone=America/New_York
Accept: application/json 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,6 +1,8 @@
using ChrisKaczor.HomeMonitor.Calendar.Service.Models.NationalDays; using ChrisKaczor.HomeMonitor.Calendar.Service.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using RestSharp; using RestSharp;
using DaysOfTheYear = ChrisKaczor.HomeMonitor.Calendar.Service.Models.DaysOfTheYear;
using HolidayCalendar = ChrisKaczor.HomeMonitor.Calendar.Service.Models.HolidayCalendar;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Controllers; namespace ChrisKaczor.HomeMonitor.Calendar.Service.Controllers;
@@ -9,17 +11,59 @@ namespace ChrisKaczor.HomeMonitor.Calendar.Service.Controllers;
public class NationalDaysController(IConfiguration configuration, RestClient restClient) : ControllerBase public class NationalDaysController(IConfiguration configuration, RestClient restClient) : ControllerBase
{ {
[HttpGet("today")] [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 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"]); if (provider == "DaysOfTheYear")
restRequest.AddHeader("X-Api-Key", configuration["Calendar:NationalDays:Key"] ?? string.Empty); {
restRequest.AddQueryParameter("timezone_offset", timeZoneOffset); 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 countries = configuration.GetSection("Calendar:HolidayCalendar:CountryCodes").Get<List<string>>() ?? [];
var restRequest = new RestRequest(configuration["Calendar:HolidayCalendar:Url"]);
restRequest.AddHeader("Authorization", $"Bearer {configuration["Calendar:HolidayCalendar:Key"] ?? string.Empty}");
restRequest.AddQueryParameter("limit", 100);
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.Where(i => countries.Contains(i.Country.Code)).Select(i => new NationalDay(i));
return nationalDays;
} }
} }

View File

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

View File

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

View File

@@ -1,11 +1,11 @@
using JetBrains.Annotations; using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.NationalDays; namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.DaysOfTheYear;
[PublicAPI] [PublicAPI]
public class Response public class Response
{ {
public int Code { get; set; } public int Code { get; set; }
public Meta Meta { get; set; } = new(); 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,11 @@
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.HolidayCalendar;
[PublicAPI]
public class Country
{
public required string Id { get; set; }
public required string Name { get; set; }
public required string Code { 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,14 @@
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; }
public required Country Country { 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": { "Calendar": {
"PersonalUrl": "", "PersonalUrl": "",
"HolidayUrl": "", "HolidayUrl": "",
"NationalDays": { "DaysOfTheYear": {
"Url": "", "Url": "",
"Key": "" "Key": ""
},
"HolidayCalendar": {
"Url": "",
"Key": "",
"CountryCodes": [ "US", "INTL" ]
},
"NationalDays": {
"Provider": "HolidayCalendar"
} }
} }
} }

View File

@@ -35,16 +35,26 @@ spec:
secretKeyRef: secretKeyRef:
name: calendar-config name: calendar-config
key: HOLIDAYS_URL key: HOLIDAYS_URL
- name: Calendar__NationalDays__Url - name: Calendar__DaysOfTheYear__Url
valueFrom: valueFrom:
secretKeyRef: secretKeyRef:
name: calendar-config name: calendar-config
key: NATIONAL_DAYS_URL key: DAYS_OF_THE_YEAR_URL
- name: Calendar__NationalDays__Key - name: Calendar__DaysOfTheYear__Key
valueFrom: valueFrom:
secretKeyRef: secretKeyRef:
name: calendar-config 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 restartPolicy: Always
terminationGracePeriodSeconds: 30 terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst dnsPolicy: ClusterFirst