From 398d06954884bf980c9bbbbf6d00d89087c9dd16 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Tue, 29 Apr 2014 14:37:47 -0400 Subject: [PATCH] ComicsParams --- example/main.go | 2 +- marvel.go | 37 ++++++++++++++++++++++--------------- marvel_test.go | 2 +- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/example/main.go b/example/main.go index 0858a84..3e65d89 100644 --- a/example/main.go +++ b/example/main.go @@ -30,7 +30,7 @@ func main() { limit := 100 imgs := []image.Image{} for { - r, err := c.Series(*seriesID).Comics(marvel.CommonParams{offset, limit}) + r, err := c.Series(*seriesID).Comics(marvel.ComicsParams{Offset: offset, Limit: limit}) if err != nil { panic(err) } diff --git a/marvel.go b/marvel.go index 5034b5f..de2e079 100644 --- a/marvel.go +++ b/marvel.go @@ -52,7 +52,7 @@ func (c Client) baseURL(path string, params interface{}) url.URL { } // Fields common to all response entities -type commonResponse struct { +type CommonResponse struct { Code int ETag string Status string @@ -61,13 +61,25 @@ type commonResponse struct { AttributionHTML string } +type ComicsResponse struct { + CommonResponse + Data struct { + CommonList + Results []Comic + } +} + type CommonParams struct { Offset int `url:"offset,omitempty"` Limit int `url:"limit,omitempty"` } +type ComicsParams struct { + CommonParams +} + // Fields common to data that lists entities, with pagination -type commonList struct { +type CommonList struct { Offset int Limit int Total int @@ -118,30 +130,25 @@ func (d Date) Parse() time.Time { return t } -func (c Client) Series(id int) SeriesRequest { - return SeriesRequest{seriesID: id, client: c} +func (c Client) Series(id int) SeriesResource { + return SeriesResource{seriesID: id, client: c} } -type SeriesRequest struct { +type SeriesResource struct { seriesID int - client Client + client Client } -func (s SeriesRequest) Comics(params CommonParams) (resp struct{ - commonResponse - Data struct { - commonList - Results []Comic - } -}, err error) { +func (s SeriesResource) Comics(params ComicsParams) (*ComicsResponse, error) { u := s.client.baseURL(fmt.Sprintf("series/%d/comics", s.seriesID), params) r, err := s.client.fetch(u) if err != nil { - return + return nil, err } defer r.Close() + var resp ComicsResponse err = json.NewDecoder(r).Decode(&resp) - return + return &resp, err } func (c Client) fetch(u url.URL) (io.ReadCloser, error) { diff --git a/marvel_test.go b/marvel_test.go index 3f2eac0..76c3678 100644 --- a/marvel_test.go +++ b/marvel_test.go @@ -13,7 +13,7 @@ var ( func TestRequest(t *testing.T) { flag.Parse() - r, err := NewClient(*apiKey, *secret).Series(2258).Comics(CommonParams{}) + r, err := NewClient(*apiKey, *secret).Series(2258).Comics(ComicsParams{}) if err != nil { t.Errorf("error: %v", err) return