1
|
|
|
package lemonsqueezy |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"net/http" |
7
|
|
|
"net/url" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// SubscriptionItemsService is the API client for the `/subscription-items` endpoint |
11
|
|
|
type SubscriptionItemsService service |
12
|
|
|
|
13
|
|
|
// Update a subscription item |
14
|
|
|
// |
15
|
|
|
// https://docs.lemonsqueezy.com/api/subscription-items#update-a-subscription-item |
16
|
|
|
func (service *SubscriptionItemsService) Update(ctx context.Context, params *SubscriptionItemUpdateParams) (*SubscriptionItemApiResponse, *Response, error) { |
17
|
|
|
payload := map[string]any{ |
18
|
|
|
"data": map[string]any{ |
19
|
|
|
"type": "subscription-items", |
20
|
|
|
"id": params.ID, |
21
|
|
|
"attributes": params.Attributes, |
22
|
|
|
}, |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
response, err := service.client.do(ctx, http.MethodPatch, "/v1/subscriptions-items/"+params.ID, payload) |
26
|
|
|
if err != nil { |
27
|
|
|
return nil, response, err |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
subscriptionItem := new(SubscriptionItemApiResponse) |
31
|
|
|
if err = json.Unmarshal(*response.Body, subscriptionItem); err != nil { |
32
|
|
|
return nil, response, err |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return subscriptionItem, response, nil |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// List returns a paginated list of subscription items you can add extra query params to your request |
39
|
|
|
// |
40
|
|
|
// https://docs.lemonsqueezy.com/api/subscriptions#list-all-subscriptions |
41
|
|
|
func (service *SubscriptionItemsService) List(ctx context.Context, queryParams map[string]string) (*SubscriptionItemsApiResponse, *Response, error) { |
42
|
|
|
basePath := "/v1/subscription-items" |
43
|
|
|
parsedURL, err := url.Parse(basePath) |
44
|
|
|
if err != nil { |
45
|
|
|
return nil, nil, err |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if queryParams != nil { |
49
|
|
|
query := parsedURL.Query() |
50
|
|
|
for key, val := range queryParams { |
51
|
|
|
query.Add(key, val) |
52
|
|
|
} |
53
|
|
|
parsedURL.RawQuery = query.Encode() |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
response, err := service.client.do(ctx, http.MethodGet, parsedURL.String()) |
57
|
|
|
if err != nil { |
58
|
|
|
return nil, response, err |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
subscriptionItems := new(SubscriptionItemsApiResponse) |
62
|
|
|
if err = json.Unmarshal(*response.Body, subscriptionItems); err != nil { |
63
|
|
|
return nil, response, err |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return subscriptionItems, response, nil |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Get returns the subscription item with the given ID. |
70
|
|
|
// |
71
|
|
|
// https://docs.lemonsqueezy.com/api/subscription-items#retrieve-a-subscription-item |
72
|
|
|
func (service *SubscriptionItemsService) Get(ctx context.Context, subscriptionItemID string) (*SubscriptionItemApiResponse, *Response, error) { |
73
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/subscription-items/"+subscriptionItemID) |
74
|
|
|
if err != nil { |
75
|
|
|
return nil, response, err |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
subscriptionItem := new(SubscriptionItemApiResponse) |
79
|
|
|
if err = json.Unmarshal(*response.Body, subscriptionItem); err != nil { |
80
|
|
|
return nil, response, err |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return subscriptionItem, response, nil |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Current usage returns a subscription item's current usage with the given ID. |
87
|
|
|
// |
88
|
|
|
// https://docs.lemonsqueezy.com/api/subscription-items#retrieve-a-subscription-item-s-current-usage |
89
|
|
|
func (service *SubscriptionItemsService) CurrentUsage(ctx context.Context, subscriptionItemID string) (*SubscriptionItemCurrentUsageApiResponse, *Response, error) { |
90
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/subscription-items/"+subscriptionItemID+"/current-usage") |
91
|
|
|
if err != nil { |
92
|
|
|
return nil, response, err |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
subscriptionItemCurrentUsage := new(SubscriptionItemCurrentUsageApiResponse) |
96
|
|
|
if err = json.Unmarshal(*response.Body, subscriptionItemCurrentUsage); err != nil { |
97
|
|
|
return nil, response, err |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return subscriptionItemCurrentUsage, response, nil |
101
|
|
|
} |
102
|
|
|
|