Skip to content

Commit

Permalink
feat: convert apple music to use shared 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 a796740 commit a37efbf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
2 changes: 2 additions & 0 deletions internal/apis/applemusic/applemusic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/go-chi/chi/v5"
)

const API_ENDPOINT = "https://api.music.apple.com/"

type cacheData struct {
RecentlyPlayed []song `json:"recently_played"`
Playlists map[string]playlist `json:"playlists"`
Expand Down
42 changes: 34 additions & 8 deletions internal/apis/applemusic/playlists.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package applemusic

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

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

Expand All @@ -30,26 +32,50 @@ type playlistResponse struct {
}

func fetchPlaylist(id string) (playlist, error) {
playlistData, err := sendAPIRequest[playlistResponse](path.Join("v1/me/library/playlists/", id))
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)
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 := sendAPIRequest[playlistTracksResponse](
path.Join("v1/me/library/playlists/", id, "tracks"),
)
trackData, err := apis.SendRequest[playlistTracksResponse](req)
if err != nil {
lumber.Error(err, "failed to get tracks for playlist with id of", id)
return playlist{}, nil
return playlist{}, err
}
totalResponseData = append(totalResponseData, trackData.Data...)
for trackData.Next != "" {
trackData, err = sendAPIRequest[playlistTracksResponse](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)
if err != nil {
lumber.Error(err, "failed to paginate through tracks for playlist with id of", id)
return playlist{}, nil
return playlist{}, err
}
}

Expand Down
22 changes: 19 additions & 3 deletions internal/apis/applemusic/recent.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package applemusic

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

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

type recentlyPlayedResponse struct {
Data []songResponse `json:"data"`
}

func fetchRecentlyPlayed() ([]song, error) {
response, err := sendAPIRequest[recentlyPlayedResponse](
"v1/me/recent/played/tracks",
)
u, err := url.JoinPath(API_ENDPOINT, "v1/me/recent/played/tracks")
if err != nil {
lumber.Error(err, "failed to create URl")
return []song{}, err
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
lumber.Error(err, "failed to create request")
return []song{}, err
}
response, err := apis.SendRequest[recentlyPlayedResponse](req)
if err != nil {
return []song{}, err
}
Expand Down
16 changes: 4 additions & 12 deletions internal/apis/applemusic/api.go → internal/apis/request.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package applemusic
package apis

import (
"encoding/json"
"fmt"
"io"
"net/http"

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

func sendAPIRequest[T any](endpoint string) (T, error) {
// sends a given http.Request and will unmarshal the JSON from the response body and return that as the given type.
func SendRequest[T any](req *http.Request) (T, error) {
var zeroValue T // to be used as "nil" when returning errors
req, err := http.NewRequest("GET", "https://api.music.apple.com/"+endpoint, nil)
if err != nil {
lumber.Error(err, "creating request failed")
return zeroValue, err
}
req.Header.Set("Authorization", "Bearer "+secrets.SECRETS.AppleMusicAppToken)
req.Header.Set("Music-User-Token", secrets.SECRETS.AppleMusicUserToken)

resp, err := http.DefaultClient.Do(req)
if err != nil {
lumber.Error(err, "sending request failed")
Expand All @@ -34,7 +26,7 @@ func sendAPIRequest[T any](endpoint string) (T, error) {
}
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf(
"status code of %d returned from apple music API. Code of 200 expected",
"status code of %d returned from API. Code of 200 expected",
resp.StatusCode,
)
if resp.StatusCode == http.StatusBadGateway ||
Expand Down

0 comments on commit a37efbf

Please sign in to comment.