Fix doAPI pagination

This commit is contained in:
178inaba 2017-05-04 23:35:00 +09:00
parent d32c4d4ed1
commit eacd4e066d

View File

@ -45,7 +45,10 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
if values, ok := params.(url.Values); ok { if values, ok := params.(url.Values); ok {
var body io.Reader var body io.Reader
if method == http.MethodGet { if method == http.MethodGet {
u.RawQuery = pg.setValues(values).Encode() if pg != nil {
values = pg.setValues(values)
}
u.RawQuery = values.Encode()
} else { } else {
body = strings.NewReader(values.Encode()) body = strings.NewReader(values.Encode())
} }
@ -80,6 +83,9 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
} }
ct = mw.FormDataContentType() ct = mw.FormDataContentType()
} else { } else {
if method == http.MethodGet && pg != nil {
u.RawQuery = pg.toValues().Encode()
}
req, err = http.NewRequest(method, u.String(), nil) req, err = http.NewRequest(method, u.String(), nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -253,17 +259,19 @@ func getPaginationID(rawurl, key string) (int64, error) {
return id, nil return id, nil
} }
func (p *Pagination) toValues() url.Values {
return p.setValues(url.Values{})
}
func (p *Pagination) setValues(params url.Values) url.Values { func (p *Pagination) setValues(params url.Values) url.Values {
if p != nil {
if p.MaxID != nil { if p.MaxID != nil {
params.Set("max_id", fmt.Sprint(p.MaxID)) params.Set("max_id", fmt.Sprint(*p.MaxID))
} }
if p.SinceID != nil { if p.SinceID != nil {
params.Set("since_id", fmt.Sprint(p.SinceID)) params.Set("since_id", fmt.Sprint(*p.SinceID))
} }
if p.Limit != nil { if p.Limit != nil {
params.Set("limit", fmt.Sprint(p.Limit)) params.Set("limit", fmt.Sprint(*p.Limit))
}
} }
return params return params