mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-06-15 09:45:07 -04:00
Add another national day source since DOTY is broken
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
|
###
|
||||||
@@ -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 Microsoft.AspNetCore.Mvc;
|
||||||
using RestSharp;
|
using RestSharp;
|
||||||
|
|
||||||
@@ -9,17 +11,58 @@ 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 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -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; } = [];
|
||||||
}
|
}
|
||||||
13
Calendar/Service/Models/HolidayCalendar/Data.cs
Normal file
13
Calendar/Service/Models/HolidayCalendar/Data.cs
Normal 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; }
|
||||||
|
}
|
||||||
13
Calendar/Service/Models/HolidayCalendar/Item.cs
Normal file
13
Calendar/Service/Models/HolidayCalendar/Item.cs
Normal 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; }
|
||||||
|
}
|
||||||
10
Calendar/Service/Models/HolidayCalendar/Response.cs
Normal file
10
Calendar/Service/Models/HolidayCalendar/Response.cs
Normal 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();
|
||||||
|
}
|
||||||
31
Calendar/Service/Models/NationalDay.cs
Normal file
31
Calendar/Service/Models/NationalDay.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,9 +12,17 @@
|
|||||||
"Calendar": {
|
"Calendar": {
|
||||||
"PersonalUrl": "",
|
"PersonalUrl": "",
|
||||||
"HolidayUrl": "",
|
"HolidayUrl": "",
|
||||||
"NationalDays": {
|
"DaysOfTheYear": {
|
||||||
"Url": "",
|
"Url": "",
|
||||||
"Key": ""
|
"Key": ""
|
||||||
|
},
|
||||||
|
"HolidayCalendar": {
|
||||||
|
"Url": "",
|
||||||
|
"Key": "",
|
||||||
|
"Country": "recYxvMVBeTBnsPrJ"
|
||||||
|
},
|
||||||
|
"NationalDays": {
|
||||||
|
"Provider": "HolidayCalendar"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user