Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstrauch committed Jul 23, 2021
1 parent f840602 commit 0c1d002
Show file tree
Hide file tree
Showing 17 changed files with 341 additions and 291 deletions.
50 changes: 25 additions & 25 deletions accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,49 +149,49 @@ func BuildPKCEAuthURI(clientID, redirectURI, challenge, state string, scopes ...

// RequestToken allows a user to exchange an authorization code for an access token.
func RequestToken(clientID, code, redirectURI, clientSecret string) (*Token, error) {
v := url.Values{}
v.Set("client_id", clientID)
v.Set("grant_type", "authorization_code")
v.Set("code", code)
v.Set("redirect_uri", redirectURI)
v.Set("client_secret", clientSecret)
body := strings.NewReader(v.Encode())
query := make(url.Values)
query.Set("client_id", clientID)
query.Set("grant_type", "authorization_code")
query.Set("code", code)
query.Set("redirect_uri", redirectURI)
query.Set("client_secret", clientSecret)
body := strings.NewReader(query.Encode())

return postToken(body)
}

// RequestPKCEToken allows a user to exchange an authorization code for an access token.
func RequestPKCEToken(clientID, code, redirectURI, verifier string) (*Token, error) {
v := url.Values{}
v.Set("client_id", clientID)
v.Set("grant_type", "authorization_code")
v.Set("code", code)
v.Set("redirect_uri", redirectURI)
v.Set("code_verifier", verifier)
body := strings.NewReader(v.Encode())
query := make(url.Values)
query.Set("client_id", clientID)
query.Set("grant_type", "authorization_code")
query.Set("code", code)
query.Set("redirect_uri", redirectURI)
query.Set("code_verifier", verifier)
body := strings.NewReader(query.Encode())

return postToken(body)
}

// RefreshToken allows a user to exchange a refresh token for an access token.
func RefreshToken(refreshToken, clientID, clientSecret string) (*Token, error) {
v := url.Values{}
v.Set("grant_type", "refresh_token")
v.Set("refresh_token", refreshToken)
v.Set("client_id", clientID)
v.Set("client_secret", clientSecret)
body := strings.NewReader(v.Encode())
query := make(url.Values)
query.Set("grant_type", "refresh_token")
query.Set("refresh_token", refreshToken)
query.Set("client_id", clientID)
query.Set("client_secret", clientSecret)
body := strings.NewReader(query.Encode())

return postToken(body)
}

// RefreshPKCEToken allows a user to exchange a refresh token for an access token.
func RefreshPKCEToken(refreshToken, clientID string) (*Token, error) {
v := url.Values{}
v.Set("grant_type", "refresh_token")
v.Set("refresh_token", refreshToken)
v.Set("client_id", clientID)
body := strings.NewReader(v.Encode())
query := make(url.Values)
query.Set("grant_type", "refresh_token")
query.Set("refresh_token", refreshToken)
query.Set("client_id", clientID)
body := strings.NewReader(query.Encode())

return postToken(body)
}
Expand Down
17 changes: 0 additions & 17 deletions album.go

This file was deleted.

11 changes: 2 additions & 9 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ import (

const APIHost = "api.spotify.com"

// Error represents an ErrorObject in the Spotify API
// https://developer.spotify.com/documentation/web-api/reference/#object-errorobject
type Error struct {
Status int `json:"status"`
Message string `json:"message"`
}

type API struct {
token string
}
Expand All @@ -31,8 +24,8 @@ func (a *API) get(apiVersion, endpoint string, query url.Values, res interface{}
return a.call(http.MethodGet, apiVersion, endpoint, query, nil, res)
}

func (a *API) post(apiVersion, endpoint string, query url.Values, body io.Reader) error {
return a.call(http.MethodPost, apiVersion, endpoint, query, body, nil)
func (a *API) post(apiVersion, endpoint string, query url.Values, body io.Reader, res interface{}) error {
return a.call(http.MethodPost, apiVersion, endpoint, query, body, res)
}

func (a *API) put(apiVersion, endpoint string, query url.Values, body io.Reader) error {
Expand Down
10 changes: 0 additions & 10 deletions artist.go

This file was deleted.

88 changes: 88 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package spotify

import (
"net/url"
"strconv"
"strings"
"time"
)

// Duration is a wrapper struct for time.Duration to unmarshal durations in milliseconds.
type Duration struct {
time.Duration
}

func (d *Duration) UnmarshalJSON(data []byte) error {
ms, err := strconv.Atoi(string(data))
if err != nil {
return err
}
d.Duration = time.Duration(ms * 1000000)
return nil
}

type HREF string

// Get returns the most recent version of the object in its entirety.
func (h *HREF) Get(api *API, obj interface{}) error {
url, err := h.URL()
if err != nil {
return err
}

// Example path: v1/me/player
idx := strings.Index(url.Path, "/")
version := url.Path[:idx]
endpoint := url.Path[idx:]

return api.get(version, endpoint, url.Query(), obj)
}

// URL parses HREF into a URL object.
func (h *HREF) URL() (*url.URL, error) {
return url.Parse(string(*h))
}

// Meta represents common fields found in most API responses.
type Meta struct {
HREF HREF
ExternalURLs map[string]string `json:"external_urls"`
ID string `json:"id"`
Type string `json:"type"`
URI string `json:"uri"`
}

// PagingMeta represents common fields in paged responses
type PagingMeta struct {
HREF HREF
Limit int `json:"limit"`
Next string `json:"next"`
Offset int `json:"offset"`
Previous string `json:"previous"`
Total int `json:"total"`
}

// Get returns the most recent version of the object in its entirety
func (im *PagingMeta) Get(api *API, obj interface{}) error {
return im.HREF.Get(api, obj)
}

type AlbumPage struct {
PagingMeta
Items []*Album `json:"items"`
}

type TrackPage struct {
PagingMeta
Items []*Track `json:"items"`
}

type PlaylistPage struct {
PagingMeta
Items []*Playlist `json:"items"`
}

type PlaylistTrackPage struct {
PagingMeta
Items []*PlaylistTrack `json:"items"`
}
28 changes: 0 additions & 28 deletions href.go

This file was deleted.

9 changes: 0 additions & 9 deletions image.go

This file was deleted.

14 changes: 8 additions & 6 deletions library.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import (
)

// SaveTracks saves one or more tracks to the current user's 'Your Music' library.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-save-tracks-user
func (a *API) SaveTracks(ids ...string) error {
v := url.Values{}
v.Add("ids", strings.Join(ids, ","))
query := make(url.Values)
query.Add("ids", strings.Join(ids, ","))

return a.put("v1", "/me/tracks", v, nil)
return a.put("v1", "/me/tracks", query, nil)
}

// RemoveSavedTracks removes one or more tracks from the current user's 'Your Music' library.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-remove-tracks-user
func (a *API) RemoveSavedTracks(ids ...string) error {
v := url.Values{}
v.Add("ids", strings.Join(ids, ","))
query := make(url.Values)
query.Add("ids", strings.Join(ids, ","))

return a.delete("v1", "/me/tracks", v)
return a.delete("v1", "/me/tracks", query)
}
30 changes: 0 additions & 30 deletions meta.go

This file was deleted.

Loading

0 comments on commit 0c1d002

Please sign in to comment.