Skip to content

Commit

Permalink
feat: get cache name via type
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <git@mattglei.ch>
  • Loading branch information
gleich committed Feb 3, 2025
1 parent 91cccd9 commit 22438fc
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions pkg/lcp/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit 22438fc

Please sign in to comment.