Add dedicated SimilarArtists call
This commit is contained in:
+29
-7
@@ -7,6 +7,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -27,14 +28,10 @@ type Client struct {
|
||||
hc HttpClient
|
||||
}
|
||||
|
||||
// TODO SimilarArtists()
|
||||
func (c *Client) ArtistGetInfo(ctx context.Context, name string) (*Artist, error) {
|
||||
params := url.Values{}
|
||||
params.Add("method", "artist.getInfo")
|
||||
func (c *Client) makeRequest(params url.Values) (*Response, error) {
|
||||
params.Add("format", "json")
|
||||
params.Add("api_key", c.apiKey)
|
||||
params.Add("artist", name)
|
||||
params.Add("lang", c.lang)
|
||||
|
||||
req, _ := http.NewRequest("GET", apiBaseUrl, nil)
|
||||
req.URL.RawQuery = params.Encode()
|
||||
|
||||
@@ -55,7 +52,32 @@ func (c *Client) ArtistGetInfo(ctx context.Context, name string) (*Artist, error
|
||||
|
||||
var response Response
|
||||
err = json.Unmarshal(data, &response)
|
||||
return &response.Artist, err
|
||||
|
||||
return &response, err
|
||||
}
|
||||
|
||||
func (c *Client) ArtistGetInfo(ctx context.Context, name string) (*Artist, error) {
|
||||
params := url.Values{}
|
||||
params.Add("method", "artist.getInfo")
|
||||
params.Add("artist", name)
|
||||
params.Add("lang", c.lang)
|
||||
response, err := c.makeRequest(params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &response.Artist, nil
|
||||
}
|
||||
|
||||
func (c *Client) ArtistGetSimilar(ctx context.Context, name string, limit int) ([]Artist, error) {
|
||||
params := url.Values{}
|
||||
params.Add("method", "artist.getSimilar")
|
||||
params.Add("artist", name)
|
||||
params.Add("limit", strconv.Itoa(limit))
|
||||
response, err := c.makeRequest(params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response.SimilarArtists.Artists, nil
|
||||
}
|
||||
|
||||
func (c *Client) parseError(data []byte) error {
|
||||
|
||||
@@ -21,7 +21,7 @@ var _ = Describe("Client", func() {
|
||||
client = NewClient("API_KEY", "pt", httpClient)
|
||||
})
|
||||
|
||||
Describe("ArtistInfo", func() {
|
||||
Describe("ArtistGetInfo", func() {
|
||||
It("returns an artist for a successful response", func() {
|
||||
f, _ := os.Open("tests/fixtures/lastfm.artist.getinfo.json")
|
||||
httpClient.res = http.Response{Body: f, StatusCode: 200}
|
||||
@@ -60,6 +60,46 @@ var _ = Describe("Client", func() {
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Describe("ArtistGetSimilar", func() {
|
||||
It("returns an artist for a successful response", func() {
|
||||
f, _ := os.Open("tests/fixtures/lastfm.artist.getsimilar.json")
|
||||
httpClient.res = http.Response{Body: f, StatusCode: 200}
|
||||
|
||||
artists, err := client.ArtistGetSimilar(context.TODO(), "U2", 2)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(len(artists)).To(Equal(2))
|
||||
Expect(httpClient.savedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&limit=2&method=artist.getSimilar"))
|
||||
})
|
||||
|
||||
It("fails if Last.FM returns an error", func() {
|
||||
httpClient.res = http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":3,"message":"Invalid Method - No method with that name in this package"}`)),
|
||||
StatusCode: 400,
|
||||
}
|
||||
|
||||
_, err := client.ArtistGetSimilar(context.TODO(), "U2", 2)
|
||||
Expect(err).To(MatchError("last.fm error(3): Invalid Method - No method with that name in this package"))
|
||||
})
|
||||
|
||||
It("fails if HttpClient.Do() returns error", func() {
|
||||
httpClient.err = errors.New("generic error")
|
||||
|
||||
_, err := client.ArtistGetSimilar(context.TODO(), "U2", 2)
|
||||
Expect(err).To(MatchError("generic error"))
|
||||
})
|
||||
|
||||
It("fails if returned body is not a valid JSON", func() {
|
||||
httpClient.res = http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`<xml>NOT_VALID_JSON</xml>`)),
|
||||
StatusCode: 200,
|
||||
}
|
||||
|
||||
_, err := client.ArtistGetSimilar(context.TODO(), "U2", 2)
|
||||
Expect(err).To(MatchError("invalid character '<' looking for beginning of value"))
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
type fakeHttpClient struct {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package lastfm
|
||||
|
||||
type Response struct {
|
||||
Artist Artist `json:"artist"`
|
||||
Artist Artist `json:"artist"`
|
||||
SimilarArtists SimilarArtists `json:"similarartists"`
|
||||
}
|
||||
|
||||
type Artist struct {
|
||||
@@ -14,15 +15,17 @@ type Artist struct {
|
||||
Listeners string `json:"listeners"`
|
||||
Plays string `json:"plays"`
|
||||
} `json:"stats"`
|
||||
Similar struct {
|
||||
Artists []Artist `json:"artist"`
|
||||
} `json:"similar"`
|
||||
Tags struct {
|
||||
Similar SimilarArtists `json:"similar"`
|
||||
Tags struct {
|
||||
Tag []ArtistTag `json:"tag"`
|
||||
} `json:"tags"`
|
||||
Bio ArtistBio `json:"bio"`
|
||||
}
|
||||
|
||||
type SimilarArtists struct {
|
||||
Artists []Artist `json:"artist"`
|
||||
}
|
||||
|
||||
type ArtistImage struct {
|
||||
URL string `json:"#text"`
|
||||
Size string `json:"size"`
|
||||
|
||||
@@ -28,6 +28,19 @@ var _ = Describe("LastFM responses", func() {
|
||||
})
|
||||
})
|
||||
|
||||
Describe("SimilarArtists", func() {
|
||||
It("parses the response correctly", func() {
|
||||
var resp Response
|
||||
body, _ := ioutil.ReadFile("tests/fixtures/lastfm.artist.getsimilar.json")
|
||||
err := json.Unmarshal(body, &resp)
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
Expect(resp.SimilarArtists.Artists).To(HaveLen(2))
|
||||
Expect(resp.SimilarArtists.Artists[0].Name).To(Equal("Passengers"))
|
||||
Expect(resp.SimilarArtists.Artists[1].Name).To(Equal("INXS"))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Error", func() {
|
||||
It("parses the error response correctly", func() {
|
||||
var error Error
|
||||
|
||||
Reference in New Issue
Block a user