Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial last.fm client implementation
- Loading branch information
Showing
7 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package lastfm | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
const ( | ||
apiBaseUrl = "https://ws.audioscrobbler.com/2.0/" | ||
) | ||
|
||
type HttpClient interface { | ||
Do(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
func NewClient(apiKey string, lang string, hc HttpClient) *Client { | ||
return &Client{apiKey, lang, hc} | ||
} | ||
|
||
type Client struct { | ||
apiKey string | ||
lang string | ||
hc HttpClient | ||
} | ||
|
||
// TODO SimilarArtists() | ||
func (c *Client) ArtistGetInfo(name string) (*Artist, error) { | ||
params := url.Values{} | ||
params.Add("method", "artist.getInfo") | ||
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() | ||
|
||
resp, err := c.hc.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
return nil, c.parseError(data) | ||
} | ||
|
||
var response Response | ||
err = json.Unmarshal(data, &response) | ||
return &response.Artist, err | ||
} | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package lastfm | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Client", func() { | ||
var httpClient *fakeHttpClient | ||
var client *Client | ||
|
||
BeforeEach(func() { | ||
httpClient = &fakeHttpClient{} | ||
client = NewClient("API_KEY", "pt", httpClient) | ||
}) | ||
|
||
Describe("ArtistInfo", 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} | ||
|
||
artist, err := client.ArtistGetInfo("U2") | ||
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&method=artist.getInfo")) | ||
}) | ||
|
||
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.ArtistGetInfo("U2") | ||
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.ArtistGetInfo("U2") | ||
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.ArtistGetInfo("U2") | ||
Expect(err).To(MatchError("invalid character '<' looking for beginning of value")) | ||
}) | ||
|
||
}) | ||
}) | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package lastfm | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/deluan/navidrome/log" | ||
"github.com/deluan/navidrome/tests" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestLastFM(t *testing.T) { | ||
tests.Init(t, false) | ||
log.SetLevel(log.LevelCritical) | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "LastFM Test Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package lastfm | ||
|
||
type Response struct { | ||
Artist Artist `json:"artist"` | ||
} | ||
|
||
type Artist struct { | ||
Name string `json:"name"` | ||
MBID string `json:"mbid"` | ||
URL string `json:"url"` | ||
Image []ArtistImage `json:"image"` | ||
Streamable string `json:"streamable"` | ||
Stats struct { | ||
Listeners string `json:"listeners"` | ||
Plays string `json:"plays"` | ||
} `json:"stats"` | ||
Similar struct { | ||
Artists []Artist `json:"artist"` | ||
} `json:"similar"` | ||
Tags struct { | ||
Tag []ArtistTag `json:"tag"` | ||
} `json:"tags"` | ||
Bio ArtistBio `json:"bio"` | ||
} | ||
|
||
type ArtistImage struct { | ||
URL string `json:"#text"` | ||
Size string `json:"size"` | ||
} | ||
|
||
type ArtistTag struct { | ||
Name string `json:"name"` | ||
URL string `json:"url"` | ||
} | ||
|
||
type ArtistBio struct { | ||
Published string `json:"published"` | ||
Summary string `json:"summary"` | ||
Content string `json:"content"` | ||
} | ||
|
||
type Error struct { | ||
Code int `json:"error"` | ||
Message string `json:"message"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package lastfm | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("LastFM responses", func() { | ||
Describe("Artist", func() { | ||
It("parses the response correctly", func() { | ||
var resp Response | ||
body, _ := ioutil.ReadFile("tests/fixtures/lastfm.artist.getinfo.json") | ||
err := json.Unmarshal(body, &resp) | ||
Expect(err).To(BeNil()) | ||
|
||
Expect(resp.Artist.Name).To(Equal("U2")) | ||
Expect(resp.Artist.MBID).To(Equal("a3cb23fc-acd3-4ce0-8f36-1e5aa6a18432")) | ||
Expect(resp.Artist.URL).To(Equal("https://www.last.fm/music/U2")) | ||
Expect(resp.Artist.Bio.Summary).To(ContainSubstring("U2 é uma das mais importantes bandas de rock de todos os tempos")) | ||
|
||
similarArtists := []string{"Passengers", "INXS", "R.E.M.", "Simple Minds", "Bono"} | ||
for i, similar := range similarArtists { | ||
Expect(resp.Artist.Similar.Artists[i].Name).To(Equal(similar)) | ||
} | ||
}) | ||
}) | ||
|
||
Describe("Error", func() { | ||
It("parses the error response correctly", func() { | ||
var error Error | ||
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.Message).To(Equal("Invalid Method - No method with that name in this package")) | ||
}) | ||
}) | ||
}) |
Large diffs are not rendered by default.
Oops, something went wrong.