~samwhited/paddle

ref: v0.0.3 paddle/product/products.go -rw-r--r-- 1.7 KiB
fa35a6a5Sam Whited paddle: add note about maintenance status 1 year, 6 months ago
                                                                                
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Package product provides APIs related to products.
package product

import (
	"bytes"
	"encoding/json"

	"code.soquee.net/paddle"
)

// Client performs API operations.
type Client struct {
	*paddle.Client
}

const listProductsURL = "/product/get_products"

// 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) List() (Iter, error) {
	msg, err := c.Do(listProductsURL, nil)
	if err != nil {
		return Iter{}, err
	}
	pd := &productsDecode{}
	err = json.Unmarshal(msg, pd)
	if err != nil {
		return Iter{}, err
	}
	return Iter{
		Total: pd.Total,
		Count: pd.Count,
		d:     json.NewDecoder(bytes.NewReader(pd.Products)),
	}, nil
}

type productsDecode struct {
	Total    int             `json:"total"`
	Count    int             `json:"count"`
	Products json.RawMessage `json:"products"`
}

// 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 Iter) Next() bool {
	return p.d.More()
}

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

// Product contains data from a single product.
type Product struct {
	ID          uint64   `json:"id"`
	Name        string   `json:"name"`
	Description string   `json:"description"`
	BasePrice   float64  `json:"base_price"`
	SalePrice   *int     `json:"sale_price"`
	Screenshots []string `json:"screenshots"`
	Icon        string   `json:"icon"`
}