1
|
|
|
package client |
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
|
|
|
// https://docs.lemonsqueezy.com/api/products#retrieve-a-product |
14
|
|
|
func (service *ProductsService) Get(ctx context.Context, productID string) (*ProductApiResponse, *Response, error) { |
15
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/products/"+productID) |
16
|
|
|
if err != nil { |
17
|
|
|
return nil, response, err |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
product := new(ProductApiResponse) |
21
|
|
|
if err = json.Unmarshal(*response.Body, product); err != nil { |
22
|
|
|
return nil, response, err |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return product, response, nil |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// List returns a paginated list of products. |
29
|
|
|
// https://docs.lemonsqueezy.com/api/products#list-all-products |
30
|
|
|
func (service *ProductsService) List(ctx context.Context) (*ProductsApiResponse, *Response, error) { |
31
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/products") |
32
|
|
|
if err != nil { |
33
|
|
|
return nil, response, err |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
products := new(ProductsApiResponse) |
37
|
|
|
if err = json.Unmarshal(*response.Body, products); err != nil { |
38
|
|
|
return nil, response, err |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return products, response, nil |
42
|
|
|
} |
43
|
|
|
|