This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
forked from rpip/upvest-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
171 lines (148 loc) · 5.04 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package upvest
import (
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/pkg/errors"
)
// Webhook represents an Upvest webhook
type Webhook struct {
ID string `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
HMACSecretKey string `json:"hmac_secret_key"`
Headers map[string]string `json:"headers"`
Version string `json:"version"`
Status string `json:"status"`
//EventFilters []map[string]EventFilter `json:"event_filters"`
// TODO: report inconsistent response schema. event filters should be: [string -> map]
// temporarily decode to generic interface
EventFilters []interface{} `json:"event_filters"`
}
// EventFilterScope represents one of the configured event filter scopes
type EventFilterScope string
// EventFilter represents serialized event filter as returned from the server
type EventFilter struct {
EventNoun string `json:"event_noun"`
EventVerb string `json:"event_verb"`
LimitToApplication bool `json:"limit_to_application"`
MaxConfirmations int `json:"max_confirmations"`
ProtocolName string `json:"procol_name"`
WalletAddress string `json:"wallet_address"`
}
// WebhookParams is the set of parameters that can be used when creating a webhook
type WebhookParams struct {
URL string `json:"url"`
Name string `json:"name"`
HMACSecretKey string `json:"hmac_secret_key"`
Headers map[string]string `json:"headers"`
Version string `json:"version"`
Status string `json:"status"`
// An array of platform, noun and verb combinations capturing desired events.
EventFilters []EventFilterScope `json:"event_filters"`
}
// WebhookService handles operations related to the webhook
type WebhookService struct {
service
}
// WebhookList is a list object for webhooks.
type WebhookList struct {
Meta ListMeta
Values []Webhook `json:"results"`
}
// Create creates a new webhook
// Unlike other resource enndpoints, we can use the same webhook struct to create a new one
// as the parameters required to create a new one is basically all the fields in the webhook struct.
// Only difference being that it has not yet been saved on Upvest backend
// TODO: validate params
func (s *WebhookService) Create(wh *WebhookParams) (*Webhook, error) {
u := "/tenancy/webhooks/"
webhook := &Webhook{}
p := NewParams(s.auth)
err := s.client.Call(http.MethodPost, u, wh, webhook, p)
return webhook, err
}
// Get retrives and returns a webhook object.
func (s *WebhookService) Get(webhookID string) (*Webhook, error) {
u := fmt.Sprintf("/tenancy/webhooks/%s", webhookID)
webhook := &Webhook{}
p := NewParams(s.auth)
err := s.client.Call(http.MethodGet, u, nil, webhook, p)
return webhook, err
}
// List returns list of all webhooks.
func (s *WebhookService) List() (*WebhookList, error) {
path := "/tenancy/webhooks/"
u, _ := url.Parse(path)
p := &Params{}
p.SetAuthProvider(s.auth)
var results []Webhook
webhooks := &WebhookList{}
for {
err := s.client.Call(http.MethodGet, u.String(), nil, webhooks, p)
if err != nil {
return nil, errors.Wrap(err, "Could not retrieve list of webhooks")
}
results = append(results, webhooks.Values...)
// append page_size param to the returned params
u1, err := url.Parse(webhooks.Meta.Next)
if err != nil {
return nil, errors.Wrap(err, "Can not parse url")
}
q := u1.Query()
q.Set("page_size", strconv.Itoa(MaxPageSize))
u.RawQuery = q.Encode()
if webhooks.Meta.Next == "" {
break
}
}
return &WebhookList{Values: results}, nil
}
// ListN returns a specific number of webhooks
func (s *WebhookService) ListN(count int) (*WebhookList, error) {
path := "/tenancy/webhooks/"
u, _ := url.Parse(path)
p := &Params{}
p.SetAuthProvider(s.auth)
var results []Webhook
webhooks := &WebhookList{}
total := 0
for total <= count {
err := s.client.Call(http.MethodGet, u.String(), nil, webhooks, p)
if err != nil {
return nil, errors.Wrap(err, "Could not retrieve list of webhooks")
}
results = append(results, webhooks.Values...)
total += len(webhooks.Values)
// append page_size param to the returned params
u1, err := url.Parse(webhooks.Meta.Next)
if err != nil {
return nil, errors.Wrap(err, "Can not parse url")
}
q := u1.Query()
q.Set("page_size", strconv.Itoa(MaxPageSize))
u.RawQuery = q.Encode()
if webhooks.Meta.Next == "" {
break
}
}
return &WebhookList{Values: results}, nil
}
// Delete permanently deletes a webhook
func (s *WebhookService) Delete(webhookID string) error {
u := fmt.Sprintf("/tenancy/webhooks/%s", webhookID)
resp := &Response{}
p := NewParams(s.auth)
err := s.client.Call(http.MethodDelete, u, map[string]string{}, resp, p)
return err
}
// Verify a webhook
func (s *WebhookService) Verify(url string) bool {
u := "/tenancy/webhooks-verify/"
body := map[string]string{"verify_url": url}
resp := &Response{}
p := NewParams(s.auth)
err := s.client.Call(http.MethodPost, u, body, resp, p)
return err == nil
}