Skip to content

Commit

Permalink
device endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstrauch committed Jul 15, 2021
1 parent 8b21e0d commit 5e406af
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 41 deletions.
17 changes: 9 additions & 8 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ 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 {
Error struct {
Status int `json:"status"`
Message string `json:"message"`
Reason string `json:"reason"`
} `json:"error"`
Status int `json:"status"`
Message string `json:"message"`
}

type API struct {
Expand All @@ -28,8 +27,8 @@ func NewAPI(token string) *API {
return &API{token}
}

func (a *API) get(apiVersion, endpoint string, query url.Values, result interface{}) error {
return a.call(http.MethodGet, apiVersion, endpoint, query, nil, result)
func (a *API) get(apiVersion, endpoint string, query url.Values, res interface{}) error {
return a.call(http.MethodGet, apiVersion, endpoint, query, nil, res)
}

func (a *API) post(apiVersion, endpoint string, query url.Values, body io.Reader) error {
Expand Down Expand Up @@ -77,7 +76,9 @@ func (a *API) call(method, apiVersion, endpoint string, query url.Values, body i
}

// Error
spotifyErr := new(Error)
spotifyErr := &struct {
Error Error `json:"error"`
}{}
if err := json.NewDecoder(res.Body).Decode(spotifyErr); err != nil {
return err
}
Expand Down
86 changes: 63 additions & 23 deletions player.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,32 @@ type Playback struct {
ShuffleState bool `json:"shuffle_state"`
}

// Device represents a DeviceObject in the Spotify API
// https://developer.spotify.com/documentation/web-api/reference/#object-deviceobject
type Device struct {
ID string `json:"id"`
IsActive bool `json:"is_active"`
IsPrivateSession bool `json:"is_private_session"`
IsRestricted bool `json:"is_restricted"`
Name string `json:"name"`
Type string `json:"type"`
VolumePercent int `json:"volume_percent"`
}

type Item struct {
Track
Show Show `json:"show"`
Type string `json:"type"`
}

// Show represents a ShowObject in the Spotify API
// https://developer.spotify.com/documentation/web-api/reference/#object-showobject
type Show struct {
Name string `json:"name"`
}

// GetPlayback gets information about the user's current playback state, including track or episode, progress, and active device.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-information-about-the-users-current-playback
func (a *API) GetPlayback() (*Playback, error) {
v := url.Values{}
v.Add("additional_types", "episode")
Expand All @@ -41,41 +56,66 @@ func (a *API) GetPlayback() (*Playback, error) {
return playback, err
}

// Pause pauses playback on the user's account.
func (a *API) Pause() error {
return a.put("v1", "/me/player/pause", nil, nil)
// GetDevices gets information about a user's available devices.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-a-users-available-devices
func (a *API) GetDevices() ([]*Device, error) {
res := &struct {
Devices []*Device `json:"devices"`
}{}

err := a.get("v1", "/me/player/devices", nil, res)
return res.Devices, err
}

// Play starts a new context or resume current playback on the user's active device.
func (a *API) Play(uris ...string) error {
if len(uris) == 0 {
return a.put("v1", "/me/player/play", nil, nil)
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-start-a-users-playback
func (a *API) Play(deviceID string, uris ...string) error {
v := make(url.Values)
if deviceID != "" {
v.Add("device_id", deviceID)
}

type Body struct {
URIs []string `json:"uris"`
if len(uris) == 0 {
return a.put("v1", "/me/player/play", v, nil)
}

body := new(Body)
body.URIs = uris
body := &struct {
URIs []string `json:"uris"`
}{URIs: uris}

data, err := json.Marshal(body)
if err != nil {
return err
}

return a.put("v1", "/me/player/play", nil, bytes.NewReader(data))
return a.put("v1", "/me/player/play", v, bytes.NewReader(data))
}

// Queue adds an item to the end of the user's current playback queue.
func (a *API) Queue(uri string) error {
v := url.Values{}
v.Add("uri", uri)
// Pause pauses playback on the user's account.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-pause-a-users-playback
func (a *API) Pause(deviceID string) error {
v := make(url.Values)
if deviceID != "" {
v.Add("device_id", deviceID)
}

return a.post("v1", "/me/player/queue", v, nil)
return a.put("v1", "/me/player/pause", v, nil)
}

// SkipToPreviousTrack skips to the previous track in the user's queue.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-skip-users-playback-to-previous-track
func (a *API) SkipToPreviousTrack() error {
return a.post("v1", "/me/player/previous", nil, nil)
}

// SkipToNextTrack skips to the next track in the user's queue.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-skip-users-playback-to-next-track
func (a *API) SkipToNextTrack() error {
return a.post("v1", "/me/player/next", nil, nil)
}

// Repeat sets the repeat mode for the user's playback. Options are repeat-track, repeat-context, and off.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-set-repeat-mode-on-users-playback
func (a *API) Repeat(state string) error {
v := url.Values{}
v.Add("state", state)
Expand All @@ -84,19 +124,19 @@ func (a *API) Repeat(state string) error {
}

// Shuffle toggles shuffle on or off for user's playback.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-toggle-shuffle-for-users-playback
func (a *API) Shuffle(state bool) error {
v := url.Values{}
v.Add("state", strconv.FormatBool(state))

return a.put("v1", "/me/player/shuffle", v, nil)
}

// SkipToPreviousTrack skips to the previous track in the user's queue.
func (a *API) SkipToPreviousTrack() error {
return a.post("v1", "/me/player/previous", nil, nil)
}
// Queue adds an item to the end of the user's current playback queue.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-add-to-queue
func (a *API) Queue(uri string) error {
v := url.Values{}
v.Add("uri", uri)

// SkipToNextTrack skips to the next track in the user's queue.
func (a *API) SkipToNextTrack() error {
return a.post("v1", "/me/player/next", nil, nil)
return a.post("v1", "/me/player/queue", v, nil)
}
18 changes: 8 additions & 10 deletions playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@ type Playlist struct {
Tracks PlaylistTrackPage `json:"tracks"`
}

// GetPlaylists gets a list of the playlists owned or followed by the current Spotify user.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-a-list-of-current-users-playlists
func (a *API) GetPlaylists() ([]*Playlist, error) {
playlists := new(PlaylistPage)
if err := a.get("v1", "/me/playlists", nil, playlists); err != nil {
return nil, err
}

return playlists.Items, nil
err := a.get("v1", "/me/playlists", nil, playlists)
return playlists.Items, err
}

// GetPlaylist gets a playlist owned by a Spotify user.
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-playlist
func (a *API) GetPlaylist(id string) (*Playlist, error) {
playlist := new(Playlist)
if err := a.get("v1", fmt.Sprintf("/playlists/%s", id), nil, playlist); err != nil {
return nil, err
}

return playlist, nil
err := a.get("v1", fmt.Sprintf("/playlists/%s", id), nil, playlist)
return playlist, err
}

0 comments on commit 5e406af

Please sign in to comment.