Skip to content

Commit

Permalink
feat: bring back apple music specific request sender
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Dec 1, 2024
1 parent a37efbf commit 040d135
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
34 changes: 34 additions & 0 deletions internal/apis/applemusic/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package applemusic

import (
"net/http"
"net/url"

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

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)
if err != nil {
lumber.Error(err, "failed to create request")
return zeroValue, err
}
req.Header.Set("Authorization", "Bearer "+secrets.SECRETS.AppleMusicAppToken)
req.Header.Set("Music-User-Token", secrets.SECRETS.AppleMusicUserToken)

resp, err := apis.SendRequest[T](req)
if err != nil {
lumber.Error(err, "failed to make apple music API request")
return zeroValue, err
}
return resp, nil
}
35 changes: 3 additions & 32 deletions internal/apis/applemusic/playlists.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package applemusic

import (
"net/http"
"net/url"
"time"

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

Expand All @@ -32,47 +29,21 @@ type playlistResponse struct {
}

func fetchPlaylist(id string) (playlist, error) {
u, err := url.JoinPath(API_ENDPOINT, "v1/me/library/playlist")
if err != nil {
lumber.Error(err, "failed to join urls")
return playlist{}, err
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
lumber.Error(err, "failed to make new request")
return playlist{}, err
}
playlistData, err := apis.SendRequest[playlistResponse](req)
playlistData, err := sendAppleMusicAPIRequest[playlistResponse]("v1/me/library/playlist")
if err != nil {
lumber.Error(err, "failed to fetch playlist for", id)
return playlist{}, err
}

u, err = url.JoinPath(API_ENDPOINT, "v1/me/library/playlists", id, "tracks")
if err != nil {
lumber.Error(err, "failed to join urls")
return playlist{}, err
}
req, err = http.NewRequest("GET", u, nil)
if err != nil {
lumber.Error(err, "failed to make new request")
return playlist{}, err
}

var totalResponseData []songResponse
trackData, err := apis.SendRequest[playlistTracksResponse](req)
trackData, err := sendAppleMusicAPIRequest[playlistTracksResponse]("v1/me/library/playlists")
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 make request for paginated track data", trackData.Next)
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

0 comments on commit 040d135

Please sign in to comment.