forked from getconversio/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
99 lines (85 loc) · 3.05 KB
/
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
package goshopify
import (
"fmt"
"time"
)
const webhooksBasePath = "admin/webhooks"
// WebhookService is an interface for interfacing with the webhook endpoints of
// the Shopify API.
// See: https://help.shopify.com/api/reference/webhook
type WebhookService interface {
List(interface{}) ([]Webhook, error)
Count(interface{}) (int, error)
Get(int, interface{}) (*Webhook, error)
Create(Webhook) (*Webhook, error)
Update(Webhook) (*Webhook, error)
Delete(int) error
}
// WebhookServiceOp handles communication with the webhook-related methods of
// the Shopify API.
type WebhookServiceOp struct {
client *Client
}
// Webhook represents a Shopify webhook
type Webhook struct {
ID int `json:"id"`
Address string `json:"address"`
Topic string `json:"topic"`
Format string `json:"format"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
Fields []string `json:"fields"`
MetafieldNamespaces []string `json:"metafield_namespaces"`
}
// WebhookOptions can be used for filtering webhooks on a List request.
type WebhookOptions struct {
Address string `url:"address,omitempty"`
Topic string `url:"topic,omitempty"`
}
// WebhookResource represents the result from the admin/webhooks.json endpoint
type WebhookResource struct {
Webhook *Webhook `json:"webhook"`
}
// WebhooksResource is the root object for a webhook get request.
type WebhooksResource struct {
Webhooks []Webhook `json:"webhooks"`
}
// List webhooks
func (s *WebhookServiceOp) List(options interface{}) ([]Webhook, error) {
path := fmt.Sprintf("%s.json", webhooksBasePath)
resource := new(WebhooksResource)
err := s.client.Get(path, resource, options)
return resource.Webhooks, err
}
// Count webhooks
func (s *WebhookServiceOp) Count(options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", webhooksBasePath)
return s.client.Count(path, options)
}
// Get individual webhook
func (s *WebhookServiceOp) Get(webhookdID int, options interface{}) (*Webhook, error) {
path := fmt.Sprintf("%s/%d.json", webhooksBasePath, webhookdID)
resource := new(WebhookResource)
err := s.client.Get(path, resource, options)
return resource.Webhook, err
}
// Create a new webhook
func (s *WebhookServiceOp) Create(webhook Webhook) (*Webhook, error) {
path := fmt.Sprintf("%s.json", webhooksBasePath)
wrappedData := WebhookResource{Webhook: &webhook}
resource := new(WebhookResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Webhook, err
}
// Update an existing webhook.
func (s *WebhookServiceOp) Update(webhook Webhook) (*Webhook, error) {
path := fmt.Sprintf("%s/%d.json", webhooksBasePath, webhook.ID)
wrappedData := WebhookResource{Webhook: &webhook}
resource := new(WebhookResource)
err := s.client.Put(path, wrappedData, resource)
return resource.Webhook, err
}
// Delete an existing webhooks
func (s *WebhookServiceOp) Delete(ID int) error {
return s.client.Delete(fmt.Sprintf("%s/%d.json", webhooksBasePath, ID))
}