forked from EasyPost/easypost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.go
More file actions
89 lines (75 loc) · 3 KB
/
webhook.go
File metadata and controls
89 lines (75 loc) · 3 KB
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
package easypost
import (
"context"
"time"
)
// A Webhook represents an EasyPost webhook callback URL.
type Webhook struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
Mode string `json:"mode,omitempty"`
URL string `json:"url,omitempty"`
DisabledAt *time.Time `json:"disabled_at,omitempty"`
}
type createWebhookRequest struct {
Webhook *Webhook `json:"webhook,omitempty"`
}
// CreateWebhook creates a new webhook with the given URL.
func (c *Client) CreateWebhook(u string) (out *Webhook, err error) {
req := &createWebhookRequest{Webhook: &Webhook{URL: u}}
err = c.post(nil, "webhooks", req, &out)
return
}
// CreateWebhookWithContext performs the same operation as CreateWebhook, but
// allows specifying a context that can interrupt the request.
func (c *Client) CreateWebhookWithContext(ctx context.Context, u string) (out *Webhook, err error) {
req := &createWebhookRequest{Webhook: &Webhook{URL: u}}
err = c.post(ctx, "webhooks", req, &out)
return
}
type listWebhooksResult struct {
Webhooks *[]*Webhook `json:"webhooks,omitempty"`
}
// ListWebhooks returns all webhooks associated with the EasyPost account.
func (c *Client) ListWebhooks() (out []*Webhook, err error) {
err = c.get(nil, "webhooks", &listWebhooksResult{Webhooks: &out})
return
}
// ListWebhooksWithContext performs the same operation as
// ListWebhooksWithContext, but allows specifying a context that can interrupt
// the request.
func (c *Client) ListWebhooksWithContext(ctx context.Context) (out []*Webhook, err error) {
err = c.get(ctx, "webhooks", &listWebhooksResult{Webhooks: &out})
return
}
// GetWebhook retrieves a Webhook object with the given ID.
func (c *Client) GetWebhook(webhookID string) (out *Webhook, err error) {
err = c.get(nil, "webhooks/"+webhookID, &out)
return
}
// GetWebhookWithContext performs the same operation as GetWebhook, but allows
// specifying a context that can interrupt the request.
func (c *Client) GetWebhookWithContext(ctx context.Context, webhookID string) (out *Webhook, err error) {
err = c.get(ctx, "webhooks/"+webhookID, &out)
return
}
// EnableWebhook re-enables a disabled webhook.
func (c *Client) EnableWebhook(webhookID string) (out *Webhook, err error) {
err = c.put(nil, "webhooks/"+webhookID, nil, &out)
return
}
// EnableWebhookWithContext performs the same operation as EnableWebhook, but
// allows specifying a context that can interrupt the request.
func (c *Client) EnableWebhookWithContext(ctx context.Context, webhookID string) (out *Webhook, err error) {
err = c.put(ctx, "webhooks/"+webhookID, nil, &out)
return
}
// DeleteWebhook removes a webhook.
func (c *Client) DeleteWebhook(webhookID string) error {
return c.del(nil, "webhooks/"+webhookID)
}
// DeleteWebhookWithContext performs the same operation as DeleteWebhook, but
// allows specifying a context that can interrupt the request.
func (c *Client) DeleteWebhookWithContext(ctx context.Context, webhookID string) error {
return c.del(ctx, "webhooks/"+webhookID)
}