Skip to content

Commit 16e45d8

Browse files
authored
changes to support GetShow, GetShowEpisodes (#157)
1 parent 85b5787 commit 16e45d8

File tree

5 files changed

+2016
-1
lines changed

5 files changed

+2016
-1
lines changed

page.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ type CategoryPage struct {
9494
Categories []Category `json:"items"`
9595
}
9696

97+
// SimpleEpisodePage contains EpisodePage returned by the Web API.
98+
type SimpleEpisodePage struct {
99+
basePage
100+
Episodes []EpisodePage `json:"items"`
101+
}
102+
97103
// pageable is an internal interface for types that support paging
98104
// by embedding basePage.
99105
type pageable interface{ canPage() }

show.go

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package spotify
22

33
import (
4+
"net/url"
45
"strconv"
56
"strings"
67
"time"
@@ -20,7 +21,7 @@ type FullShow struct {
2021
SimpleShow
2122

2223
// A list of the show’s episodes.
23-
Episodes EpisodePage `json:"episode"`
24+
Episodes SimpleEpisodePage `json:"episodes"`
2425
}
2526

2627
// SimpleShow contains basic data about a show.
@@ -166,3 +167,69 @@ func (e *EpisodePage) ReleaseDateTime() time.Time {
166167
year, _ := strconv.Atoi(e.ReleaseDate)
167168
return time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC)
168169
}
170+
171+
// GetShow retrieves information about a specific show.
172+
// API reference: https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-a-show
173+
func (c *Client) GetShow(id string) (*FullShow, error) {
174+
return c.GetShowOpt(nil, id)
175+
}
176+
177+
// GetShowOpt is like GetShow while supporting an optional market parameter.
178+
// API reference: https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-a-show
179+
func (c *Client) GetShowOpt(opt *Options, id string) (*FullShow, error) {
180+
spotifyURL := c.baseURL + "shows/" + id
181+
if opt != nil {
182+
v := url.Values{}
183+
if opt.Country != nil {
184+
v.Set("market", *opt.Country)
185+
}
186+
if params := v.Encode(); params != "" {
187+
spotifyURL += "?" + params
188+
}
189+
}
190+
191+
var result FullShow
192+
193+
err := c.get(spotifyURL, &result)
194+
if err != nil {
195+
return nil, err
196+
}
197+
198+
return &result, nil
199+
}
200+
201+
// GetShowEpisodes retrieves paginated episode information about a specific show.
202+
// API reference: https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-a-shows-episodes
203+
func (c *Client) GetShowEpisodes(id string) (*SimpleEpisodePage, error) {
204+
return c.GetShowEpisodesOpt(nil, id)
205+
}
206+
207+
// GetShowEpisodesOpt is like GetShowEpisodes while supporting optional market, limit, offset parameters.
208+
// API reference: https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-a-shows-episodes
209+
func (c *Client) GetShowEpisodesOpt(opt *Options, id string) (*SimpleEpisodePage, error) {
210+
spotifyURL := c.baseURL + "shows/" + id + "/episodes"
211+
if opt != nil {
212+
v := url.Values{}
213+
if opt.Country != nil {
214+
v.Set("market", *opt.Country)
215+
}
216+
if opt.Limit != nil {
217+
v.Set("limit", strconv.Itoa(*opt.Limit))
218+
}
219+
if opt.Offset != nil {
220+
v.Set("offset", strconv.Itoa(*opt.Offset))
221+
}
222+
if params := v.Encode(); params != "" {
223+
spotifyURL += "?" + params
224+
}
225+
}
226+
227+
var result SimpleEpisodePage
228+
229+
err := c.get(spotifyURL, &result)
230+
if err != nil {
231+
return nil, err
232+
}
233+
234+
return &result, nil
235+
}

show_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package spotify
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
)
7+
8+
func TestGetShow(t *testing.T) {
9+
c, s := testClientFile(http.StatusOK, "test_data/get_show.txt")
10+
defer s.Close()
11+
12+
r, err := c.GetShow("1234")
13+
if err != nil {
14+
t.Fatal(err)
15+
}
16+
if r.SimpleShow.Name != "Uncommon Core" {
17+
t.Error("Invalid data:", r.Name)
18+
}
19+
if len(r.Episodes.Episodes) != 25 {
20+
t.Error("Invalid data", len(r.Episodes.Episodes))
21+
}
22+
}
23+
24+
func TestGetShowEpisodes(t *testing.T) {
25+
c, s := testClientFile(http.StatusOK, "test_data/get_show_episodes.txt")
26+
defer s.Close()
27+
28+
r, err := c.GetShowEpisodes("1234")
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
if r.Total != 25 {
33+
t.Error("Invalid data:", r.Total)
34+
}
35+
if r.Offset != 0 {
36+
t.Error("Invalid data:", r.Offset)
37+
}
38+
if len(r.Episodes) != 25 {
39+
t.Error("Invalid data", len(r.Episodes))
40+
}
41+
}

0 commit comments

Comments
 (0)