From a5868c9d891f54cdf25917772a9a9f364c5860b8 Mon Sep 17 00:00:00 2001 From: Jeff <> Date: Tue, 16 Jul 2019 19:39:34 -0400 Subject: [PATCH 1/6] add method for getting all shows --- tvmaze/show.go | 12 +++++++++++- tvmaze/tvmaze_test.go | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tvmaze/show.go b/tvmaze/show.go index 0886309..1d8bd42 100644 --- a/tvmaze/show.go +++ b/tvmaze/show.go @@ -4,10 +4,11 @@ import ( "encoding/json" "fmt" "reflect" + "strconv" "time" - log "github.com/sirupsen/logrus" "github.com/pkg/errors" + log "github.com/sirupsen/logrus" ) // ShowResponse wraps a TV Maze search response @@ -106,6 +107,15 @@ func (s Show) GetIMDBID() string { return val } +func (c Client) GetShows(offset int) ([]Show, error) { + url := baseURLWithPathQuery("shows", "page", strconv.Itoa(offset)) + shows := []Show{} + if err := c.get(url, &shows); err != nil { + return nil, err + } + return shows, nil +} + // FindShows finds all matches for a given search string func (c Client) FindShows(name string) (s []ShowResponse, err error) { url := baseURLWithPathQuery("search/shows", "q", name) diff --git a/tvmaze/tvmaze_test.go b/tvmaze/tvmaze_test.go index 62a3df1..6d16453 100644 --- a/tvmaze/tvmaze_test.go +++ b/tvmaze/tvmaze_test.go @@ -9,6 +9,13 @@ import ( func TestTVMaze(t *testing.T) { c := DefaultClient + t.Run("get shows", func(t *testing.T) { + t.Parallel() + results, err := c.GetShows(0) + require.NoError(t, err) + require.NotEmpty(t, len(results)) + }) + t.Run("find show", func(t *testing.T) { t.Parallel() results, err := c.FindShows("archer") From f6d35e2a62146a9439be123a24f307719a79dcd5 Mon Sep 17 00:00:00 2001 From: jeff <> Date: Wed, 17 Jul 2019 18:49:04 -0400 Subject: [PATCH 2/6] use page as parameter name --- tvmaze/show.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tvmaze/show.go b/tvmaze/show.go index 1d8bd42..20292f4 100644 --- a/tvmaze/show.go +++ b/tvmaze/show.go @@ -107,7 +107,7 @@ func (s Show) GetIMDBID() string { return val } -func (c Client) GetShows(offset int) ([]Show, error) { +func (c Client) GetShows(page int) ([]Show, error) { url := baseURLWithPathQuery("shows", "page", strconv.Itoa(offset)) shows := []Show{} if err := c.get(url, &shows); err != nil { From 2e62a3950d5d5f6905ca3c8dd5d4c674ed85aa5d Mon Sep 17 00:00:00 2001 From: jeff <> Date: Wed, 17 Jul 2019 19:03:45 -0400 Subject: [PATCH 3/6] handle 404s for documented cases; otherwise status codes > 400 trigger errors --- tvmaze/client.go | 10 +++++++--- tvmaze/episode.go | 10 +++++++--- tvmaze/show.go | 28 +++++++++++++++++++--------- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/tvmaze/client.go b/tvmaze/client.go index ef46dc8..09f9705 100644 --- a/tvmaze/client.go +++ b/tvmaze/client.go @@ -25,14 +25,18 @@ func NewClient() Client { return Client{} } -func (c Client) get(url url.URL, ret interface{}) (err error) { +func (c Client) get(url url.URL, ret interface{}) (status int, err error) { r, err := http.Get(url.String()) if err != nil { - return errors.Wrapf(err, "failed to get url: %s", url.String()) + return 0, errors.Wrapf(err, "failed to get url: %s", url.String()) + } + + if r.StatusCode >= http.StatusBadRequest { + return r.StatusCode, fmt.Errorf("received error status code (%d): %s", r.StatusCode, r.Status) } defer r.Body.Close() - return json.NewDecoder(r.Body).Decode(&ret) + return r.StatusCode, json.NewDecoder(r.Body).Decode(&ret) } func baseURLWithPath(path string) url.URL { diff --git a/tvmaze/episode.go b/tvmaze/episode.go index 23ba22c..cd58ad7 100644 --- a/tvmaze/episode.go +++ b/tvmaze/episode.go @@ -2,6 +2,7 @@ package tvmaze import ( "fmt" + "net/http" "strconv" "time" ) @@ -21,7 +22,7 @@ type Episode struct { func (c Client) GetEpisodes(s Show) (episodes []Episode, err error) { url := baseURLWithPath(fmt.Sprintf("shows/%d/episodes", s.ID)) - if err = c.get(url, &episodes); err != nil { + if _, err = c.get(url, &episodes); err != nil { return nil, err } @@ -33,7 +34,7 @@ func (c Client) GetNextEpisode(s Show) (*Episode, error) { url := baseURLWithPathQuery(fmt.Sprintf("shows/%d", s.ID), "embed", "nextepisode") var embed embeddedNextEpisode - if err := c.get(url, &embed); err != nil { + if _, err := c.get(url, &embed); err != nil { return nil, err } @@ -51,7 +52,10 @@ func (c Client) GetEpisode(s Show, season int, episode int) (*Episode, error) { }) var epOut Episode - if err := c.get(url, &epOut); err != nil { + if status, err := c.get(url, &epOut); err != nil { + if status == http.StatusNotFound { + return &epOut, nil + } return nil, err } return &epOut, nil diff --git a/tvmaze/show.go b/tvmaze/show.go index 20292f4..ce53a34 100644 --- a/tvmaze/show.go +++ b/tvmaze/show.go @@ -3,6 +3,7 @@ package tvmaze import ( "encoding/json" "fmt" + "net/http" "reflect" "strconv" "time" @@ -107,10 +108,15 @@ func (s Show) GetIMDBID() string { return val } +// GetShows returns a list of all shows in the TVMaze database. When the end of the index +// is reached, a nil slice is returned. func (c Client) GetShows(page int) ([]Show, error) { - url := baseURLWithPathQuery("shows", "page", strconv.Itoa(offset)) + url := baseURLWithPathQuery("shows", "page", strconv.Itoa(page)) shows := []Show{} - if err := c.get(url, &shows); err != nil { + if status, err := c.get(url, &shows); err != nil { + if status == http.StatusNotFound { + return nil, nil + } return nil, err } return shows, nil @@ -120,7 +126,7 @@ func (c Client) GetShows(page int) ([]Show, error) { func (c Client) FindShows(name string) (s []ShowResponse, err error) { url := baseURLWithPathQuery("search/shows", "q", name) - if err := c.get(url, &s); err != nil { + if _, err := c.get(url, &s); err != nil { return nil, err } @@ -132,7 +138,7 @@ func (c Client) GetShow(name string) (*Show, error) { url := baseURLWithPathQuery("singlesearch/shows", "q", name) show := &Show{} - if err := c.get(url, show); err != nil { + if _, err := c.get(url, show); err != nil { return nil, err } @@ -144,7 +150,7 @@ func (c Client) GetShowWithID(tvMazeID string) (*Show, error) { url := baseURLWithPath(fmt.Sprintf("shows/%s", tvMazeID)) show := &Show{} - if err := c.get(url, show); err != nil { + if _, err := c.get(url, show); err != nil { return nil, err } @@ -156,7 +162,10 @@ func (c Client) GetShowWithTVRageID(tvRageID string) (*Show, error) { url := baseURLWithPathQuery("lookup/shows", "tvrage", tvRageID) show := &Show{} - if err := c.get(url, show); err != nil { + if status, err := c.get(url, show); err != nil { + if status == http.StatusNotFound { + return show, nil + } return nil, err } @@ -168,7 +177,7 @@ func (c Client) GetShowWithTVDBID(TVDBID string) (*Show, error) { url := baseURLWithPathQuery("lookup/shows", "thetvdb", TVDBID) show := &Show{} - if err := c.get(url, show); err != nil { + if _, err := c.get(url, show); err != nil { return nil, err } @@ -176,9 +185,10 @@ func (c Client) GetShowWithTVDBID(TVDBID string) (*Show, error) { } // RefreshShow refreshes a show from the server -func (c Client) RefreshShow(show *Show) (err error) { +func (c Client) RefreshShow(show *Show) error { url := baseURLWithPath(fmt.Sprintf("shows/%d", show.ID)) - return c.get(url, &show) + _, err := c.get(url, &show) + return err } // Date represents a date from tvmaze, supporting nullability From a0481538143fb53ac081f084cbcfed515d7e9c08 Mon Sep 17 00:00:00 2001 From: jeff <> Date: Wed, 17 Jul 2019 19:06:15 -0400 Subject: [PATCH 4/6] use errors package for wrapping status code errors --- tvmaze/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tvmaze/client.go b/tvmaze/client.go index 09f9705..b511323 100644 --- a/tvmaze/client.go +++ b/tvmaze/client.go @@ -32,7 +32,7 @@ func (c Client) get(url url.URL, ret interface{}) (status int, err error) { } if r.StatusCode >= http.StatusBadRequest { - return r.StatusCode, fmt.Errorf("received error status code (%d): %s", r.StatusCode, r.Status) + return r.StatusCode, errors.Errorf("received error status code (%d): %s", r.StatusCode, r.Status) } defer r.Body.Close() From 71ed6256a785d5d465533af4905bdf135b572b18 Mon Sep 17 00:00:00 2001 From: jeff <> Date: Wed, 17 Jul 2019 19:29:39 -0400 Subject: [PATCH 5/6] don't ignore 404s everywhere yet --- tvmaze/episode.go | 6 +----- tvmaze/show.go | 5 +---- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/tvmaze/episode.go b/tvmaze/episode.go index cd58ad7..0feedb7 100644 --- a/tvmaze/episode.go +++ b/tvmaze/episode.go @@ -2,7 +2,6 @@ package tvmaze import ( "fmt" - "net/http" "strconv" "time" ) @@ -52,10 +51,7 @@ func (c Client) GetEpisode(s Show, season int, episode int) (*Episode, error) { }) var epOut Episode - if status, err := c.get(url, &epOut); err != nil { - if status == http.StatusNotFound { - return &epOut, nil - } + if _, err := c.get(url, &epOut); err != nil { return nil, err } return &epOut, nil diff --git a/tvmaze/show.go b/tvmaze/show.go index ce53a34..f4d2a30 100644 --- a/tvmaze/show.go +++ b/tvmaze/show.go @@ -162,10 +162,7 @@ func (c Client) GetShowWithTVRageID(tvRageID string) (*Show, error) { url := baseURLWithPathQuery("lookup/shows", "tvrage", tvRageID) show := &Show{} - if status, err := c.get(url, show); err != nil { - if status == http.StatusNotFound { - return show, nil - } + if _, err := c.get(url, show); err != nil { return nil, err } From 82dd674b3c4b8c96a9195843dc6894c4fa59e682 Mon Sep 17 00:00:00 2001 From: jeff <> Date: Wed, 17 Jul 2019 19:29:55 -0400 Subject: [PATCH 6/6] test for nil result on 404 --- tvmaze/tvmaze_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tvmaze/tvmaze_test.go b/tvmaze/tvmaze_test.go index 6d16453..feb6b70 100644 --- a/tvmaze/tvmaze_test.go +++ b/tvmaze/tvmaze_test.go @@ -14,6 +14,9 @@ func TestTVMaze(t *testing.T) { results, err := c.GetShows(0) require.NoError(t, err) require.NotEmpty(t, len(results)) + results, err = c.GetShows(99999) // This should trigger a 404, as there aren't these many pages in the index + require.NoError(t, err) + require.Nil(t, results) }) t.Run("find show", func(t *testing.T) {