Get images
This commit is contained in:
@@ -87,7 +87,12 @@ func (c *CachedHTTPClient) deserializeReq(reqStr string) (*http.Request, error)
|
||||
bodyStr, _ := base64.StdEncoding.DecodeString(*data.Body)
|
||||
body = strings.NewReader(string(bodyStr))
|
||||
}
|
||||
return http.NewRequest(data.Method, data.URL, body)
|
||||
req, err := http.NewRequest(data.Method, data.URL, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header = data.Header
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *CachedHTTPClient) serializeResponse(resp *http.Response) string {
|
||||
|
||||
@@ -13,14 +13,16 @@ import (
|
||||
)
|
||||
|
||||
var _ = Describe("CachedHttpClient", func() {
|
||||
Context("Default TTL", func() {
|
||||
Context("GET", func() {
|
||||
var chc *CachedHTTPClient
|
||||
var ts *httptest.Server
|
||||
var requestsReceived int
|
||||
var header string
|
||||
|
||||
BeforeEach(func() {
|
||||
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestsReceived++
|
||||
header = r.Header.Get("head")
|
||||
_, _ = fmt.Fprintf(w, "Hello, %s", r.URL.Query()["name"])
|
||||
}))
|
||||
chc = NewCachedHTTPClient(http.DefaultClient, consts.DefaultCachedHttpClientTTL)
|
||||
@@ -56,6 +58,17 @@ var _ = Describe("CachedHttpClient", func() {
|
||||
Expect(err).To(BeNil())
|
||||
Expect(string(body)).To(Equal("Hello, []"))
|
||||
Expect(requestsReceived).To(Equal(2))
|
||||
|
||||
// Different again (same as before, but with header)
|
||||
r, _ = http.NewRequest("GET", ts.URL, nil)
|
||||
r.Header.Add("head", "this is a header")
|
||||
resp, err = chc.Do(r)
|
||||
Expect(err).To(BeNil())
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(string(body)).To(Equal("Hello, []"))
|
||||
Expect(string(header)).To(Equal("this is a header"))
|
||||
Expect(requestsReceived).To(Equal(3))
|
||||
})
|
||||
|
||||
It("expires responses after TTL", func() {
|
||||
@@ -77,5 +90,4 @@ var _ = Describe("CachedHttpClient", func() {
|
||||
Expect(requestsReceived).To(Equal(2))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
|
||||
type Constructor func(ctx context.Context) Interface
|
||||
|
||||
type Interface interface{}
|
||||
type Interface interface {
|
||||
AgentName() string
|
||||
}
|
||||
|
||||
type Artist struct {
|
||||
Name string
|
||||
|
||||
@@ -32,6 +32,10 @@ func lastFMConstructor(ctx context.Context) Interface {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *lastfmAgent) AgentName() string {
|
||||
return "lastfm"
|
||||
}
|
||||
|
||||
func (l *lastfmAgent) GetMBID(name string) (string, error) {
|
||||
a, err := l.callArtistGetInfo(name, "")
|
||||
if err != nil {
|
||||
|
||||
@@ -37,6 +37,10 @@ func spotifyConstructor(ctx context.Context) Interface {
|
||||
return l
|
||||
}
|
||||
|
||||
func (s *spotifyAgent) AgentName() string {
|
||||
return "spotify"
|
||||
}
|
||||
|
||||
func (s *spotifyAgent) GetImages(name, mbid string) ([]ArtistImage, error) {
|
||||
a, err := s.searchArtist(name)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user