Skip to content

Commit

Permalink
Merge pull request #188 from manland/addShowAndEpisodeInSearch
Browse files Browse the repository at this point in the history
add shows and episodes in search results
  • Loading branch information
strideynet authored Sep 23, 2022
2 parents 6a52a9c + f8f2d33 commit ace6a00
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
6 changes: 6 additions & 0 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ type SimpleEpisodePage struct {
Episodes []EpisodePage `json:"items"`
}

// SimpleShowPage contains ShowPage returned by the Web API.
type SimpleShowPage struct {
basePage
Shows []FullShow `json:"items"`
}

// pageable is an internal interface for types that support paging
// by embedding basePage.
type pageable interface{ canPage() }
Expand Down
42 changes: 42 additions & 0 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const (
SearchTypeArtist = 1 << iota
SearchTypePlaylist = 1 << iota
SearchTypeTrack = 1 << iota
SearchTypeShow = 1 << iota
SearchTypeEpisode = 1 << iota
)

func (st SearchType) encode() string {
Expand All @@ -41,6 +43,12 @@ func (st SearchType) encode() string {
if st&SearchTypeTrack != 0 {
types = append(types, "track")
}
if st&SearchTypeShow != 0 {
types = append(types, "show")
}
if st&SearchTypeEpisode != 0 {
types = append(types, "episode")
}
return strings.Join(types, ",")
}

Expand All @@ -51,6 +59,8 @@ type SearchResult struct {
Albums *SimpleAlbumPage `json:"albums"`
Playlists *SimplePlaylistPage `json:"playlists"`
Tracks *FullTrackPage `json:"tracks"`
Shows *SimpleShowPage `json:"shows"`
Episodes *SimpleEpisodePage `json:"episodes"`
}

// Search gets Spotify catalog information about artists, albums, tracks,
Expand Down Expand Up @@ -189,3 +199,35 @@ func (c *Client) NextTrackResults(ctx context.Context, s *SearchResult) error {
}
return c.get(ctx, s.Tracks.Next, s)
}

// PreviousShowResults loads the previous page of shows into the specified search result.
func (c *Client) PreviousShowResults(ctx context.Context, s *SearchResult) error {
if s.Shows == nil || s.Shows.Previous == "" {
return ErrNoMorePages
}
return c.get(ctx, s.Shows.Previous, s)
}

// NextShowResults loads the next page of shows into the specified search result.
func (c *Client) NextShowResults(ctx context.Context, s *SearchResult) error {
if s.Shows == nil || s.Shows.Next == "" {
return ErrNoMorePages
}
return c.get(ctx, s.Shows.Next, s)
}

// PreviousEpisodeResults loads the previous page of episodes into the specified search result.
func (c *Client) PreviousEpisodeResults(ctx context.Context, s *SearchResult) error {
if s.Episodes == nil || s.Episodes.Previous == "" {
return ErrNoMorePages
}
return c.get(ctx, s.Episodes.Previous, s)
}

// NextEpisodeResults loads the next page of episodes into the specified search result.
func (c *Client) NextEpisodeResults(ctx context.Context, s *SearchResult) error {
if s.Episodes == nil || s.Episodes.Next == "" {
return ErrNoMorePages
}
return c.get(ctx, s.Episodes.Next, s)
}
20 changes: 15 additions & 5 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,21 @@ func TestPrevNextSearchPageErrors(t *testing.T) {
// under either of these conditions:

// 1) there are no results (nil)
nilResults := &SearchResult{nil, nil, nil, nil}
nilResults := &SearchResult{nil, nil, nil, nil, nil, nil}
if client.NextAlbumResults(context.Background(), nilResults) != ErrNoMorePages ||
client.NextArtistResults(context.Background(), nilResults) != ErrNoMorePages ||
client.NextPlaylistResults(context.Background(), nilResults) != ErrNoMorePages ||
client.NextTrackResults(context.Background(), nilResults) != ErrNoMorePages {
client.NextTrackResults(context.Background(), nilResults) != ErrNoMorePages ||
client.NextShowResults(context.Background(), nilResults) != ErrNoMorePages ||
client.NextEpisodeResults(context.Background(), nilResults) != ErrNoMorePages {
t.Error("Next search result page should have failed for nil results")
}
if client.PreviousAlbumResults(context.Background(), nilResults) != ErrNoMorePages ||
client.PreviousArtistResults(context.Background(), nilResults) != ErrNoMorePages ||
client.PreviousPlaylistResults(context.Background(), nilResults) != ErrNoMorePages ||
client.PreviousTrackResults(context.Background(), nilResults) != ErrNoMorePages {
client.PreviousTrackResults(context.Background(), nilResults) != ErrNoMorePages ||
client.PreviousShowResults(context.Background(), nilResults) != ErrNoMorePages ||
client.PreviousEpisodeResults(context.Background(), nilResults) != ErrNoMorePages {
t.Error("Previous search result page should have failed for nil results")
}
// 2) the prev/next URL is empty
Expand All @@ -105,17 +109,23 @@ func TestPrevNextSearchPageErrors(t *testing.T) {
Albums: new(SimpleAlbumPage),
Playlists: new(SimplePlaylistPage),
Tracks: new(FullTrackPage),
Shows: new(SimpleShowPage),
Episodes: new(SimpleEpisodePage),
}
if client.NextAlbumResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.NextArtistResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.NextPlaylistResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.NextTrackResults(context.Background(), emptyURL) != ErrNoMorePages {
client.NextTrackResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.NextShowResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.NextEpisodeResults(context.Background(), emptyURL) != ErrNoMorePages {
t.Error("Next search result page should have failed with empty URL")
}
if client.PreviousAlbumResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.PreviousArtistResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.PreviousPlaylistResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.PreviousTrackResults(context.Background(), emptyURL) != ErrNoMorePages {
client.PreviousTrackResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.PreviousShowResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.PreviousEpisodeResults(context.Background(), emptyURL) != ErrNoMorePages {
t.Error("Previous search result page should have failed with empty URL")
}
}

0 comments on commit ace6a00

Please sign in to comment.