Skip to content

Commit

Permalink
fix: do not URL encode apple music requests
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Dec 2, 2024
1 parent ece7e0d commit 8d9dbbf
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
10 changes: 2 additions & 8 deletions internal/apis/applemusic/api.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
13 changes: 3 additions & 10 deletions internal/apis/applemusic/playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package applemusic

import (
"fmt"
"net/http"
"time"

"github.com/gleich/lcp-v2/internal/apis"
"github.com/gleich/lumber/v3"
)

Expand Down Expand Up @@ -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)
Expand All @@ -42,20 +40,15 @@ 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)
return playlist{}, err
}
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
Expand Down
2 changes: 1 addition & 1 deletion internal/apis/applemusic/recent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 8d9dbbf

Please sign in to comment.