Add ExternalInformation core service (not a great name, I know)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package spotify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -34,8 +35,8 @@ type Client struct {
|
||||
hc HttpClient
|
||||
}
|
||||
|
||||
func (c *Client) ArtistImages(name string) ([]Image, error) {
|
||||
token, err := c.authorize()
|
||||
func (c *Client) ArtistImages(ctx context.Context, name string) ([]Image, error) {
|
||||
token, err := c.authorize(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -58,12 +59,13 @@ func (c *Client) ArtistImages(name string) ([]Image, error) {
|
||||
if len(results.Artists.Items) == 0 {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
log.Debug(ctx, "Found artist in Spotify", "artist", results.Artists.Items[0].Name)
|
||||
return results.Artists.Items[0].Images, err
|
||||
}
|
||||
|
||||
func (c *Client) authorize() (string, error) {
|
||||
func (c *Client) authorize(ctx context.Context) (string, error) {
|
||||
payload := url.Values{}
|
||||
payload.Add("grant_type", "client_credentials.getInfo")
|
||||
payload.Add("grant_type", "client_credentials")
|
||||
|
||||
req, _ := http.NewRequest("POST", "https://accounts.spotify.com/api/token", strings.NewReader(payload.Encode()))
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
@@ -80,7 +82,7 @@ func (c *Client) authorize() (string, error) {
|
||||
if v, ok := response["access_token"]; ok {
|
||||
return v.(string), nil
|
||||
}
|
||||
log.Error("Invalid spotify response", "resp", response)
|
||||
log.Error(ctx, "Invalid spotify response", "resp", response)
|
||||
return "", errors.New("invalid response")
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package spotify
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -28,7 +29,7 @@ var _ = Describe("Client", func() {
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"access_token": "NEW_ACCESS_TOKEN","token_type": "Bearer","expires_in": 3600}`)),
|
||||
})
|
||||
|
||||
images, err := client.ArtistImages("U2")
|
||||
images, err := client.ArtistImages(context.TODO(), "U2")
|
||||
Expect(err).To(BeNil())
|
||||
Expect(images).To(HaveLen(3))
|
||||
Expect(images[0].Width).To(Equal(640))
|
||||
@@ -50,7 +51,7 @@ var _ = Describe("Client", func() {
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"access_token": "NEW_ACCESS_TOKEN","token_type": "Bearer","expires_in": 3600}`)),
|
||||
})
|
||||
|
||||
_, err := client.ArtistImages("U2")
|
||||
_, err := client.ArtistImages(context.TODO(), "U2")
|
||||
Expect(err).To(MatchError(ErrNotFound))
|
||||
})
|
||||
|
||||
@@ -62,7 +63,7 @@ var _ = Describe("Client", func() {
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":"invalid_client","error_description":"Invalid client"}`)),
|
||||
})
|
||||
|
||||
_, err := client.ArtistImages("U2")
|
||||
_, err := client.ArtistImages(context.TODO(), "U2")
|
||||
Expect(err).To(MatchError("spotify error(invalid_client): Invalid client"))
|
||||
})
|
||||
})
|
||||
@@ -74,7 +75,7 @@ var _ = Describe("Client", func() {
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"access_token": "NEW_ACCESS_TOKEN","token_type": "Bearer","expires_in": 3600}`)),
|
||||
})
|
||||
|
||||
token, err := client.authorize()
|
||||
token, err := client.authorize(nil)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(token).To(Equal("NEW_ACCESS_TOKEN"))
|
||||
auth := httpClient.lastRequest.Header.Get("Authorization")
|
||||
@@ -87,7 +88,7 @@ var _ = Describe("Client", func() {
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":"invalid_client","error_description":"Invalid client"}`)),
|
||||
})
|
||||
|
||||
_, err := client.authorize()
|
||||
_, err := client.authorize(nil)
|
||||
Expect(err).To(MatchError("spotify error(invalid_client): Invalid client"))
|
||||
})
|
||||
|
||||
@@ -97,7 +98,7 @@ var _ = Describe("Client", func() {
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{NOT_VALID}`)),
|
||||
})
|
||||
|
||||
_, err := client.authorize()
|
||||
_, err := client.authorize(nil)
|
||||
Expect(err).To(MatchError("invalid character 'N' looking for beginning of object key string"))
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user