Get better artist images results

This commit is contained in:
Deluan
2020-10-20 15:31:49 -04:00
committed by Deluan Quintão
parent 40fd5bab34
commit a257891b46
7 changed files with 56 additions and 17 deletions
+3 -3
View File
@@ -35,7 +35,7 @@ type Client struct {
hc HttpClient
}
func (c *Client) ArtistImages(ctx context.Context, name string) ([]Image, error) {
func (c *Client) SearchArtists(ctx context.Context, name string, limit int) ([]Artist, error) {
token, err := c.authorize(ctx)
if err != nil {
return nil, err
@@ -45,7 +45,7 @@ func (c *Client) ArtistImages(ctx context.Context, name string) ([]Image, error)
params.Add("type", "artist")
params.Add("q", name)
params.Add("offset", "0")
params.Add("limit", "1")
params.Add("limit", strconv.Itoa(limit))
req, _ := http.NewRequest("GET", apiBaseUrl+"search", nil)
req.URL.RawQuery = params.Encode()
req.Header.Add("Authorization", "Bearer "+token)
@@ -60,7 +60,7 @@ func (c *Client) ArtistImages(ctx context.Context, name string) ([]Image, error)
return nil, ErrNotFound
}
log.Debug(ctx, "Found artist in Spotify", "artist", results.Artists.Items[0].Name)
return results.Artists.Items[0].Images, err
return results.Artists.Items, err
}
func (c *Client) authorize(ctx context.Context) (string, error) {
+7 -3
View File
@@ -29,8 +29,12 @@ var _ = Describe("Client", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(`{"access_token": "NEW_ACCESS_TOKEN","token_type": "Bearer","expires_in": 3600}`)),
})
images, err := client.ArtistImages(context.TODO(), "U2")
artists, err := client.SearchArtists(context.TODO(), "U2", 10)
Expect(err).To(BeNil())
Expect(artists).To(HaveLen(20))
Expect(artists[0].Popularity).To(Equal(82))
images := artists[0].Images
Expect(images).To(HaveLen(3))
Expect(images[0].Width).To(Equal(640))
Expect(images[1].Width).To(Equal(320))
@@ -51,7 +55,7 @@ var _ = Describe("Client", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(`{"access_token": "NEW_ACCESS_TOKEN","token_type": "Bearer","expires_in": 3600}`)),
})
_, err := client.ArtistImages(context.TODO(), "U2")
_, err := client.SearchArtists(context.TODO(), "U2", 10)
Expect(err).To(MatchError(ErrNotFound))
})
@@ -63,7 +67,7 @@ var _ = Describe("Client", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":"invalid_client","error_description":"Invalid client"}`)),
})
_, err := client.ArtistImages(context.TODO(), "U2")
_, err := client.SearchArtists(context.TODO(), "U2", 10)
Expect(err).To(MatchError("spotify error(invalid_client): Invalid client"))
})
})
+6 -5
View File
@@ -10,11 +10,12 @@ type ArtistsResult struct {
}
type Artist struct {
Genres []string `json:"genres"`
HRef string `json:"href"`
ID string `json:"id"`
Images []Image `json:"images"`
Name string `json:"name"`
Genres []string `json:"genres"`
HRef string `json:"href"`
ID string `json:"id"`
Popularity int `json:"popularity"`
Images []Image `json:"images"`
Name string `json:"name"`
}
type Image struct {