-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
90 lines (68 loc) · 1.55 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
package yandex
import (
"encoding/json"
"log"
"github.com/google/go-querystring/query"
)
type Webhook struct {
Id string `json:"id"`
Event string `json:"event"`
Url string `json:"url"`
}
type WebhooksListResponse struct {
Type string `json:"type"`
Items []*Webhook `json:"items"`
}
func (y *Yandex) SubscribeToWebhook(idempKey string, req *Webhook) (*Webhook, error) {
q, err := query.Values(req)
if err != nil {
log.Printf("Failed creating query: %v\n", err)
return nil, err
}
r := &HttpRequest{
Method: "POST",
Path: "/webhooks",
IdempotenceKey: idempKey,
OAuthToken: y.OAuthToken,
Data: q,
}
bytes, err := r.SendRequest()
if err != nil {
return nil, err
}
res := &Webhook{}
if err := json.Unmarshal(bytes, res); err != nil {
log.Printf("Failed unmarshaling bytes to struct: %v\n", err)
return nil, err
}
return res, nil
}
func (y *Yandex) GetWebhooksList() (*WebhooksListResponse, error) {
r := &HttpRequest{
Method: "GET",
Path: "/webhooks",
OAuthToken: y.OAuthToken,
}
bytes, err := r.SendRequest()
if err != nil {
return nil, err
}
res := &WebhooksListResponse{}
if err := json.Unmarshal(bytes, res); err != nil {
log.Printf("Failed unmarshaling bytes to struct: %v\n", err)
return nil, err
}
return res, nil
}
func (y *Yandex) DeleteWebhook(id string) error {
r := &HttpRequest{
Method: "DELETE",
Path: "/webhooks/" + id,
OAuthToken: y.OAuthToken,
}
_, err := r.SendRequest()
if err != nil {
return err
}
return nil
}