1 | ;
|
2 | // Copyright 2019 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 | // http://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 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | exports.Channel = void 0;
|
17 | const index_js_1 = require("./nodejs-common/index.js");
|
18 | const promisify_1 = require("@google-cloud/promisify");
|
19 | /**
|
20 | * Create a channel object to interact with a Cloud Storage channel.
|
21 | *
|
22 | * See {@link https://cloud.google.com/storage/docs/object-change-notification| Object Change Notification}
|
23 | *
|
24 | * @class
|
25 | *
|
26 | * @param {string} id The ID of the channel.
|
27 | * @param {string} resourceId The resource ID of the channel.
|
28 | *
|
29 | * @example
|
30 | * ```
|
31 | * const {Storage} = require('@google-cloud/storage');
|
32 | * const storage = new Storage();
|
33 | * const channel = storage.channel('id', 'resource-id');
|
34 | * ```
|
35 | */
|
36 | class Channel extends index_js_1.ServiceObject {
|
37 | constructor(storage, id, resourceId) {
|
38 | const config = {
|
39 | parent: storage,
|
40 | baseUrl: '/channels',
|
41 | // An ID shouldn't be included in the API requests.
|
42 | // RE:
|
43 | // https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1145
|
44 | id: '',
|
45 | methods: {
|
46 | // Only need `request`.
|
47 | },
|
48 | };
|
49 | super(config);
|
50 | this.metadata.id = id;
|
51 | this.metadata.resourceId = resourceId;
|
52 | }
|
53 | /**
|
54 | * @typedef {array} StopResponse
|
55 | * @property {object} 0 The full API response.
|
56 | */
|
57 | /**
|
58 | * @callback StopCallback
|
59 | * @param {?Error} err Request error, if any.
|
60 | * @param {object} apiResponse The full API response.
|
61 | */
|
62 | /**
|
63 | * Stop this channel.
|
64 | *
|
65 | * @param {StopCallback} [callback] Callback function.
|
66 | * @returns {Promise<StopResponse>}
|
67 | *
|
68 | * @example
|
69 | * ```
|
70 | * const {Storage} = require('@google-cloud/storage');
|
71 | * const storage = new Storage();
|
72 | * const channel = storage.channel('id', 'resource-id');
|
73 | * channel.stop(function(err, apiResponse) {
|
74 | * if (!err) {
|
75 | * // Channel stopped successfully.
|
76 | * }
|
77 | * });
|
78 | *
|
79 | * //-
|
80 | * // If the callback is omitted, we'll return a Promise.
|
81 | * //-
|
82 | * channel.stop().then(function(data) {
|
83 | * const apiResponse = data[0];
|
84 | * });
|
85 | * ```
|
86 | */
|
87 | stop(callback) {
|
88 | callback = callback || index_js_1.util.noop;
|
89 | this.request({
|
90 | method: 'POST',
|
91 | uri: '/stop',
|
92 | json: this.metadata,
|
93 | }, (err, apiResponse) => {
|
94 | callback(err, apiResponse);
|
95 | });
|
96 | }
|
97 | }
|
98 | exports.Channel = Channel;
|
99 | /*! Developer Documentation
|
100 | *
|
101 | * All async methods (except for streams) will return a Promise in the event
|
102 | * that a callback is omitted.
|
103 | */
|
104 | (0, promisify_1.promisifyAll)(Channel);
|