~samwhited/paddle

c4f67c369314bf826814fdfc58dc90962f0b1260 — Sam Whited 2 years ago b2c09c1
paddle, product: move product code to new package
1 files changed, 20 insertions(+), 12 deletions(-)

R products.go => product/products.go
R products.go => product/products.go +20 -12
@@ 1,26 1,34 @@
package paddle
// Package product provides the /product/*_product APIs.
package product

import (
	"bytes"
	"encoding/json"

	"soquee.net/paddle"
)

// Client performs operations related to products.
type Client struct {
	*paddle.Client
}

const listProductsURL = "/product/get_products"

// ListProducts requests a list of proucts available on your account and returns
// an iterator that decodes them from the response body.
// List requests a list of proucts available on your account and returns an
// iterator that decodes them from the response body.
// If an error is returned, it is decoded and returned immediately.
func (c *Client) ListProducts() (*Products, error) {
func (c Client) List() (Iter, error) {
	msg, err := c.Do(listProductsURL, nil)
	if err != nil {
		return nil, err
		return Iter{}, err
	}
	pd := &productsDecode{}
	err = json.Unmarshal(msg, pd)
	if err != nil {
		return nil, err
		return Iter{}, err
	}
	return &Products{
	return Iter{
		Total: pd.Total,
		Count: pd.Count,
		d:     json.NewDecoder(bytes.NewReader(pd.Products)),


@@ 33,21 41,21 @@ type productsDecode struct {
	Products json.RawMessage `json:"products"`
}

// Products provides a mechanism for lazily decoding and iterating over a list
// of products.
type Products struct {
// Iter provides a mechanism for lazily decoding and iterating over a list of
// products.
type Iter struct {
	Total int
	Count int
	d     *json.Decoder
}

// Next returns true if there are more products to decode.
func (p Products) Next() bool {
func (p Iter) Next() bool {
	return p.d.More()
}

// Decode returns the next product.
func (p Products) Decode() (*Product, error) {
func (p Iter) Decode() (*Product, error) {
	prod := &Product{}
	err := p.d.Decode(prod)
	return prod, err