From 8d9dbbf693fb2d3472ef9b7635796b65c8f6d0c4 Mon Sep 17 00:00:00 2001 From: Matt Gleich Date: Mon, 2 Dec 2024 15:28:40 -0500 Subject: [PATCH] fix: do not URL encode apple music requests Signed-off-by: Matt Gleich --- internal/apis/applemusic/api.go | 10 ++-------- internal/apis/applemusic/playlists.go | 13 +++---------- internal/apis/applemusic/recent.go | 2 +- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/internal/apis/applemusic/api.go b/internal/apis/applemusic/api.go index db409d2..0a959a0 100644 --- a/internal/apis/applemusic/api.go +++ b/internal/apis/applemusic/api.go @@ -1,8 +1,8 @@ package applemusic import ( + "fmt" "net/http" - "net/url" "github.com/gleich/lcp-v2/internal/apis" "github.com/gleich/lcp-v2/internal/secrets" @@ -11,13 +11,7 @@ import ( func sendAppleMusicAPIRequest[T any](path string) (T, error) { var zeroValue T - u, err := url.JoinPath("https://api.music.apple.com/", path) - if err != nil { - lumber.Error(err, "failed to join URL") - return zeroValue, err - } - - req, err := http.NewRequest("GET", u, nil) + req, err := http.NewRequest("GET", fmt.Sprintf("https://api.music.apple.com%s", path), nil) if err != nil { lumber.Error(err, "failed to create request") return zeroValue, err diff --git a/internal/apis/applemusic/playlists.go b/internal/apis/applemusic/playlists.go index 99e9c7f..37e030d 100644 --- a/internal/apis/applemusic/playlists.go +++ b/internal/apis/applemusic/playlists.go @@ -2,10 +2,8 @@ package applemusic import ( "fmt" - "net/http" "time" - "github.com/gleich/lcp-v2/internal/apis" "github.com/gleich/lumber/v3" ) @@ -33,7 +31,7 @@ type playlistResponse struct { func fetchPlaylist(id string) (playlist, error) { playlistData, err := sendAppleMusicAPIRequest[playlistResponse]( - fmt.Sprintf("v1/me/library/playlists/%s", id), + fmt.Sprintf("/v1/me/library/playlists/%s", id), ) if err != nil { lumber.Error(err, "failed to fetch playlist for", id) @@ -42,7 +40,7 @@ func fetchPlaylist(id string) (playlist, error) { var totalResponseData []songResponse trackData, err := sendAppleMusicAPIRequest[playlistTracksResponse]( - fmt.Sprintf("v1/me/library/playlists/%s/tracks", id), + fmt.Sprintf("/v1/me/library/playlists/%s/tracks", id), ) if err != nil { lumber.Error(err, "failed to get tracks for playlist with id of", id) @@ -50,12 +48,7 @@ func fetchPlaylist(id string) (playlist, error) { } totalResponseData = append(totalResponseData, trackData.Data...) for trackData.Next != "" { - req, err := http.NewRequest("GET", trackData.Next, nil) - if err != nil { - lumber.Error(err, "failed to create pagination request") - return playlist{}, err - } - trackData, err = apis.SendRequest[playlistTracksResponse](req) + trackData, err = sendAppleMusicAPIRequest[playlistTracksResponse](trackData.Next) if err != nil { lumber.Error(err, "failed to paginate through tracks for playlist with id of", id) return playlist{}, err diff --git a/internal/apis/applemusic/recent.go b/internal/apis/applemusic/recent.go index 141eee4..15b89a8 100644 --- a/internal/apis/applemusic/recent.go +++ b/internal/apis/applemusic/recent.go @@ -5,7 +5,7 @@ type recentlyPlayedResponse struct { } func fetchRecentlyPlayed() ([]song, error) { - response, err := sendAppleMusicAPIRequest[recentlyPlayedResponse]("v1/me/recent/played/tracks") + response, err := sendAppleMusicAPIRequest[recentlyPlayedResponse]("/v1/me/recent/played/tracks") if err != nil { return []song{}, err }