generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsubscriptions_service.go
86 lines (71 loc) · 2.67 KB
/
subscriptions_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
package lemonsqueezy
import (
"context"
"encoding/json"
"net/http"
)
// SubscriptionsService is the API client for the `/subscriptions` endpoint
type SubscriptionsService service
// Update an existing subscription to specific parameter
//
// https://docs.lemonsqueezy.com/api/subscriptions#update-a-subscription
func (service *SubscriptionsService) Update(ctx context.Context, params *SubscriptionUpdateParams) (*SubscriptionApiResponse, *Response, error) {
payload := map[string]any{
"data": map[string]any{
"id": params.ID,
"type": "subscriptions",
"attributes": params.Attributes,
},
}
response, err := service.client.do(ctx, http.MethodPatch, "/v1/subscriptions/"+params.ID, payload)
if err != nil {
return nil, response, err
}
subscription := new(SubscriptionApiResponse)
if err = json.Unmarshal(*response.Body, subscription); err != nil {
return nil, response, err
}
return subscription, response, nil
}
// List returns a paginated list of subscriptions ordered by created_at (descending)
//
// https://docs.lemonsqueezy.com/api/subscriptions#list-all-subscriptions
func (service *SubscriptionsService) List(ctx context.Context) (*SubscriptionsApiResponse, *Response, error) {
response, err := service.client.do(ctx, http.MethodGet, "/v1/subscriptions")
if err != nil {
return nil, response, err
}
subscriptions := new(SubscriptionsApiResponse)
if err = json.Unmarshal(*response.Body, subscriptions); err != nil {
return nil, response, err
}
return subscriptions, response, nil
}
// Get returns the subscription with the given ID.
//
// https://docs.lemonsqueezy.com/api/subscriptions#retrieve-a-subscription
func (service *SubscriptionsService) Get(ctx context.Context, subscriptionID string) (*SubscriptionApiResponse, *Response, error) {
response, err := service.client.do(ctx, http.MethodGet, "/v1/subscriptions/"+subscriptionID)
if err != nil {
return nil, response, err
}
subscription := new(SubscriptionApiResponse)
if err = json.Unmarshal(*response.Body, subscription); err != nil {
return nil, response, err
}
return subscription, response, nil
}
// Cancel an active subscription the given ID.
//
// https://docs.lemonsqueezy.com/api/subscriptions#retrieve-a-subscription
func (service *SubscriptionsService) Cancel(ctx context.Context, subscriptionID string) (*SubscriptionApiResponse, *Response, error) {
response, err := service.client.do(ctx, http.MethodDelete, "/v1/subscriptions/"+subscriptionID)
if err != nil {
return nil, response, err
}
subscription := new(SubscriptionApiResponse)
if err = json.Unmarshal(*response.Body, subscription); err != nil {
return nil, response, err
}
return subscription, response, nil
}