-
Notifications
You must be signed in to change notification settings - Fork 0
/
artist.go
126 lines (99 loc) · 3.22 KB
/
artist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package gotidal
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
)
// Artist represents an individual artist.
type Artist struct {
artistResource `json:"resource"`
}
type artistResource struct {
ID string `json:"id"`
Name string `json:"name"`
Picture []Image `json:"picture"`
URL string `json:"tidalURL"`
Main bool `json:"main"`
}
// GetSingleArtist returns an artist that matches an ID.
func (c *Client) GetSingleArtist(ctx context.Context, id string) (*Artist, error) {
if id == "" {
return nil, ErrMissingRequiredParameters
}
response, err := c.request(ctx, http.MethodGet, concat("/artists/", id), nil)
if err != nil {
return nil, fmt.Errorf("failed to connect to the artists endpoint: %w", err)
}
var result Artist
err = json.Unmarshal(response, &result)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal the artist response body: %w", err)
}
return &result, nil
}
// GetAlbumsByArtist returns a paginated list of albums for an artist.
func (c *Client) GetAlbumsByArtist(ctx context.Context, id string, params PaginationParams) ([]Album, error) {
if id == "" {
return nil, ErrMissingRequiredParameters
}
response, err := c.request(ctx, http.MethodGet, concat("/artists/", id, "/albums"), params)
if err != nil {
return nil, fmt.Errorf("failed to connect to the artist albums endpoint: %w", err)
}
var results albumResults
err = json.Unmarshal(response, &results)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal the artist albums response body: %w", err)
}
return results.Data, nil
}
type artistResults struct {
Data []Artist `json:"data"`
}
// GetMultipleArtists returns a list of artists filtered by their IDs.
func (c *Client) GetMultipleArtists(ctx context.Context, ids []string) ([]Artist, error) {
type multiArtistParams struct {
ids string
}
params := multiArtistParams{
ids: strings.Join(ids, ","),
}
response, err := c.request(ctx, http.MethodGet, "/artists", params)
if err != nil {
return nil, fmt.Errorf("failed to connect to the multiple artists endpoint: %w", err)
}
var results artistResults
err = json.Unmarshal(response, &results)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal the multiple artists response body: %w", err)
}
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)
}
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)
}
var artistIDs []string
for _, artistID := range results.Data {
artistIDs = append(artistIDs, artistID.Resource.ID)
}
return artistIDs, nil
}