-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebhook.go
94 lines (80 loc) · 2.51 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
package spark
import (
"bytes"
"encoding/json"
"errors"
"net/url"
)
const WebhookUrl = "https://api.ciscospark.com/v1/webhooks"
type Webhook struct {
Id string `json:"id"`
Name string `json:"name"`
TargetUrl string `json:"targetUrl"`
Resource string `json:"resource"`
Event string `json:"event"`
Filter string `json:"filter,omitempty"`
Secret string `json:"secret,omitempty"`
OrgId string `json:"orgId,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
AppId string `json:"appId,omitempty"`
OwnedBy string `json:"ownedBy,omitempty"`
Status string `json:"active,omitempty"`
ActorId string `json:"actorId,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
}
// https://developer.ciscospark.com/resource-webhooks.html
type Webhooks struct {
Items []Webhook
}
func (s *Spark) ListWebhooks(uv *url.Values) ([]Webhook, error) {
var w Webhooks
// parameter: max is the only accepted right now.
// https://developer.ciscospark.com/endpoint-webhooks-get.html
bytes, err := s.GetRequest(WebhookUrl, uv)
if err != nil {
return w.Items, err
}
err = json.Unmarshal(bytes, &w)
return w.Items, err
}
func (s *Spark) CreateWebhook(w Webhook) (Webhook, error) {
var rwh Webhook
if w.Name == "" {
return rwh, errors.New("You must specify a name for the webhook")
}
if w.TargetUrl == "" {
return rwh, errors.New("You must specify a target URL for the webhook")
}
// see: https://developer.ciscospark.com/webhooks-explained.html
// resource should be the plural form of a spark api:
// * messages
// * memberships
// * rooms
// * teams
if w.Resource == "" {
return rwh, errors.New("You must specify a Resource for the webhook")
}
// created, updated, deleted, or all
if w.Event == "" {
return rwh, errors.New("You should specify an event for the webhook resource")
}
/* generally, you'll probably want:
{"resource" : "messages", "event" : "created" }
*/
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(w)
bytes, err := s.PostRequest(WebhookUrl, b)
if err != nil {
return rwh, err
}
err = json.Unmarshal(bytes, &rwh)
return rwh, err
}
func (s *Spark) DeleteWebhook(w Webhook) error {
if w.Id == "" {
return errors.New("Must specify the Webhook ID to delete")
}
url := WebhookUrl + "/" + w.Id
_, err := s.DeleteRequest(url)
return err
}