|
1 | 1 | /**
|
2 |
| - * Copyright 2017, Google, Inc. |
3 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
| - * you may not use this file except in compliance with the License. |
5 |
| - * You may obtain a copy of the License at |
6 |
| - * |
7 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
8 |
| - * |
9 |
| - * Unless required by applicable law or agreed to in writing, software |
10 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
11 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
| - * See the License for the specific language governing permissions and |
13 |
| - * limitations under the License. |
14 |
| - */ |
| 2 | +* Copyright 2018, Google, Inc. |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
15 | 15 |
|
16 | 16 | 'use strict';
|
17 | 17 |
|
18 |
| -const {google} = require('googleapis'); |
19 |
| -const cloudtasks = google.cloudtasks('v2beta2'); |
20 |
| - |
21 | 18 | /**
|
22 |
| - * Create a task for a given queue with an arbitrary payload. |
23 |
| - */ |
| 19 | +* Create a task for a given queue with an arbitrary payload. |
| 20 | +*/ |
24 | 21 | function createTask (project, location, queue, options) {
|
25 | 22 | // [START cloud_tasks_appengine_create_task]
|
26 |
| - google.auth.getClient({ |
27 |
| - scopes: ['https://www.googleapis.com/auth/cloud-platform'] |
28 |
| - }).then(authClient => { |
29 |
| - const task = { |
30 |
| - app_engine_http_request: { |
31 |
| - http_method: 'POST', |
32 |
| - relative_url: '/log_payload' |
33 |
| - } |
34 |
| - }; |
| 23 | + // Imports the Google Cloud Tasks library. |
| 24 | + const cloudTasks = require('@google-cloud/tasks'); |
35 | 25 |
|
36 |
| - if (options.payload !== undefined) { |
37 |
| - task.app_engine_http_request.payload = Buffer.from(options.payload).toString('base64'); |
38 |
| - } |
| 26 | + // Instantiates a client. |
| 27 | + const client = new cloudTasks.CloudTasksClient(); |
39 | 28 |
|
40 |
| - if (options.inSeconds !== undefined) { |
41 |
| - task.schedule_time = (new Date(options.inSeconds * 1000 + Date.now())).toISOString(); |
| 29 | + // Construct the fully qualified queue name. |
| 30 | + const parent = client.queuePath(project, location, queue); |
| 31 | + |
| 32 | + const task = { |
| 33 | + appEngineHttpRequest: { |
| 34 | + httpMethod: 'POST', |
| 35 | + relativeUrl: '/log_payload' |
42 | 36 | }
|
| 37 | + }; |
| 38 | + |
| 39 | + if (options.payload !== undefined) { |
| 40 | + task.appEngineHttpRequest.payload = Buffer.from(options.payload).toString('base64'); |
| 41 | + } |
43 | 42 |
|
44 |
| - const request = { |
45 |
| - parent: `projects/${project}/locations/${location}/queues/${queue}`, // TODO: Update placeholder value. |
46 |
| - resource: { |
47 |
| - task: task |
48 |
| - }, |
49 |
| - auth: authClient |
| 43 | + if (options.inSeconds !== undefined) { |
| 44 | + task.scheduleTime = { |
| 45 | + seconds: (options.inSeconds + Date.now() / 1000) |
50 | 46 | };
|
| 47 | + } |
| 48 | + |
| 49 | + const request = { |
| 50 | + parent: parent, |
| 51 | + task: task |
| 52 | + }; |
51 | 53 |
|
52 |
| - console.log('Sending task %j', task); |
53 |
| - return cloudtasks.projects.locations.queues.tasks.create(request); |
54 |
| - }).then(response => { |
55 |
| - console.log('Created task.', response.name); |
56 |
| - console.log(JSON.stringify(response, null, 2)); |
57 |
| - }).catch(console.error); |
| 54 | + console.log('Sending task %j', task); |
| 55 | + // Send create task request. |
| 56 | + client.createTask(request).then(response => { |
| 57 | + const task = response[0].name; |
| 58 | + console.log(`Created task ${task}`); |
| 59 | + }).catch(err => { |
| 60 | + console.error(`Error in createTask: ${err.message || err}`); |
| 61 | + }); |
58 | 62 | // [END cloud_tasks_appengine_create_task]
|
59 | 63 | }
|
60 | 64 |
|
|
0 commit comments