-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathevents.gs
164 lines (157 loc) · 5.95 KB
/
events.gs
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
/**
* Copyright Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START events_create_subscription]
/**
* Creates a subscription to receive events about a Google Workspace resource.
* For a list of supported resources and event types, see the
* [Google Workspace Events API Overview](https://developers.google.com/workspace/events#supported-events).
* For additional information, see the
* [subscriptions.create](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/create)
* method reference.
* @param {!string} targetResource The full resource name of the Google Workspace resource to subscribe to.
* @param {!string|!Array<string>} eventTypes The types of events to receive about the resource.
* @param {!string} pubsubTopic The resource name of the Pub/Sub topic that receives events from the subscription.
*/
function createSubscription(targetResource, eventTypes, pubsubTopic) {
try {
const operation = WorkspaceEvents.Subscriptions.create({
targetResource: targetResource,
eventTypes: eventTypes,
notificationEndpoint: {
pubsubTopic: pubsubTopic,
},
});
console.log(operation);
} catch (err) {
// TODO (developer) - Handle exception
console.log('Failed to create subscription with error %s', err.message);
}
}
// [END events_create_subscription]
// [START events_list_subscriptions]
/**
* Lists subscriptions created by the calling app filtered by one or more event types and optionally by a target resource.
* For additional information, see the
* [subscriptions.list](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/list)
* method reference.
* @param {!string} filter The query filter.
*/
function listSubscriptions(filter) {
try {
const response = WorkspaceEvents.Subscriptions.list({ filter });
console.log(response);
} catch (err) {
// TODO (developer) - Handle exception
console.log('Failed to list subscriptions with error %s', err.message);
}
}
// [END events_list_subscriptions]
// [START events_get_subscription]
/**
* Gets details about a subscription.
* For additional information, see the
* [subscriptions.get](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/get)
* method reference.
* @param {!string} name The resource name of the subscription.
*/
function getSubscription(name) {
try {
const subscription = WorkspaceEvents.Subscriptions.get(name);
console.log(subscription);
} catch (err) {
// TODO (developer) - Handle exception
console.log('Failed to get subscription with error %s', err.message);
}
}
// [END events_get_subscription]
// [START events_patch_subscription]
/**
* Updates an existing subscription.
* This can be used to renew a subscription that is about to expire.
* For additional information, see the
* [subscriptions.patch](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/patch)
* method reference.
* @param {!string} name The resource name of the subscription.
*/
function patchSubscription(name) {
try {
const operation = WorkspaceEvents.Subscriptions.patch({
// Setting the TTL to 0 seconds extends the subscription to its maximum expiration time.
ttl: '0s',
}, name);
console.log(operation);
} catch (err) {
// TODO (developer) - Handle exception
console.log('Failed to update subscription with error %s', err.message);
}
}
// [END events_patch_subscription]
// [START events_reactivate_subscription]
/**
* Reactivates a suspended subscription.
* Before reactivating, you must resolve any errors with the subscription.
* For additional information, see the
* [subscriptions.reactivate](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/reactivate)
* method reference.
* @param {!string} name The resource name of the subscription.
*/
function reactivateSubscription(name) {
try {
const operation = WorkspaceEvents.Subscriptions.reactivate({}, name);
console.log(operation);
} catch (err) {
// TODO (developer) - Handle exception
console.log('Failed to reactivate subscription with error %s', err.message);
}
}
// [END events_reactivate_subscription]
// [START events_delete_subscription]
/**
* Deletes a subscription.
* For additional information, see the
* [subscriptions.delete](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/delete)
* method reference.
* @param {!string} name The resource name of the subscription.
*/
function deleteSubscription(name) {
try {
const operation = WorkspaceEvents.Subscriptions.remove(name);
console.log(operation);
} catch (err) {
// TODO (developer) - Handle exception
console.log('Failed to delete subscription with error %s', err.message);
}
}
// [END events_delete_subscription]
// [START events_get_operation]
/**
* Gets details about an operation returned by one of the methods on the subscription
* resource of the Google Workspace Events API.
* For additional information, see the
* [operations.get](https://developers.google.com/workspace/events/reference/rest/v1/operations/get)
* method reference.
* @param {!string} name The resource name of the operation.
*/
function getOperation(name) {
try {
const operation = WorkspaceEvents.Operations.get(name);
console.log(operation);
} catch (err) {
// TODO (developer) - Handle exception
console.log('Failed to get operation with error %s', err.message);
}
}
// [END events_get_operation]