Skip to content

Commit ace6a00

Browse files
authored
Merge pull request #188 from manland/addShowAndEpisodeInSearch
add shows and episodes in search results
2 parents 6a52a9c + f8f2d33 commit ace6a00

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

page.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ type SimpleEpisodePage struct {
101101
Episodes []EpisodePage `json:"items"`
102102
}
103103

104+
// SimpleShowPage contains ShowPage returned by the Web API.
105+
type SimpleShowPage struct {
106+
basePage
107+
Shows []FullShow `json:"items"`
108+
}
109+
104110
// pageable is an internal interface for types that support paging
105111
// by embedding basePage.
106112
type pageable interface{ canPage() }

search.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const (
2525
SearchTypeArtist = 1 << iota
2626
SearchTypePlaylist = 1 << iota
2727
SearchTypeTrack = 1 << iota
28+
SearchTypeShow = 1 << iota
29+
SearchTypeEpisode = 1 << iota
2830
)
2931

3032
func (st SearchType) encode() string {
@@ -41,6 +43,12 @@ func (st SearchType) encode() string {
4143
if st&SearchTypeTrack != 0 {
4244
types = append(types, "track")
4345
}
46+
if st&SearchTypeShow != 0 {
47+
types = append(types, "show")
48+
}
49+
if st&SearchTypeEpisode != 0 {
50+
types = append(types, "episode")
51+
}
4452
return strings.Join(types, ",")
4553
}
4654

@@ -51,6 +59,8 @@ type SearchResult struct {
5159
Albums *SimpleAlbumPage `json:"albums"`
5260
Playlists *SimplePlaylistPage `json:"playlists"`
5361
Tracks *FullTrackPage `json:"tracks"`
62+
Shows *SimpleShowPage `json:"shows"`
63+
Episodes *SimpleEpisodePage `json:"episodes"`
5464
}
5565

5666
// Search gets Spotify catalog information about artists, albums, tracks,
@@ -189,3 +199,35 @@ func (c *Client) NextTrackResults(ctx context.Context, s *SearchResult) error {
189199
}
190200
return c.get(ctx, s.Tracks.Next, s)
191201
}
202+
203+
// PreviousShowResults loads the previous page of shows into the specified search result.
204+
func (c *Client) PreviousShowResults(ctx context.Context, s *SearchResult) error {
205+
if s.Shows == nil || s.Shows.Previous == "" {
206+
return ErrNoMorePages
207+
}
208+
return c.get(ctx, s.Shows.Previous, s)
209+
}
210+
211+
// NextShowResults loads the next page of shows into the specified search result.
212+
func (c *Client) NextShowResults(ctx context.Context, s *SearchResult) error {
213+
if s.Shows == nil || s.Shows.Next == "" {
214+
return ErrNoMorePages
215+
}
216+
return c.get(ctx, s.Shows.Next, s)
217+
}
218+
219+
// PreviousEpisodeResults loads the previous page of episodes into the specified search result.
220+
func (c *Client) PreviousEpisodeResults(ctx context.Context, s *SearchResult) error {
221+
if s.Episodes == nil || s.Episodes.Previous == "" {
222+
return ErrNoMorePages
223+
}
224+
return c.get(ctx, s.Episodes.Previous, s)
225+
}
226+
227+
// NextEpisodeResults loads the next page of episodes into the specified search result.
228+
func (c *Client) NextEpisodeResults(ctx context.Context, s *SearchResult) error {
229+
if s.Episodes == nil || s.Episodes.Next == "" {
230+
return ErrNoMorePages
231+
}
232+
return c.get(ctx, s.Episodes.Next, s)
233+
}

search_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,21 @@ func TestPrevNextSearchPageErrors(t *testing.T) {
8686
// under either of these conditions:
8787

8888
// 1) there are no results (nil)
89-
nilResults := &SearchResult{nil, nil, nil, nil}
89+
nilResults := &SearchResult{nil, nil, nil, nil, nil, nil}
9090
if client.NextAlbumResults(context.Background(), nilResults) != ErrNoMorePages ||
9191
client.NextArtistResults(context.Background(), nilResults) != ErrNoMorePages ||
9292
client.NextPlaylistResults(context.Background(), nilResults) != ErrNoMorePages ||
93-
client.NextTrackResults(context.Background(), nilResults) != ErrNoMorePages {
93+
client.NextTrackResults(context.Background(), nilResults) != ErrNoMorePages ||
94+
client.NextShowResults(context.Background(), nilResults) != ErrNoMorePages ||
95+
client.NextEpisodeResults(context.Background(), nilResults) != ErrNoMorePages {
9496
t.Error("Next search result page should have failed for nil results")
9597
}
9698
if client.PreviousAlbumResults(context.Background(), nilResults) != ErrNoMorePages ||
9799
client.PreviousArtistResults(context.Background(), nilResults) != ErrNoMorePages ||
98100
client.PreviousPlaylistResults(context.Background(), nilResults) != ErrNoMorePages ||
99-
client.PreviousTrackResults(context.Background(), nilResults) != ErrNoMorePages {
101+
client.PreviousTrackResults(context.Background(), nilResults) != ErrNoMorePages ||
102+
client.PreviousShowResults(context.Background(), nilResults) != ErrNoMorePages ||
103+
client.PreviousEpisodeResults(context.Background(), nilResults) != ErrNoMorePages {
100104
t.Error("Previous search result page should have failed for nil results")
101105
}
102106
// 2) the prev/next URL is empty
@@ -105,17 +109,23 @@ func TestPrevNextSearchPageErrors(t *testing.T) {
105109
Albums: new(SimpleAlbumPage),
106110
Playlists: new(SimplePlaylistPage),
107111
Tracks: new(FullTrackPage),
112+
Shows: new(SimpleShowPage),
113+
Episodes: new(SimpleEpisodePage),
108114
}
109115
if client.NextAlbumResults(context.Background(), emptyURL) != ErrNoMorePages ||
110116
client.NextArtistResults(context.Background(), emptyURL) != ErrNoMorePages ||
111117
client.NextPlaylistResults(context.Background(), emptyURL) != ErrNoMorePages ||
112-
client.NextTrackResults(context.Background(), emptyURL) != ErrNoMorePages {
118+
client.NextTrackResults(context.Background(), emptyURL) != ErrNoMorePages ||
119+
client.NextShowResults(context.Background(), emptyURL) != ErrNoMorePages ||
120+
client.NextEpisodeResults(context.Background(), emptyURL) != ErrNoMorePages {
113121
t.Error("Next search result page should have failed with empty URL")
114122
}
115123
if client.PreviousAlbumResults(context.Background(), emptyURL) != ErrNoMorePages ||
116124
client.PreviousArtistResults(context.Background(), emptyURL) != ErrNoMorePages ||
117125
client.PreviousPlaylistResults(context.Background(), emptyURL) != ErrNoMorePages ||
118-
client.PreviousTrackResults(context.Background(), emptyURL) != ErrNoMorePages {
126+
client.PreviousTrackResults(context.Background(), emptyURL) != ErrNoMorePages ||
127+
client.PreviousShowResults(context.Background(), emptyURL) != ErrNoMorePages ||
128+
client.PreviousEpisodeResults(context.Background(), emptyURL) != ErrNoMorePages {
119129
t.Error("Previous search result page should have failed with empty URL")
120130
}
121131
}

0 commit comments

Comments
 (0)