Skip to content

Commit c44aaf9

Browse files
authored
Merge pull request #145 from zmb3/v2
V2
2 parents 1fa84a5 + ae714dd commit c44aaf9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1252
-1157
lines changed

.github/workflows/lint.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
jobs:
8+
golangci:
9+
name: lint
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: golangci-lint
14+
uses: golangci/golangci-lint-action@v2
15+
with:
16+
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
17+
version: v1.39

.github/workflows/test.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: test
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
jobs:
8+
unit:
9+
name: unit
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- uses: actions/setup-go@v2
14+
with:
15+
go-version: '1.16.3' # The Go version to download (if necessary) and use.
16+
- run: go test ./...

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
17+
.idea/

.travis.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

README.md

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ Spotify
33
=======
44

55
[![GoDoc](https://godoc.org/github.com/zmb3/spotify?status.svg)](http://godoc.org/github.com/zmb3/spotify)
6-
[![Build status](https://ci.appveyor.com/api/projects/status/1nr9vv0jqq438nj2?svg=true)](https://ci.appveyor.com/project/zmb3/spotify)
7-
[![Build Status](https://travis-ci.org/zmb3/spotify.svg)](https://travis-ci.org/zmb3/spotify)
86

97
This is a Go wrapper for working with Spotify's
108
[Web API](https://developer.spotify.com/web-api/).
@@ -19,7 +17,7 @@ By using this library you agree to Spotify's
1917

2018
To install the library, simply
2119

22-
`go get github.com/zmb3/spotify`
20+
`go get github.com/zmb3/spotify/v2`
2321

2422
## Authentication
2523

@@ -44,11 +42,7 @@ provide this data manually.
4442
````Go
4543
// the redirect URL must be an exact match of a URL you've registered for your application
4644
// scopes determine which permissions the user is prompted to authorize
47-
auth := spotify.NewAuthenticator(redirectURL, spotify.ScopeUserReadPrivate)
48-
49-
// if you didn't store your ID and secret key in the specified environment variables,
50-
// you can set them manually here
51-
auth.SetAuthInfo(clientID, secretKey)
45+
auth := spotifyauth.New(spotifyauth.WithRedirectURL(redirectURL), spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate))
5246

5347
// get the user to this URL - how you do that is up to you
5448
// you should specify a unique state string to identify the session
@@ -58,13 +52,13 @@ url := auth.AuthURL(state)
5852
// typically you'll have a handler set up like the following:
5953
func redirectHandler(w http.ResponseWriter, r *http.Request) {
6054
// use the same state string here that you used to generate the URL
61-
token, err := auth.Token(state, r)
55+
token, err := auth.Token(r.Context(), state, r)
6256
if err != nil {
6357
http.Error(w, "Couldn't get token", http.StatusNotFound)
6458
return
6559
}
6660
// create a client using the specified token
67-
client := auth.NewClient(token)
61+
client := spotify.New(auth.Client(r.Context(), token))
6862

6963
// the client can now be used to make authenticated requests
7064
}
@@ -81,14 +75,6 @@ https://godoc.org/golang.org/x/oauth2/google
8175

8276
## Helpful Hints
8377

84-
85-
### Optional Parameters
86-
87-
Many of the functions in this package come in two forms - a simple version that
88-
omits optional parameters and uses reasonable defaults, and a more sophisticated
89-
version that accepts additional parameters. The latter is suffixed with `Opt`
90-
to indicate that it accepts some optional parameters.
91-
9278
### Automatic Retries
9379

9480
The API will throttle your requests if you are sending them too rapidly.

album.go

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package spotify
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
6-
"net/url"
77
"strconv"
88
"strings"
99
"time"
@@ -102,21 +102,17 @@ type SavedAlbum struct {
102102
}
103103

104104
// GetAlbum gets Spotify catalog information for a single album, given its Spotify ID.
105-
func (c *Client) GetAlbum(id ID) (*FullAlbum, error) {
106-
return c.GetAlbumOpt(id, nil)
107-
}
108-
109-
// GetAlbum is like GetAlbumOpt but it accepts an additional country option for track relinking
110-
func (c *Client) GetAlbumOpt(id ID, opt *Options) (*FullAlbum, error) {
105+
// Supported options: Market
106+
func (c *Client) GetAlbum(ctx context.Context, id ID, opts ...RequestOption) (*FullAlbum, error) {
111107
spotifyURL := fmt.Sprintf("%salbums/%s", c.baseURL, id)
112108

113-
if opt != nil && opt.Country != nil {
114-
spotifyURL += "?market=" + *opt.Country
109+
if params := processOptions(opts...).urlParams.Encode(); params != "" {
110+
spotifyURL += "?" + params
115111
}
116112

117113
var a FullAlbum
118114

119-
err := c.get(spotifyURL, &a)
115+
err := c.get(ctx, spotifyURL, &a)
120116
if err != nil {
121117
return nil, err
122118
}
@@ -136,31 +132,24 @@ func toStringSlice(ids []ID) []string {
136132
// Spotify IDs. It supports up to 20 IDs in a single call. Albums are returned
137133
// in the order requested. If an album is not found, that position in the
138134
// result slice will be nil.
139-
func (c *Client) GetAlbums(ids ...ID) ([]*FullAlbum, error) {
140-
return c.GetAlbumsOpt(nil, ids...)
141-
}
142-
143-
// GetAlbumsOpt is like GetAlbums but it accepts an additional country option for track relinking
135+
//
144136
// Doc API: https://developer.spotify.com/documentation/web-api/reference/albums/get-several-albums/
145-
func (c *Client) GetAlbumsOpt(opt *Options, ids ...ID) ([]*FullAlbum, error) {
137+
//
138+
// Supported options: Market
139+
func (c *Client) GetAlbums(ctx context.Context, ids []ID, opts ...RequestOption) ([]*FullAlbum, error) {
146140
if len(ids) > 20 {
147141
return nil, errors.New("spotify: exceeded maximum number of albums")
148142
}
149-
150-
params := url.Values{}
143+
params := processOptions(opts...).urlParams
151144
params.Set("ids", strings.Join(toStringSlice(ids), ","))
152145

153-
if opt != nil && opt.Country != nil {
154-
params.Set("market", *opt.Country)
155-
}
156-
157146
spotifyURL := fmt.Sprintf("%salbums?%s", c.baseURL, params.Encode())
158147

159148
var a struct {
160149
Albums []*FullAlbum `json:"albums"`
161150
}
162151

163-
err := c.get(spotifyURL, &a)
152+
err := c.get(ctx, spotifyURL, &a)
164153
if err != nil {
165154
return nil, err
166155
}
@@ -202,39 +191,17 @@ func (at AlbumType) encode() string {
202191
// GetAlbumTracks gets the tracks for a particular album.
203192
// If you only care about the tracks, this call is more efficient
204193
// than GetAlbum.
205-
func (c *Client) GetAlbumTracks(id ID) (*SimpleTrackPage, error) {
206-
return c.GetAlbumTracksOpt(id, nil)
207-
}
208-
209-
// GetAlbumTracksOpt behaves like GetAlbumTracks, with the exception that it
210-
// allows you to specify options that limit the number of results returned and if
211-
// track relinking should be used.
212-
// The maximum number of results to return is specified by limit.
213-
// The offset argument can be used to specify the index of the first track to return.
214-
// It can be used along with limit to request the next set of results.
215-
// Track relinking can be enabled by setting the Country option
216-
func (c *Client) GetAlbumTracksOpt(id ID, opt *Options) (*SimpleTrackPage, error) {
194+
//
195+
// Supported Options: Market, Limit, Offset
196+
func (c *Client) GetAlbumTracks(ctx context.Context, id ID, opts ...RequestOption) (*SimpleTrackPage, error) {
217197
spotifyURL := fmt.Sprintf("%salbums/%s/tracks", c.baseURL, id)
218198

219-
if opt != nil {
220-
v := url.Values{}
221-
if opt.Limit != nil {
222-
v.Set("limit", strconv.Itoa(*opt.Limit))
223-
}
224-
if opt.Offset != nil {
225-
v.Set("offset", strconv.Itoa(*opt.Offset))
226-
}
227-
if opt.Country != nil {
228-
v.Set("market", *opt.Country)
229-
}
230-
optional := v.Encode()
231-
if optional != "" {
232-
spotifyURL += "?" + optional
233-
}
199+
if params := processOptions(opts...).urlParams.Encode(); params != "" {
200+
spotifyURL += "?" + params
234201
}
235202

236203
var result SimpleTrackPage
237-
err := c.get(spotifyURL, &result)
204+
err := c.get(ctx, spotifyURL, &result)
238205
if err != nil {
239206
return nil, err
240207
}

album_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package spotify
22

33
import (
4+
"context"
45
"net/http"
56
"testing"
67
)
@@ -10,7 +11,7 @@ func TestFindAlbum(t *testing.T) {
1011
client, server := testClientFile(http.StatusOK, "test_data/find_album.txt")
1112
defer server.Close()
1213

13-
album, err := client.GetAlbum(ID("0sNOF9WDwhWunNAHPD3Baj"))
14+
album, err := client.GetAlbum(context.Background(), ID("0sNOF9WDwhWunNAHPD3Baj"))
1415
if err != nil {
1516
t.Fatal(err)
1617
}
@@ -30,7 +31,7 @@ func TestFindAlbumBadID(t *testing.T) {
3031
client, server := testClientString(http.StatusNotFound, `{ "error": { "status": 404, "message": "non existing id" } }`)
3132
defer server.Close()
3233

33-
album, err := client.GetAlbum(ID("asdf"))
34+
album, err := client.GetAlbum(context.Background(), ID("asdf"))
3435
if album != nil {
3536
t.Fatal("Expected nil album, got", album.Name)
3637
}
@@ -51,7 +52,7 @@ func TestFindAlbums(t *testing.T) {
5152
client, server := testClientFile(http.StatusOK, "test_data/find_albums.txt")
5253
defer server.Close()
5354

54-
res, err := client.GetAlbums(ID("41MnTivkwTO3UUJ8DrqEJJ"), ID("6JWc4iAiJ9FjyK0B59ABb4"), ID("6UXCm6bOO4gFlDQZV5yL37"), ID("0X8vBD8h1Ga9eLT8jx9VCC"))
55+
res, err := client.GetAlbums(context.Background(), []ID{"41MnTivkwTO3UUJ8DrqEJJ", "6JWc4iAiJ9FjyK0B59ABb4", "6UXCm6bOO4gFlDQZV5yL37", "0X8vBD8h1Ga9eLT8jx9VCC"})
5556
if err != nil {
5657
t.Fatal(err)
5758
}
@@ -89,8 +90,7 @@ func TestFindAlbumTracks(t *testing.T) {
8990
client, server := testClientFile(http.StatusOK, "test_data/find_album_tracks.txt")
9091
defer server.Close()
9192

92-
limit := 1
93-
res, err := client.GetAlbumTracksOpt(ID("0sNOF9WDwhWunNAHPD3Baj"), &Options{Limit: &limit})
93+
res, err := client.GetAlbumTracks(context.Background(), ID("0sNOF9WDwhWunNAHPD3Baj"), Limit(1))
9494
if err != nil {
9595
t.Fatal(err)
9696
}

0 commit comments

Comments
 (0)