-
Notifications
You must be signed in to change notification settings - Fork 2
/
paywall_webhook.go
113 lines (91 loc) · 3.06 KB
/
paywall_webhook.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
102
103
104
105
106
107
108
109
110
111
112
113
package nodeless
import (
"context"
"fmt"
"net/http"
"net/url"
)
// GetPaywallWebooks gets a list of PaywallWebhooks.
func (c *Client) GetPaywallWebhooks(ctx context.Context, paywallID string) ([]Webhook, error) {
endpoint, err := url.JoinPath(c.config.apiBase(), "api/v1/paywall", paywallID, "webhook")
if err != nil {
return nil, fmt.Errorf("url JoinPath: %w", err)
}
var resp struct {
Data []Webhook `json:"data"`
}
if err := c.do(ctx, http.MethodGet, endpoint, nil, &resp); err != nil {
return nil, err
}
return resp.Data, nil
}
// CreatePaywallWebhookRequest is a request to create a Paywall Webhook.
type CreatePaywallWebhookRequest struct {
PaywallID string `json:"-"`
Type WebhookType `json:"type"`
URL string `json:"url"`
Events []WebhookEvent `json:"events"`
Secret string `json:"secret"`
Status WebhookStatus `json:"status"`
}
// CreatePaywallWebhook creates a new Paywall Webhook.
func (c *Client) CreatePaywallWebhook(ctx context.Context, r CreatePaywallWebhookRequest) (*Webhook, error) {
endpoint, err := url.JoinPath(c.config.apiBase(), "api/v1/paywall", r.PaywallID, "webhook")
if err != nil {
return nil, fmt.Errorf("url JoinPath: %w", err)
}
var resp struct {
Data Webhook `json:"data"`
}
if err := c.do(ctx, http.MethodPost, endpoint, r, &resp); err != nil {
return nil, err
}
return &resp.Data, nil
}
// GetPaywallWebhook gets a Paywall Webhook.
func (c *Client) GetPaywallWebhook(ctx context.Context, paywallID, id string) (*Webhook, error) {
endpoint, err := url.JoinPath(c.config.apiBase(), "api/v1/paywall", paywallID, "webhook", id)
if err != nil {
return nil, fmt.Errorf("url JoinPath: %w", err)
}
var resp struct {
Data Webhook `json:"data"`
}
if err := c.do(ctx, http.MethodGet, endpoint, nil, &resp); err != nil {
return nil, err
}
return &resp.Data, nil
}
// DeletePaywallWebhook deletes a Paywall Webhook.
func (c *Client) DeletePaywallWebhook(ctx context.Context, paywallID, id string) error {
endpoint, err := url.JoinPath(c.config.apiBase(), "api/v1/paywall", paywallID, "webhook", id)
if err != nil {
return fmt.Errorf("url JoinPath: %w", err)
}
if err := c.do(ctx, http.MethodDelete, endpoint, nil, nil); err != nil {
return err
}
return nil
}
// UpdatePaywallWebhookRequest is a request to update a Paywall Webhook.
type UpdatePaywallWebhookRequest struct {
PaywallID string `json:"-"`
WebhookID string `json:"-"`
URL string `json:"url"`
Events []WebhookEvent `json:"events"`
Status WebhookStatus `json:"status"`
}
// UpdatePaywallWebhook updates a Paywall Webhook.
func (c *Client) UpdatePaywallWebhook(ctx context.Context, r UpdatePaywallWebhookRequest) (*Webhook, error) {
endpoint, err := url.JoinPath(c.config.apiBase(), "api/v1/paywall", r.PaywallID, "webhook", r.WebhookID)
if err != nil {
return nil, fmt.Errorf("url JoinPath: %w", err)
}
var resp struct {
Data Webhook `json:"data"`
}
if err := c.do(ctx, http.MethodPut, endpoint, r, &resp); err != nil {
return nil, err
}
return &resp.Data, nil
}