mirror of
https://github.com/imjasonh/go-marvel
synced 2026-07-21 14:47:15 +00:00
ComicsParams
This commit is contained in:
parent
fd93910594
commit
398d069548
3 changed files with 24 additions and 17 deletions
|
|
@ -30,7 +30,7 @@ func main() {
|
||||||
limit := 100
|
limit := 100
|
||||||
imgs := []image.Image{}
|
imgs := []image.Image{}
|
||||||
for {
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
37
marvel.go
37
marvel.go
|
|
@ -52,7 +52,7 @@ func (c Client) baseURL(path string, params interface{}) url.URL {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fields common to all response entities
|
// Fields common to all response entities
|
||||||
type commonResponse struct {
|
type CommonResponse struct {
|
||||||
Code int
|
Code int
|
||||||
ETag string
|
ETag string
|
||||||
Status string
|
Status string
|
||||||
|
|
@ -61,13 +61,25 @@ type commonResponse struct {
|
||||||
AttributionHTML string
|
AttributionHTML string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ComicsResponse struct {
|
||||||
|
CommonResponse
|
||||||
|
Data struct {
|
||||||
|
CommonList
|
||||||
|
Results []Comic
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type CommonParams struct {
|
type CommonParams struct {
|
||||||
Offset int `url:"offset,omitempty"`
|
Offset int `url:"offset,omitempty"`
|
||||||
Limit int `url:"limit,omitempty"`
|
Limit int `url:"limit,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ComicsParams struct {
|
||||||
|
CommonParams
|
||||||
|
}
|
||||||
|
|
||||||
// Fields common to data that lists entities, with pagination
|
// Fields common to data that lists entities, with pagination
|
||||||
type commonList struct {
|
type CommonList struct {
|
||||||
Offset int
|
Offset int
|
||||||
Limit int
|
Limit int
|
||||||
Total int
|
Total int
|
||||||
|
|
@ -118,30 +130,25 @@ func (d Date) Parse() time.Time {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) Series(id int) SeriesRequest {
|
func (c Client) Series(id int) SeriesResource {
|
||||||
return SeriesRequest{seriesID: id, client: c}
|
return SeriesResource{seriesID: id, client: c}
|
||||||
}
|
}
|
||||||
|
|
||||||
type SeriesRequest struct {
|
type SeriesResource struct {
|
||||||
seriesID int
|
seriesID int
|
||||||
client Client
|
client Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SeriesRequest) Comics(params CommonParams) (resp struct{
|
func (s SeriesResource) Comics(params ComicsParams) (*ComicsResponse, error) {
|
||||||
commonResponse
|
|
||||||
Data struct {
|
|
||||||
commonList
|
|
||||||
Results []Comic
|
|
||||||
}
|
|
||||||
}, err error) {
|
|
||||||
u := s.client.baseURL(fmt.Sprintf("series/%d/comics", s.seriesID), params)
|
u := s.client.baseURL(fmt.Sprintf("series/%d/comics", s.seriesID), params)
|
||||||
r, err := s.client.fetch(u)
|
r, err := s.client.fetch(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
|
var resp ComicsResponse
|
||||||
err = json.NewDecoder(r).Decode(&resp)
|
err = json.NewDecoder(r).Decode(&resp)
|
||||||
return
|
return &resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) fetch(u url.URL) (io.ReadCloser, error) {
|
func (c Client) fetch(u url.URL) (io.ReadCloser, error) {
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ var (
|
||||||
func TestRequest(t *testing.T) {
|
func TestRequest(t *testing.T) {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
r, err := NewClient(*apiKey, *secret).Series(2258).Comics(CommonParams{})
|
r, err := NewClient(*apiKey, *secret).Series(2258).Comics(ComicsParams{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error: %v", err)
|
t.Errorf("error: %v", err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue