From 22438fc099d9cf1fa365cc9e1122ca867eab37c1 Mon Sep 17 00:00:00 2001 From: Matt Gleich Date: Mon, 3 Feb 2025 11:12:13 -0500 Subject: [PATCH] feat: get cache name via type Signed-off-by: Matt Gleich --- pkg/lcp/fetch.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/pkg/lcp/fetch.go b/pkg/lcp/fetch.go index 6b6099e..2e959a2 100644 --- a/pkg/lcp/fetch.go +++ b/pkg/lcp/fetch.go @@ -10,15 +10,6 @@ import ( "time" ) -type Cache string - -const ( - AppleMusic Cache = "applemusic" - GitHub Cache = "github" - Steam Cache = "steam" - Strava Cache = "strava" -) - type Client struct { Token string httpClient http.Client @@ -29,13 +20,25 @@ type Response[T CacheData] struct { Updated time.Time } -func FetchCache[T CacheData](client *Client, cache Cache) (Response[T], error) { +func FetchCache[T CacheData](client *Client) (Response[T], error) { var zeroValue Response[T] // acts as "nil" value to be used when returning an error if client.Token == "" { return zeroValue, errors.New("no token provided in client") } - url, err := url.JoinPath("https://lcp.dev.mattglei.ch", string(cache)) + var cacheName string + switch any(zeroValue.Data).(type) { + case AppleMusicCache: + cacheName = "applemusic" + case []GitHubRepository: + cacheName = "github" + case []SteamGame: + cacheName = "steam" + case []StravaActivity: + cacheName = "strava" + } + + url, err := url.JoinPath("https://lcp.dev.mattglei.ch", cacheName) if err != nil { return zeroValue, fmt.Errorf("%v failed to join path for URL", err) }