forked from koltyakov/gosip
/
subscriptions.go
180 lines (161 loc) · 5.57 KB
/
subscriptions.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/pnocera/gosip"
)
//go:generate ggen -ent Subscriptions -item Subscription -conf -coll
//go:generate ggen -ent Subscription -conf
// API reference: https://docs.microsoft.com/en-us/sharepoint/dev/apis/webhooks/lists/overview-sharepoint-list-webhooks
// Subscriptions represent SharePoint lists Subscriptions API queryable collection struct
// Always use NewSubscriptions constructor instead of &Subscriptions{}
type Subscriptions struct {
client *gosip.SPClient
config *RequestConfig
endpoint string
}
// Subscription represent SharePoint lists Subscription API
// Always use NewSubscription constructor instead of &Subscription{}
type Subscription struct {
client *gosip.SPClient
config *RequestConfig
endpoint string
}
// SubscriptionInfo list subscription info
type SubscriptionInfo struct {
ID string `json:"id"`
// The service URL to send notifications to
NotificationURL string `json:"notificationUrl"`
// The date the notification will expire and be deleted
ExpirationDateTime time.Time `json:"expirationDateTime"`
// The URL of the list to receive notifications from
Resource string `json:"resource"`
// Optional. Opaque string passed back to the client on all notifications.
// You can use this for validating notifications or tagging different subscriptions.
ClientState string `json:"clientState"`
ResourceData string `json:"resourceData"`
}
// NewSubscriptions - Subscriptions struct constructor function
func NewSubscriptions(client *gosip.SPClient, endpoint string, config *RequestConfig) *Subscriptions {
return &Subscriptions{
client: client,
endpoint: endpoint,
config: config,
}
}
// NewSubscription - subscription struct constructor function
func NewSubscription(client *gosip.SPClient, endpoint string, config *RequestConfig) *Subscription {
return &Subscription{
client: client,
endpoint: endpoint,
config: config,
}
}
// Get gets list subscriptions response collection
func (subscriptions *Subscriptions) Get() ([]*SubscriptionInfo, error) {
client := NewHTTPClient(subscriptions.client)
resp, err := client.Get(subscriptions.endpoint, subscriptions.config)
if err != nil {
return nil, err
}
data, _ := NormalizeODataCollection(resp)
var subs []*SubscriptionInfo
if err := json.Unmarshal(data, &subs); err != nil {
return nil, err
}
return subs, nil
}
// GetByID gets list subscription by its ID (GUID)
func (subscriptions *Subscriptions) GetByID(subscriptionID string) *Subscription {
return NewSubscription(
subscriptions.client,
fmt.Sprintf("%s('%s')", subscriptions.endpoint, subscriptionID),
subscriptions.config,
)
}
// Add adds/updates new subscription to a list
func (subscriptions *Subscriptions) Add(notificationURL string, expiration time.Time, clientState string) (*SubscriptionInfo, error) {
client := NewHTTPClient(subscriptions.client)
payload := map[string]interface{}{
"notificationUrl": notificationURL,
"expirationDateTime": expiration,
"resource": getPriorEndpoint(subscriptions.endpoint, "/subscriptions"),
"clientState": clientState,
}
body, err := json.Marshal(payload)
if err != nil {
return nil, err
}
conf := patchConfigHeaders(subscriptions.config, map[string]string{
"Content-Type": "application/json",
})
resp, err := client.Post(subscriptions.endpoint, bytes.NewBuffer(body), conf)
if err != nil {
return nil, err
}
data := NormalizeODataItem(resp)
var sub *SubscriptionInfo
if err := json.Unmarshal(data, &sub); err != nil {
return nil, err
}
return sub, nil
}
// Get gets subscription info
func (subscription *Subscription) Get() (*SubscriptionInfo, error) {
client := NewHTTPClient(subscription.client)
resp, err := client.Get(subscription.endpoint, subscription.config)
if err != nil {
return nil, err
}
return subscription.parseResponse(resp)
}
// Delete deletes a subscription by its ID (GUID)
func (subscription *Subscription) Delete() error {
client := NewHTTPClient(subscription.client)
_, err := client.Delete(subscription.endpoint, subscription.config)
return err
}
// Update updates a subscription
func (subscription *Subscription) Update(metadata map[string]interface{}) (*SubscriptionInfo, error) {
client := NewHTTPClient(subscription.client)
body, err := json.Marshal(metadata)
if err != nil {
return nil, err
}
conf := patchConfigHeaders(subscription.config, map[string]string{
"Content-Type": "application/json",
})
if _, err := client.Update(subscription.endpoint, bytes.NewBuffer(body), conf); err != nil {
return nil, err
}
return subscription.Get()
}
// SetExpiration sets new expiration datetime
func (subscription *Subscription) SetExpiration(expiration time.Time) (*SubscriptionInfo, error) {
return subscription.Update(map[string]interface{}{
"expirationDateTime": expiration,
})
}
// SetNotificationURL sets new notification URL state
func (subscription *Subscription) SetNotificationURL(notificationURL string) (*SubscriptionInfo, error) {
return subscription.Update(map[string]interface{}{
"notificationUrl": notificationURL,
})
}
// SetClientState sets new client state
func (subscription *Subscription) SetClientState(clientState string) (*SubscriptionInfo, error) {
return subscription.Update(map[string]interface{}{
"clientState": clientState,
})
}
// parseResponse normalize and unmarshal subscription info response
func (subscription *Subscription) parseResponse(bytes []byte) (*SubscriptionInfo, error) {
data := NormalizeODataItem(bytes)
var sub *SubscriptionInfo
if err := json.Unmarshal(data, &sub); err != nil {
return nil, err
}
return sub, nil
}