|
| 1 | +/** |
| 2 | + * Copyright Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// [START events_create_subscription] |
| 18 | +/** |
| 19 | + * Creates a subscription to receive events about a Google Workspace resource. |
| 20 | + * For a list of supported resources and event types, see the |
| 21 | + * [Google Workspace Events API Overview](https://developers.google.com/workspace/events#supported-events). |
| 22 | + * For additional information, see the |
| 23 | + * [subscriptions.create](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/create) |
| 24 | + * method reference. |
| 25 | + * @param {!string} targetResource The full resource name of the Google Workspace resource to subscribe to. |
| 26 | + * @param {!string|!Array<string>} eventTypes The types of events to receive about the resource. |
| 27 | + * @param {!string} pubsubTopic The resource name of the Pub/Sub topic that receives events from the subscription. |
| 28 | + */ |
| 29 | +function createSubscription(targetResource, eventTypes, pubsubTopic) { |
| 30 | + try { |
| 31 | + const operation = WorkspaceEvents.Subscriptions.create({ |
| 32 | + targetResource: targetResource, |
| 33 | + eventTypes: eventTypes, |
| 34 | + notificationEndpoint: { |
| 35 | + pubsubTopic: pubsubTopic, |
| 36 | + }, |
| 37 | + }); |
| 38 | + console.log(operation); |
| 39 | + } catch (err) { |
| 40 | + // TODO (developer) - Handle exception |
| 41 | + console.log('Failed to create subscription with error %s', err.message); |
| 42 | + } |
| 43 | +} |
| 44 | +// [END events_create_subscription] |
| 45 | + |
| 46 | +// [START events_list_subscriptions] |
| 47 | +/** |
| 48 | + * Lists subscriptions created by the calling app filtered by one or more event types and optionally by a target resource. |
| 49 | + * For additional information, see the |
| 50 | + * [subscriptions.list](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/list) |
| 51 | + * method reference. |
| 52 | + * @param {!string} filter The query filter. |
| 53 | + */ |
| 54 | +function listSubscriptions(filter) { |
| 55 | + try { |
| 56 | + const response = WorkspaceEvents.Subscriptions.list({ filter }); |
| 57 | + console.log(response); |
| 58 | + } catch (err) { |
| 59 | + // TODO (developer) - Handle exception |
| 60 | + console.log('Failed to list subscriptions with error %s', err.message); |
| 61 | + } |
| 62 | +} |
| 63 | +// [END events_list_subscriptions] |
| 64 | + |
| 65 | +// [START events_get_subscription] |
| 66 | +/** |
| 67 | + * Gets details about a subscription. |
| 68 | + * For additional information, see the |
| 69 | + * [subscriptions.get](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/get) |
| 70 | + * method reference. |
| 71 | + * @param {!string} name The resource name of the subscription. |
| 72 | + */ |
| 73 | +function getSubscription(name) { |
| 74 | + try { |
| 75 | + const subscription = WorkspaceEvents.Subscriptions.get(name); |
| 76 | + console.log(subscription); |
| 77 | + } catch (err) { |
| 78 | + // TODO (developer) - Handle exception |
| 79 | + console.log('Failed to get subscription with error %s', err.message); |
| 80 | + } |
| 81 | +} |
| 82 | +// [END events_get_subscription] |
| 83 | + |
| 84 | +// [START events_patch_subscription] |
| 85 | +/** |
| 86 | + * Updates an existing subscription. |
| 87 | + * This can be used to renew a subscription that is about to expire. |
| 88 | + * For additional information, see the |
| 89 | + * [subscriptions.patch](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/patch) |
| 90 | + * method reference. |
| 91 | + * @param {!string} name The resource name of the subscription. |
| 92 | + */ |
| 93 | +function patchSubscription(name) { |
| 94 | + try { |
| 95 | + const operation = WorkspaceEvents.Subscriptions.patch({ |
| 96 | + // Setting the TTL to 0 seconds extends the subscription to its maximum expiration time. |
| 97 | + ttl: '0s', |
| 98 | + }, name); |
| 99 | + console.log(operation); |
| 100 | + } catch (err) { |
| 101 | + // TODO (developer) - Handle exception |
| 102 | + console.log('Failed to update subscription with error %s', err.message); |
| 103 | + } |
| 104 | +} |
| 105 | +// [END events_patch_subscription] |
| 106 | + |
| 107 | +// [START events_reactivate_subscription] |
| 108 | +/** |
| 109 | + * Reactivates a suspended subscription. |
| 110 | + * Before reactivating, you must resolve any errors with the subscription. |
| 111 | + * For additional information, see the |
| 112 | + * [subscriptions.reactivate](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/reactivate) |
| 113 | + * method reference. |
| 114 | + * @param {!string} name The resource name of the subscription. |
| 115 | + */ |
| 116 | +function reactivateSubscription(name) { |
| 117 | + try { |
| 118 | + const operation = WorkspaceEvents.Subscriptions.reactivate({}, name); |
| 119 | + console.log(operation); |
| 120 | + } catch (err) { |
| 121 | + // TODO (developer) - Handle exception |
| 122 | + console.log('Failed to reactivate subscription with error %s', err.message); |
| 123 | + } |
| 124 | +} |
| 125 | +// [END events_reactivate_subscription] |
| 126 | + |
| 127 | +// [START events_delete_subscription] |
| 128 | +/** |
| 129 | + * Deletes a subscription. |
| 130 | + * For additional information, see the |
| 131 | + * [subscriptions.delete](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/delete) |
| 132 | + * method reference. |
| 133 | + * @param {!string} name The resource name of the subscription. |
| 134 | + */ |
| 135 | +function deleteSubscription(name) { |
| 136 | + try { |
| 137 | + const operation = WorkspaceEvents.Subscriptions.remove(name); |
| 138 | + console.log(operation); |
| 139 | + } catch (err) { |
| 140 | + // TODO (developer) - Handle exception |
| 141 | + console.log('Failed to delete subscription with error %s', err.message); |
| 142 | + } |
| 143 | +} |
| 144 | +// [END events_delete_subscription] |
| 145 | + |
| 146 | +// [START events_get_operation] |
| 147 | +/** |
| 148 | + * Gets details about an operation returned by one of the methods on the subscription |
| 149 | + * resource of the Google Workspace Events API. |
| 150 | + * For additional information, see the |
| 151 | + * [operations.get](https://developers.google.com/workspace/events/reference/rest/v1/operations/get) |
| 152 | + * method reference. |
| 153 | + * @param {!string} name The resource name of the operation. |
| 154 | + */ |
| 155 | +function getOperation(name) { |
| 156 | + try { |
| 157 | + const operation = WorkspaceEvents.Operations.get(name); |
| 158 | + console.log(operation); |
| 159 | + } catch (err) { |
| 160 | + // TODO (developer) - Handle exception |
| 161 | + console.log('Failed to get operation with error %s', err.message); |
| 162 | + } |
| 163 | +} |
| 164 | +// [END events_get_operation] |
0 commit comments