Retry calls to Last.FM without MBIDs when if returns artist invalid (#1138)

* Call Last.FM's getInfo again without mbid when artist is not found

* Call Last.FM's getSimilar again without mbid when artist is not found

* Call Last.FM's getTopTracks again without mbid when artist is not found
This commit is contained in:
Deluan Quintão
2021-05-27 20:53:24 -04:00
committed by GitHub
parent 4e0177ee53
commit 89b12b34be
12 changed files with 288 additions and 112 deletions
+26 -18
View File
@@ -14,6 +14,15 @@ const (
apiBaseUrl = "https://ws.audioscrobbler.com/2.0/"
)
type Error struct {
Code int
Message string
}
func (e *Error) Error() string {
return fmt.Sprintf("last.fm error(%d): %s", e.Code, e.Message)
}
type httpDoer interface {
Do(req *http.Request) (*http.Response, error)
}
@@ -46,14 +55,22 @@ func (c *Client) makeRequest(params url.Values) (*Response, error) {
return nil, err
}
if resp.StatusCode != 200 {
return nil, c.parseError(data)
var response Response
jsonErr := json.Unmarshal(data, &response)
if resp.StatusCode != 200 && jsonErr != nil {
return nil, fmt.Errorf("last.fm http status: (%d)", resp.StatusCode)
}
var response Response
err = json.Unmarshal(data, &response)
if jsonErr != nil {
return nil, jsonErr
}
return &response, err
if response.Error != 0 {
return &response, &Error{Code: response.Error, Message: response.Message}
}
return &response, nil
}
func (c *Client) ArtistGetInfo(ctx context.Context, name string, mbid string) (*Artist, error) {
@@ -69,7 +86,7 @@ func (c *Client) ArtistGetInfo(ctx context.Context, name string, mbid string) (*
return &response.Artist, nil
}
func (c *Client) ArtistGetSimilar(ctx context.Context, name string, mbid string, limit int) ([]Artist, error) {
func (c *Client) ArtistGetSimilar(ctx context.Context, name string, mbid string, limit int) (*SimilarArtists, error) {
params := url.Values{}
params.Add("method", "artist.getSimilar")
params.Add("artist", name)
@@ -79,10 +96,10 @@ func (c *Client) ArtistGetSimilar(ctx context.Context, name string, mbid string,
if err != nil {
return nil, err
}
return response.SimilarArtists.Artists, nil
return &response.SimilarArtists, nil
}
func (c *Client) ArtistGetTopTracks(ctx context.Context, name string, mbid string, limit int) ([]Track, error) {
func (c *Client) ArtistGetTopTracks(ctx context.Context, name string, mbid string, limit int) (*TopTracks, error) {
params := url.Values{}
params.Add("method", "artist.getTopTracks")
params.Add("artist", name)
@@ -92,14 +109,5 @@ func (c *Client) ArtistGetTopTracks(ctx context.Context, name string, mbid strin
if err != nil {
return nil, err
}
return response.TopTracks.Track, nil
}
func (c *Client) parseError(data []byte) error {
var e Error
err := json.Unmarshal(data, &e)
if err != nil {
return err
}
return fmt.Errorf("last.fm error(%d): %s", e.Code, e.Message)
return &response.TopTracks, nil
}
+39 -85
View File
@@ -8,49 +8,71 @@ import (
"net/http"
"os"
"github.com/navidrome/navidrome/tests"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Client", func() {
var httpClient *fakeHttpClient
var httpClient *tests.FakeHttpClient
var client *Client
BeforeEach(func() {
httpClient = &fakeHttpClient{}
httpClient = &tests.FakeHttpClient{}
client = NewClient("API_KEY", "pt", httpClient)
})
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}
httpClient.Res = http.Response{Body: f, StatusCode: 200}
artist, err := client.ArtistGetInfo(context.TODO(), "U2", "123")
Expect(err).To(BeNil())
Expect(artist.Name).To(Equal("U2"))
Expect(httpClient.savedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&lang=pt&mbid=123&method=artist.getInfo"))
Expect(httpClient.SavedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&lang=pt&mbid=123&method=artist.getInfo"))
})
It("fails if Last.FM returns an error", func() {
httpClient.res = http.Response{
It("fails if Last.FM returns an http status != 200", func() {
httpClient.Res = http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString(`Internal Server Error`)),
StatusCode: 500,
}
_, err := client.ArtistGetInfo(context.TODO(), "U2", "123")
Expect(err).To(MatchError("last.fm http status: (500)"))
})
It("fails if Last.FM returns an http status != 200", 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.ArtistGetInfo(context.TODO(), "U2", "123")
Expect(err).To(MatchError("last.fm error(3): Invalid Method - No method with that name in this package"))
Expect(err).To(MatchError(&Error{Code: 3, Message: "Invalid Method - No method with that name in this package"}))
})
It("fails if Last.FM returns an error", func() {
httpClient.Res = http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":6,"message":"The artist you supplied could not be found"}`)),
StatusCode: 200,
}
_, err := client.ArtistGetInfo(context.TODO(), "U2", "123")
Expect(err).To(MatchError(&Error{Code: 6, Message: "The artist you supplied could not be found"}))
})
It("fails if HttpClient.Do() returns error", func() {
httpClient.err = errors.New("generic error")
httpClient.Err = errors.New("generic error")
_, err := client.ArtistGetInfo(context.TODO(), "U2", "123")
Expect(err).To(MatchError("generic error"))
})
It("fails if returned body is not a valid JSON", func() {
httpClient.res = http.Response{
httpClient.Res = http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString(`<xml>NOT_VALID_JSON</xml>`)),
StatusCode: 200,
}
@@ -64,92 +86,24 @@ 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}
httpClient.Res = http.Response{Body: f, StatusCode: 200}
artists, err := client.ArtistGetSimilar(context.TODO(), "U2", "123", 2)
similar, err := client.ArtistGetSimilar(context.TODO(), "U2", "123", 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&mbid=123&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", "123", 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", "123", 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", "123", 2)
Expect(err).To(MatchError("invalid character '<' looking for beginning of value"))
Expect(len(similar.Artists)).To(Equal(2))
Expect(httpClient.SavedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&limit=2&mbid=123&method=artist.getSimilar"))
})
})
Describe("ArtistGetTopTracks", func() {
It("returns top tracks for a successful response", func() {
f, _ := os.Open("tests/fixtures/lastfm.artist.gettoptracks.json")
httpClient.res = http.Response{Body: f, StatusCode: 200}
httpClient.Res = http.Response{Body: f, StatusCode: 200}
tracks, err := client.ArtistGetTopTracks(context.TODO(), "U2", "123", 2)
top, err := client.ArtistGetTopTracks(context.TODO(), "U2", "123", 2)
Expect(err).To(BeNil())
Expect(len(tracks)).To(Equal(2))
Expect(httpClient.savedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&limit=2&mbid=123&method=artist.getTopTracks"))
})
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.ArtistGetTopTracks(context.TODO(), "U2", "123", 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.ArtistGetTopTracks(context.TODO(), "U2", "123", 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.ArtistGetTopTracks(context.TODO(), "U2", "123", 2)
Expect(err).To(MatchError("invalid character '<' looking for beginning of value"))
Expect(len(top.Track)).To(Equal(2))
Expect(httpClient.SavedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&limit=2&mbid=123&method=artist.getTopTracks"))
})
})
})
type fakeHttpClient struct {
res http.Response
err error
savedRequest *http.Request
}
func (c *fakeHttpClient) Do(req *http.Request) (*http.Response, error) {
c.savedRequest = req
if c.err != nil {
return nil, c.err
}
return &c.res, nil
}
+8 -5
View File
@@ -4,6 +4,8 @@ type Response struct {
Artist Artist `json:"artist"`
SimilarArtists SimilarArtists `json:"similarartists"`
TopTracks TopTracks `json:"toptracks"`
Error int `json:"error"`
Message string `json:"message"`
}
type Artist struct {
@@ -25,6 +27,11 @@ type Artist struct {
type SimilarArtists struct {
Artists []Artist `json:"artist"`
Attr Attr `json:"@attr"`
}
type Attr struct {
Artist string `json:"artist"`
}
type ArtistImage struct {
@@ -50,9 +57,5 @@ type Track struct {
type TopTracks struct {
Track []Track `json:"track"`
}
type Error struct {
Code int `json:"error"`
Message string `json:"message"`
Attr Attr `json:"@attr"`
}
+2 -2
View File
@@ -58,12 +58,12 @@ var _ = Describe("LastFM responses", func() {
Describe("Error", func() {
It("parses the error response correctly", func() {
var error Error
var error Response
body := []byte(`{"error":3,"message":"Invalid Method - No method with that name in this package"}`)
err := json.Unmarshal(body, &error)
Expect(err).To(BeNil())
Expect(error.Code).To(Equal(3))
Expect(error.Error).To(Equal(3))
Expect(error.Message).To(Equal("Invalid Method - No method with that name in this package"))
})
})