Skip to content

Commit

Permalink
Merge pull request #38 from tomjowitt/16-get-multiple-tracks
Browse files Browse the repository at this point in the history
Add multiple tracks
  • Loading branch information
tomjowitt authored Apr 20, 2024
2 parents 392c6a4 + da70f14 commit 8eb8653
Show file tree
Hide file tree
Showing 5 changed files with 456 additions and 17 deletions.
31 changes: 26 additions & 5 deletions album.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ type ItemMetaData struct {
Total int `json:"total"`
}

type idListParams struct {
ids string
}

// GetSingleAlbum returns an album that matches an ID.
func (c *Client) GetSingleAlbum(ctx context.Context, id string) (*Album, error) {
if id == "" {
Expand Down Expand Up @@ -184,11 +188,7 @@ func (c *Client) GetAlbumByBarcodeID(ctx context.Context, barcodeID string) ([]A

// GetMultipleAlbums returns a list of albums filtered by their IDs.
func (c *Client) GetMultipleAlbums(ctx context.Context, ids []string) ([]Album, error) {
type multiAlbumParams struct {
ids string
}

params := multiAlbumParams{
params := idListParams{
ids: strings.Join(ids, ","),
}

Expand Down Expand Up @@ -286,3 +286,24 @@ func (c *Client) GetTracksByISRC(ctx context.Context, isrc string, params Pagina

return result.Data, nil
}

// GetMultipleTracks returns a list of tracks filtered by their IDs.
func (c *Client) GetMultipleTracks(ctx context.Context, ids []string) ([]Track, error) {
params := idListParams{
ids: strings.Join(ids, ","),
}

response, err := c.request(ctx, http.MethodGet, "/tracks", params)
if err != nil {
return nil, fmt.Errorf("failed to connect to the multiple tracks endpoint: %w", err)
}

var results trackResults

err = json.Unmarshal(response, &results)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal the multiple tracks response body: %w", err)
}

return results.Data, nil
}
161 changes: 151 additions & 10 deletions album_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"testing"
)

const countryCode = "AU"

func TestGetSingleAlbum(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -42,6 +44,24 @@ func TestGetSingleAlbum(t *testing.T) {
expected expected
wantErr bool
}{
{
"Token Error",
args{
httpClient: &mockHTTPClient{FilePath: "testdata/401-token-error.json", StatusCode: http.StatusUnauthorized},
ID: "51584178",
},
expected{},
true,
},
{
"Missing ID",
args{
httpClient: &mockHTTPClient{FilePath: "testdata/401-token-error.json", StatusCode: http.StatusBadRequest},
ID: "",
},
expected{},
true,
},
{
"Single album parses correctly",
args{
Expand Down Expand Up @@ -76,11 +96,20 @@ func TestGetSingleAlbum(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

client := &Client{httpClient: tt.args.httpClient}
client := &Client{httpClient: tt.args.httpClient, CountryCode: countryCode}

album, err := client.GetSingleAlbum(context.Background(), tt.args.ID)
if (err != nil) != tt.wantErr {
t.Errorf("Client.GetSingleAlbum() error = %v, wantErr %v", err, tt.wantErr)
if err == nil && tt.wantErr {
t.Error("Client.GetSingleAlbum() expected error")
return
}

if err != nil && tt.wantErr {
return
}

if err != nil && !tt.wantErr {
t.Errorf("Client.GetTracksByISRC() error = %v", err)
return
}

Expand Down Expand Up @@ -196,7 +225,8 @@ func TestGetAlbumTracks(t *testing.T) {
t.Parallel()

c := &Client{
httpClient: tt.args.httpClient,
CountryCode: countryCode,
httpClient: tt.args.httpClient,
}

tracks, err := c.GetAlbumTracks(context.Background(), tt.args.id)
Expand Down Expand Up @@ -250,7 +280,8 @@ func TestGetSingleTrack(t *testing.T) {
t.Parallel()

c := &Client{
httpClient: tt.args.httpClient,
CountryCode: countryCode,
httpClient: tt.args.httpClient,
}

track, err := c.GetSingleTrack(context.Background(), tt.args.id)
Expand Down Expand Up @@ -287,10 +318,33 @@ func TestGetTracksByISRC(t *testing.T) {
}

tests := []struct {
name string
args args
expected expected
name string
args args
expected expected
expectedError bool
}{
{
"Token Error",
args{
httpClient: &mockHTTPClient{FilePath: "testdata/401-token-error.json", StatusCode: http.StatusUnauthorized},
id: "51584179",
},
expected{
count: 0,
},
true,
},
{
"Bad Response",
args{
httpClient: &mockHTTPClient{FilePath: "testdata/invalid-json.json", StatusCode: http.StatusInternalServerError},
id: "51584179",
},
expected{
count: 0,
},
true,
},
{
"Count of tracks by ISRC",
args{
Expand All @@ -300,6 +354,7 @@ func TestGetTracksByISRC(t *testing.T) {
expected{
count: 2,
},
false,
},
}
for _, tt := range tests {
Expand All @@ -308,11 +363,17 @@ func TestGetTracksByISRC(t *testing.T) {
t.Parallel()

c := &Client{
httpClient: tt.args.httpClient,
CountryCode: countryCode,
httpClient: tt.args.httpClient,
}

tracks, err := c.GetTracksByISRC(context.Background(), tt.args.id, PaginationParams{Limit: 5})
if err != nil {
if err == nil && tt.expectedError {
t.Error("Client.GetTracksByISRC() expected an error", err)
return
}

if err != nil && !tt.expectedError {
t.Errorf("Client.GetTracksByISRC() error = %v", err)
return
}
Expand All @@ -323,3 +384,83 @@ func TestGetTracksByISRC(t *testing.T) {
})
}
}

func TestGetMultipleTracks(t *testing.T) {
t.Parallel()

type args struct {
httpClient HTTPClient
ids string
}

type expected struct {
count int
}

tests := []struct {
name string
args args
expected expected
expectedError bool
}{
{
"Token Error",
args{
httpClient: &mockHTTPClient{FilePath: "testdata/401-token-error.json", StatusCode: http.StatusUnauthorized},
ids: "251380837,251380838",
},
expected{
count: 0,
},
true,
},
{
"Bad Response",
args{
httpClient: &mockHTTPClient{FilePath: "testdata/invalid-json.json", StatusCode: http.StatusInternalServerError},
ids: "251380837,251380838",
},
expected{
count: 0,
},
true,
},
{
"Count of tracks by ISRC",
args{
httpClient: &mockHTTPClient{FilePath: "testdata/multiple-tracks.json", StatusCode: http.StatusOK},
ids: "251380837,251380838",
},
expected{
count: 2,
},
false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

c := &Client{
CountryCode: countryCode,
httpClient: tt.args.httpClient,
}

tracks, err := c.GetMultipleTracks(context.Background(), []string{})
if err == nil && tt.expectedError {
t.Error("Client.GetMultipleTracks() expected an error", err)
return
}

if err != nil && !tt.expectedError {
t.Errorf("Client.GetMultipleTracks() error = %v", err)
return
}

if len(tracks) != tt.expected.count {
t.Errorf("Client.GetMultipleTracks() artist count %v, want %v", len(tracks), tt.expected.count)
}
})
}
}
17 changes: 15 additions & 2 deletions examples/track/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,29 @@ func main() {

log.Printf("%s - %s", track.Title, track.Artists[0].Name)

log.Println("-------------------------------------------------")
log.Println("Tracks By ISRC")
log.Println("-------------------------------------------------")

tracks, err := client.GetTracksByISRC(ctx, "GBBLY1600675", gotidal.PaginationParams{Limit: 5})
if err != nil {
log.Fatal(err)
}

for _, track := range tracks {
log.Printf("%s - %s - %s", track.Title, track.Artists[0].Name, track.Album.Title)
}

log.Println("-------------------------------------------------")
log.Println("Tracks By ISRC")
log.Println("Multiple Tracks")
log.Println("-------------------------------------------------")

for _, track := range tracks {
multipleTracks, err := client.GetMultipleTracks(ctx, []string{"251380837", "251380838"})
if err != nil {
log.Fatal(err)
}

for _, track := range multipleTracks {
log.Printf("%s - %s - %s", track.Title, track.Artists[0].Name, track.Album.Title)
}
}
1 change: 1 addition & 0 deletions testdata/invalid-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{
Loading

0 comments on commit 8eb8653

Please sign in to comment.