1
|
|
|
package lemonsqueezy |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"net/http" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
// ProductsService is the API client for the `/v1/products` endpoint |
10
|
|
|
type ProductsService service |
11
|
|
|
|
12
|
|
|
// Get returns the product with the given ID. |
13
|
|
|
// |
14
|
|
|
// https://docs.lemonsqueezy.com/api/products/retrieve-product |
15
|
|
|
func (service *ProductsService) Get(ctx context.Context, productID string) (*ProductApiResponse, *Response, error) { |
16
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/products/"+productID) |
17
|
|
|
if err != nil { |
18
|
|
|
return nil, response, err |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
product := new(ProductApiResponse) |
22
|
|
|
if err = json.Unmarshal(*response.Body, product); err != nil { |
23
|
|
|
return nil, response, err |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return product, response, nil |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// List returns a paginated list of products. |
30
|
|
|
// |
31
|
|
|
// https://docs.lemonsqueezy.com/api/products/list-all-products |
32
|
|
|
func (service *ProductsService) List(ctx context.Context, filtersKV ...string) (*ProductsApiResponse, *Response, error) { |
33
|
|
|
response, err := service.client.do(ctx, http.MethodGet, pathWithFilters("/v1/products", filtersKV...)) |
34
|
|
|
if err != nil { |
35
|
|
|
return nil, response, err |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
products := new(ProductsApiResponse) |
39
|
|
|
if err = json.Unmarshal(*response.Body, products); err != nil { |
40
|
|
|
return nil, response, err |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return products, response, nil |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// ListWithVariants returns a paginated list of products with variants. |
47
|
|
|
// |
48
|
|
|
// https://docs.lemonsqueezy.com/api/products/list-all-products |
49
|
|
|
func (service *ProductsService) ListWithVariants(ctx context.Context, filtersKV ...string) (*ProductsWithVariantsApiResponse, *Response, error) { |
50
|
|
|
response, err := service.client.do(ctx, http.MethodGet, pathWithFilters("/v1/products?include=variants", filtersKV...)) |
51
|
|
|
if err != nil { |
52
|
|
|
return nil, response, err |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
products := new(ProductsWithVariantsApiResponse) |
56
|
|
|
if err = json.Unmarshal(*response.Body, products); err != nil { |
57
|
|
|
return nil, response, err |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return products, response, nil |
61
|
|
|
} |
62
|
|
|
|