generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsubscription_items_service.go
101 lines (84 loc) · 3.28 KB
/
subscription_items_service.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package lemonsqueezy
import (
"context"
"encoding/json"
"net/http"
"net/url"
)
// SubscriptionItemsService is the API client for the `/subscription-items` endpoint
type SubscriptionItemsService service
// Update a subscription item
//
// https://docs.lemonsqueezy.com/api/subscription-items#update-a-subscription-item
func (service *SubscriptionItemsService) Update(ctx context.Context, params *SubscriptionItemUpdateParams) (*SubscriptionItemApiResponse, *Response, error) {
payload := map[string]any{
"data": map[string]any{
"type": "subscription-items",
"id": params.ID,
"attributes": params.Attributes,
},
}
response, err := service.client.do(ctx, http.MethodPatch, "/v1/subscriptions-items/"+params.ID, payload)
if err != nil {
return nil, response, err
}
subscriptionItem := new(SubscriptionItemApiResponse)
if err = json.Unmarshal(*response.Body, subscriptionItem); err != nil {
return nil, response, err
}
return subscriptionItem, response, nil
}
// List returns a paginated list of subscription items you can add extra query params to your request
//
// https://docs.lemonsqueezy.com/api/subscriptions#list-all-subscriptions
func (service *SubscriptionItemsService) List(ctx context.Context, queryParams map[string]string) (*SubscriptionItemsApiResponse, *Response, error) {
basePath := "/v1/subscription-items"
parsedURL, err := url.Parse(basePath)
if err != nil {
return nil, nil, err
}
if queryParams != nil {
query := parsedURL.Query()
for key, val := range queryParams {
query.Add(key, val)
}
parsedURL.RawQuery = query.Encode()
}
response, err := service.client.do(ctx, http.MethodGet, parsedURL.String())
if err != nil {
return nil, response, err
}
subscriptionItems := new(SubscriptionItemsApiResponse)
if err = json.Unmarshal(*response.Body, subscriptionItems); err != nil {
return nil, response, err
}
return subscriptionItems, response, nil
}
// Get returns the subscription item with the given ID.
//
// https://docs.lemonsqueezy.com/api/subscription-items#retrieve-a-subscription-item
func (service *SubscriptionItemsService) Get(ctx context.Context, subscriptionItemID string) (*SubscriptionItemApiResponse, *Response, error) {
response, err := service.client.do(ctx, http.MethodGet, "/v1/subscription-items/"+subscriptionItemID)
if err != nil {
return nil, response, err
}
subscriptionItem := new(SubscriptionItemApiResponse)
if err = json.Unmarshal(*response.Body, subscriptionItem); err != nil {
return nil, response, err
}
return subscriptionItem, response, nil
}
// Current usage returns a subscription item's current usage with the given ID.
//
// https://docs.lemonsqueezy.com/api/subscription-items#retrieve-a-subscription-item-s-current-usage
func (service *SubscriptionItemsService) CurrentUsage(ctx context.Context, subscriptionItemID string) (*SubscriptionItemCurrentUsageApiResponse, *Response, error) {
response, err := service.client.do(ctx, http.MethodGet, "/v1/subscription-items/"+subscriptionItemID+"/current-usage")
if err != nil {
return nil, response, err
}
subscriptionItemCurrentUsage := new(SubscriptionItemCurrentUsageApiResponse)
if err = json.Unmarshal(*response.Body, subscriptionItemCurrentUsage); err != nil {
return nil, response, err
}
return subscriptionItemCurrentUsage, response, nil
}