Skip to content

Commit

Permalink
docs(samples): update samples to latest standards (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and grayside committed Nov 3, 2022
1 parent 6e47428 commit b148833
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 184 deletions.
71 changes: 39 additions & 32 deletions cloud-tasks/createHttpTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

'use strict';
/*eslint no-warning-comments: [0, { "terms": ["todo", "fixme"], "location": "anywhere" }]*/

// sample-metadata:
// title: Cloud Tasks Create HTTP Target
Expand All @@ -23,7 +22,7 @@
/**
* Create a task with an HTTP target for a given queue with an arbitrary payload.
*/
async function createHttpTask(
function main(
project = 'my-project-id', // Your GCP Project id
queue = 'my-appengine-queue', // Name of your Queue
location = 'us-central1', // The GCP region of your queue
Expand All @@ -38,41 +37,49 @@ async function createHttpTask(
// Instantiates a client.
const client = new CloudTasksClient();

// TODO(developer): Uncomment these lines and replace with your values.
// const project = 'my-project-id';
// const queue = 'my-queue';
// const location = 'us-central1';
// const url = 'https://example.com/taskhandler';
// const payload = 'Hello, World!';
async function createHttpTask() {
// TODO(developer): Uncomment these lines and replace with your values.
// const project = 'my-project-id';
// const queue = 'my-queue';
// const location = 'us-central1';
// const url = 'https://example.com/taskhandler';
// const payload = 'Hello, World!';

// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);
// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);

const task = {
httpRequest: {
httpMethod: 'POST',
url,
},
};
const task = {
httpRequest: {
httpMethod: 'POST',
url,
},
};

if (payload) {
task.httpRequest.body = Buffer.from(payload).toString('base64');
}
if (payload) {
task.httpRequest.body = Buffer.from(payload).toString('base64');
}

if (inSeconds) {
// The time when the task is scheduled to be attempted.
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
}
if (inSeconds) {
// The time when the task is scheduled to be attempted.
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
}

// Send create task request.
console.log('Sending task:');
console.log(task);
const request = {parent, task};
const [response] = await client.createTask(request);
console.log(`Created task ${response.name}`);
// Send create task request.
console.log('Sending task:');
console.log(task);
const request = {parent, task};
const [response] = await client.createTask(request);
console.log(`Created task ${response.name}`);
}
createHttpTask();
// [END cloud_tasks_create_http_task]
}

createHttpTask(...process.argv.slice(2)).catch(console.error);
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
80 changes: 43 additions & 37 deletions cloud-tasks/createHttpTaskWithToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

'use strict';
/*eslint no-warning-comments: [0, { "terms": ["todo", "fixme"], "location": "anywhere" }]*/

// sample-metadata:
// title: Cloud Tasks Create HTTP Target with Token
Expand All @@ -23,7 +22,7 @@
/**
* Create a task with an HTTP target for a given queue with an arbitrary payload.
*/
async function createHttpTaskWithToken(
function main(
project = 'my-project-id', // Your GCP Project id
queue = 'my-queue', // Name of your Queue
location = 'us-central1', // The GCP region of your queue
Expand All @@ -39,47 +38,54 @@ async function createHttpTaskWithToken(
// Instantiates a client.
const client = new CloudTasksClient();

// TODO(developer): Uncomment these lines and replace with your values.
// const project = 'my-project-id';
// const queue = 'my-queue';
// const location = 'us-central1';
// const url = 'https://example.com/taskhandler';
// const serviceAccountEmail = 'client@<project-id>.iam.gserviceaccount.com';
// const payload = 'Hello, World!';
async function createHttpTaskWithToken() {
// TODO(developer): Uncomment these lines and replace with your values.
// const project = 'my-project-id';
// const queue = 'my-queue';
// const location = 'us-central1';
// const url = 'https://example.com/taskhandler';
// const serviceAccountEmail = 'client@<project-id>.iam.gserviceaccount.com';
// const payload = 'Hello, World!';

// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);
// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);

const task = {
httpRequest: {
httpMethod: 'POST',
url,
oidcToken: {
serviceAccountEmail,
const task = {
httpRequest: {
httpMethod: 'POST',
url,
oidcToken: {
serviceAccountEmail,
},
},
},
};

if (payload) {
task.httpRequest.body = Buffer.from(payload).toString('base64');
}

if (inSeconds) {
// The time when the task is scheduled to be attempted.
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
}

console.log('Sending task:');
console.log(task);
// Send create task request.
const request = {parent, task};
const [response] = await client.createTask(request);
const name = response.name;
console.log(`Created task ${name}`);
if (payload) {
task.httpRequest.body = Buffer.from(payload).toString('base64');
}

if (inSeconds) {
// The time when the task is scheduled to be attempted.
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
}

console.log('Sending task:');
console.log(task);
// Send create task request.
const request = {parent, task};
const [response] = await client.createTask(request);
const name = response.name;
console.log(`Created task ${name}`);
}
createHttpTaskWithToken();
// [END cloud_tasks_create_http_task_with_token]
}

createHttpTaskWithToken(...process.argv.slice(2)).catch(console.error);
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
45 changes: 26 additions & 19 deletions cloud-tasks/createQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,46 @@

'use strict';

// [START cloud_tasks_create_queue]
/**
* Create a new Task Queue
*/
async function createQueue(
function main(
project = 'my-project-id', // Your GCP Project id
queue = 'my-appengine-queue', // Name of the Queue to create
location = 'us-central1' // The GCP region in which to create the queue
) {
// [START cloud_tasks_create_queue]
// Imports the Google Cloud Tasks library.
const cloudTasks = require('@google-cloud/tasks');

// Instantiates a client.
const client = new cloudTasks.CloudTasksClient();

// Send create queue request.
const [response] = await client.createQueue({
// The fully qualified path to the location where the queue is created
parent: client.locationPath(project, location),
queue: {
// The fully qualified path to the queue
name: client.queuePath(project, location, queue),
appEngineHttpQueue: {
appEngineRoutingOverride: {
// The App Engine service that will receive the tasks.
service: 'default',
async function createQueue() {
// Send create queue request.
const [response] = await client.createQueue({
// The fully qualified path to the location where the queue is created
parent: client.locationPath(project, location),
queue: {
// The fully qualified path to the queue
name: client.queuePath(project, location, queue),
appEngineHttpQueue: {
appEngineRoutingOverride: {
// The App Engine service that will receive the tasks.
service: 'default',
},
},
},
},
});
console.log(`Created queue ${response.name}`);
});
console.log(`Created queue ${response.name}`);
}
createQueue();
// [END cloud_tasks_create_queue]
}
// [END cloud_tasks_create_queue]

const args = process.argv.slice(2);
createQueue(...args).catch(console.error);
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
72 changes: 39 additions & 33 deletions cloud-tasks/createTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

'use strict';
/*eslint no-warning-comments: [0, { "terms": ["todo", "fixme"], "location": "anywhere" }]*/

// sample-metadata:
// title: Cloud Tasks Create App Engine Target
Expand All @@ -23,7 +22,7 @@
/**
* Create a task for a given queue with an arbitrary payload.
*/
async function createTask(
function main(
project = 'my-project-id', // Your GCP Project id
queue = 'my-appengine-queue', // Name of your Queue
location = 'us-central1', // The GCP region of your queue
Expand All @@ -38,43 +37,50 @@ async function createTask(
// Instantiates a client.
const client = new CloudTasksClient();

// TODO(developer): Uncomment these lines and replace with your values.
// const project = 'my-project-id';
// const queue = 'my-appengine-queue';
// const location = 'us-central1';
// const payload = 'Hello, World!';
async function createTask() {
// TODO(developer): Uncomment these lines and replace with your values.
// const project = 'my-project-id';
// const queue = 'my-appengine-queue';
// const location = 'us-central1';
// const payload = 'Hello, World!';

// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);
// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);

const task = {
appEngineHttpRequest: {
httpMethod: 'POST',
relativeUri: '/log_payload',
},
};

if (payload) {
task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64');
}

if (inSeconds) {
// The time when the task is scheduled to be attempted.
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
const task = {
appEngineHttpRequest: {
httpMethod: 'POST',
relativeUri: '/log_payload',
},
};
}

console.log('Sending task:');
console.log(task);
// Send create task request.
const request = {parent, task};
const [response] = await client.createTask(request);
const name = response.name;
console.log(`Created task ${name}`);
if (payload) {
task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64');
}

if (inSeconds) {
// The time when the task is scheduled to be attempted.
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
}

console.log('Sending task:');
console.log(task);
// Send create task request.
const request = {parent, task};
const [response] = await client.createTask(request);
const name = response.name;
console.log(`Created task ${name}`);
}
createTask();
// [END cloud_tasks_appengine_create_task]
// [END tasks_quickstart]
}

createTask(...process.argv.slice(2)).catch(console.error);
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
Loading

0 comments on commit b148833

Please sign in to comment.