-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_product.go
42 lines (35 loc) · 1.04 KB
/
list_product.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package sendowl
import (
"fmt"
"golang.org/x/net/context"
)
// ListProductsRequest is an API representation of a list products request.
type ListProductsRequest struct {
PerPage int
Page int
}
// ListProductsResponse is an API representation of a list products response.
type ListProductsResponse []ListProductsResponseItem
func (r *ListProductsResponse) Items() []ListProductsResponseItem {
return []ListProductsResponseItem(*r)
}
type ListProductsResponseItem struct {
Product `json:"product"`
}
// ListProducts uses req to list a products, returning a
// ListProductsResponse and non-nil error if there was a problem.
func (c Client) ListProducts(ctx context.Context, req ListProductsRequest) (*ListProductsResponse, error) {
u := "./products"
if req.PerPage > 0 || req.Page > 0 {
u = fmt.Sprintf("%s?per_page=%d&page=%d", u, req.PerPage, req.Page)
}
r, err := c.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
var resp ListProductsResponse
if err := c.do(ctx, r, &resp); err != nil {
return nil, err
}
return &resp, nil
}