generated from milosgajdos/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhooks.go
213 lines (177 loc) · 4.66 KB
/
webhooks.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package vocode
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/url"
"github.com/milosgajdos/go-vocode/request"
)
type Event string
const (
MessageEvent Event = "event_message"
ActionEvent Event = "event_action"
CallConnectedEvent Event = "event_phone_call_connected"
CallEndedEvent Event = "event_phone_call_ended"
CallDidntConnectEvent Event = "event_phone_call_did_not_connect"
TranscriptEvent Event = "event_transcript"
RecordingEvent Event = "event_recording"
HumanDetectionEvent Event = "event_human_detection"
)
type WebhookMethod string
const (
GetWebhook WebhookMethod = "GET"
PostWebhook WebhookMethod = "POST"
)
type Webhooks struct {
Items []Webhook `json:"items"`
*Paging
}
type Webhook struct {
ID string `json:"id,omitempty"`
UserID string `json:"user_id,omitempty"`
Subs []Event `json:"subscriptions,omitempty"`
URL string `json:"url,omitempty"`
Method WebhookMethod `json:"method,omitempty"`
}
func (w *Webhook) UnmarshalJSON(data []byte) error {
var id string
if err := json.Unmarshal(data, &id); err == nil {
w.ID = id
return nil
}
type Alias Webhook
aux := &struct {
*Alias
}{
Alias: (*Alias)(w),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
return nil
}
type WebhookReq struct {
Subs []Event `json:"subscriptions"`
URL string `json:"url"`
Method WebhookMethod `json:"method"`
}
type CreateWebhookReq struct {
WebhookReq
}
type UpdateWebhookReq struct {
WebhookReq
}
func (c *Client) ListWebhooks(ctx context.Context, paging *PageParams) (*Webhooks, error) {
u, err := url.Parse(c.opts.BaseURL + "/" + c.opts.Version + "/webhooks/list")
if err != nil {
return nil, err
}
options := []request.HTTPOption{
request.WithBearer(c.opts.APIKey),
}
if paging != nil {
request.WithPageParams(paging.Encode())
}
req, err := request.NewHTTP(ctx, http.MethodGet, u.String(), nil, options...)
if err != nil {
return nil, err
}
resp, err := request.Do[*APIError](c.opts.HTTPClient, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
webhooks := new(Webhooks)
if err := json.NewDecoder(resp.Body).Decode(webhooks); err != nil {
return nil, err
}
return webhooks, nil
}
func (c *Client) GetWebhook(ctx context.Context, id string) (*Webhook, error) {
u, err := url.Parse(c.opts.BaseURL + "/" + c.opts.Version + "/webhooks")
if err != nil {
return nil, err
}
options := []request.HTTPOption{
request.WithBearer(c.opts.APIKey),
}
req, err := request.NewHTTP(ctx, http.MethodGet, u.String(), nil, options...)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("id", id)
req.URL.RawQuery = q.Encode()
resp, err := request.Do[*APIError](c.opts.HTTPClient, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
webhook := new(Webhook)
if err := json.NewDecoder(resp.Body).Decode(webhook); err != nil {
return nil, err
}
return webhook, nil
}
func (c *Client) CreateWebhook(ctx context.Context, createReq *CreateWebhookReq) (*Webhook, error) {
u, err := url.Parse(c.opts.BaseURL + "/" + c.opts.Version + "/webhooks/create")
if err != nil {
return nil, err
}
var body = &bytes.Buffer{}
enc := json.NewEncoder(body)
enc.SetEscapeHTML(false)
if err := enc.Encode(createReq); err != nil {
return nil, err
}
options := []request.HTTPOption{
request.WithBearer(c.opts.APIKey),
}
req, err := request.NewHTTP(ctx, http.MethodPost, u.String(), body, options...)
if err != nil {
return nil, err
}
resp, err := request.Do[*APIError](c.opts.HTTPClient, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
webhook := new(Webhook)
if err := json.NewDecoder(resp.Body).Decode(webhook); err != nil {
return nil, err
}
return webhook, nil
}
func (c *Client) UpdateWebhook(ctx context.Context, id string, updateReq *UpdateWebhookReq) (*Webhook, error) {
u, err := url.Parse(c.opts.BaseURL + "/" + c.opts.Version + "/webhooks/update")
if err != nil {
return nil, err
}
var body = &bytes.Buffer{}
enc := json.NewEncoder(body)
enc.SetEscapeHTML(false)
if err := enc.Encode(updateReq); err != nil {
return nil, err
}
options := []request.HTTPOption{
request.WithBearer(c.opts.APIKey),
}
req, err := request.NewHTTP(ctx, http.MethodPost, u.String(), body, options...)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("id", id)
req.URL.RawQuery = q.Encode()
resp, err := request.Do[*APIError](c.opts.HTTPClient, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
webhook := new(Webhook)
if err := json.NewDecoder(resp.Body).Decode(webhook); err != nil {
return nil, err
}
return webhook, nil
}