Skip to content

Commit

Permalink
Add similar artists
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjowitt committed Feb 28, 2024
1 parent 68c3016 commit a86a280
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
33 changes: 33 additions & 0 deletions artist.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,36 @@ func (c *Client) GetMultipleArtists(ctx context.Context, ids []string) ([]Artist

return results.Data, nil
}

type similarArtist struct {
Resource struct {
ID string `json:"id"`
}
}

type similarArtistResults struct {
Data []similarArtist `json:"data"`
MetaData ItemMetaData `json:"metadata"`
}

// GetSimilarArtists returns a slice of artist IDs that can be used as a parameter in the GetMultipleArtists function.
func (c *Client) GetSimilarArtists(ctx context.Context, id string, params PaginationParams) ([]string, error) {
response, err := c.request(ctx, http.MethodGet, concat("/artists/", id, "/similar"), params)
if err != nil {
return nil, fmt.Errorf("failed to connect to the similar artists endpoint: %w", err)

Check warning on line 110 in artist.go

View check run for this annotation

Codecov / codecov/patch

artist.go#L110

Added line #L110 was not covered by tests
}

var results similarArtistResults

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

Check warning on line 117 in artist.go

View check run for this annotation

Codecov / codecov/patch

artist.go#L117

Added line #L117 was not covered by tests
}

var artistIDs []string
for _, artistID := range results.Data {
artistIDs = append(artistIDs, artistID.Resource.ID)
}

return artistIDs, nil
}
50 changes: 50 additions & 0 deletions artist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,53 @@ func TestGetMultipleArtists(t *testing.T) {
})
}
}

func TestSimilarArtists(t *testing.T) {

Check failure on line 177 in artist_test.go

View workflow job for this annotation

GitHub Actions / lint

177-225 lines are duplicate of `artist_test.go:77-125` (dupl)
t.Parallel()

type args struct {
httpClient HTTPClient
id string
}

type expected struct {
ArtistCount int
}

tests := []struct {
name string
args args
expected expected
wantErr bool
}{
{
"Multiple artists parses correctly",
args{
httpClient: &mockHTTPClient{FilePath: "testdata/similar-artists.json", StatusCode: http.StatusOK},
id: "3566512",
},
expected{
ArtistCount: 10,
},
false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

client := &Client{httpClient: tt.args.httpClient}

artists, err := client.GetSimilarArtists(context.Background(), tt.args.id, PaginationParams{Limit: 10})
if (err != nil) != tt.wantErr {
t.Errorf("Client.GetSimilarArtists() error = %v, wantErr %v", err, tt.wantErr)
return
}

if len(artists) != tt.expected.ArtistCount {
t.Errorf("Client.GetSimilarArtists() ArtistCount = %v, want %v", len(artists), tt.expected.ArtistCount)
}
})
}
}
18 changes: 18 additions & 0 deletions examples/artist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,22 @@ func main() {
for _, artist := range artists {
log.Printf("%s - %s", artist.Name, artist.URL)
}

log.Println("-------------------------------------------------")
log.Println("Get similar artists to Square Pusher")
log.Println(" ")

similarIDs, err := client.GetSimilarArtists(ctx, "3566512", gotidal.PaginationParams{Limit: 5})
if err != nil {
log.Fatal(err)
}

similarArtists, err := client.GetMultipleArtists(ctx, similarIDs)
if err != nil {
log.Fatal(err)
}

for _, item := range similarArtists {
log.Printf("%s - %s", item.Name, item.URL)
}
}
57 changes: 57 additions & 0 deletions testdata/similar-artists.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"data": [
{
"resource": {
"id": "3637817"
}
},
{
"resource": {
"id": "3689398"
}
},
{
"resource": {
"id": "3568424"
}
},
{
"resource": {
"id": "9164136"
}
},
{
"resource": {
"id": "3563592"
}
},
{
"resource": {
"id": "3569016"
}
},
{
"resource": {
"id": "4202231"
}
},
{
"resource": {
"id": "7266"
}
},
{
"resource": {
"id": "3513893"
}
},
{
"resource": {
"id": "4193227"
}
}
],
"metadata": {
"total": 10
}
}

0 comments on commit a86a280

Please sign in to comment.