Skip to content

Commit

Permalink
Merge pull request #211 from R0qs3T/master
Browse files Browse the repository at this point in the history
Adding "GetQueue" Function to player.go.
  • Loading branch information
strideynet authored Dec 29, 2022
2 parents 135b212 + 01612a6 commit 8d3038a
Show file tree
Hide file tree
Showing 3 changed files with 1,512 additions and 0 deletions.
24 changes: 24 additions & 0 deletions player.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ type RecentlyPlayedOptions struct {
BeforeEpochMs int64
}

type Queue struct {
CurrentlyPlaying FullTrack `json:"currently_playing"`
Items []FullTrack `json:"queue"`
}

// PlayerDevices information about available devices for the current user.
//
// Requires the ScopeUserReadPlaybackState scope in order to read information
Expand Down Expand Up @@ -330,6 +335,25 @@ func (c *Client) PauseOpt(ctx context.Context, opt *PlayOptions) error {
return nil
}

// GetQueue gets the user's queue on the user's currently
// active device. This call requires ScopeUserReadPlaybackState
func (c *Client) GetQueue(ctx context.Context) (*Queue, error) {
spotifyURL := c.baseURL + "me/player/queue"
v := url.Values{}

if params := v.Encode(); params != "" {
spotifyURL += "?" + params
}

var q Queue
err := c.get(ctx, spotifyURL, &q)
if err != nil {
return nil, err
}

return &q, nil
}

// QueueSong adds a song to the user's queue on the user's currently
// active device. This call requires ScopeUserModifyPlaybackState
// in order to modify the player state
Expand Down
26 changes: 26 additions & 0 deletions player_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,29 @@ func TestPlayArgsError(t *testing.T) {
t.Error("Expected an error")
}
}

func TestGetQueue(t *testing.T) {
client, server := testClientFile(http.StatusOK, "test_data/get_queue.txt")
defer server.Close()

queue, err := client.GetQueue(context.Background())
if err != nil {
t.Error(err)
}
if l := len(queue.Items); l == 0 {
t.Fatal("Didn't get any results")
} else if l != 20 {
t.Errorf("Got %d playlists, expected 20\n", l)
}

p := queue.Items[0].SimpleTrack
if p.Name != "This Is the End (For You My Friend)" {
t.Error("Expected 'This Is the End (For You My Friend)', got", p.Name)
}

p = queue.CurrentlyPlaying.SimpleTrack

if p.Name != "Know Your Enemy" {
t.Error("Expected 'Know Your Enemy', got", p.Name)
}
}
Loading

0 comments on commit 8d3038a

Please sign in to comment.