-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NodeJs samples for Cloud Job discovery api - part1 #685
Changes from 5 commits
5d3df7c
52caacb
1dc59be
43b5fd4
d665693
dd44dfa
e33019f
7b4e5da
555f761
e5f2aa7
f825712
a5ac75f
03a542d
67aec6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* Copyright 2017, Google, Inc. | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
'use strict'; | ||
|
||
// [START basicCompanySample] | ||
|
||
const assert = require('assert'); | ||
const getClient = require('./jobsClient.js').getClient; | ||
|
||
// [START basic_company] | ||
/** | ||
* Generate data for a company. | ||
* @returns {Object} Object containing fields of 'Company' resource. | ||
*/ | ||
function generateCompany() { | ||
return { | ||
displayName: 'Google', | ||
distributorCompanyId: 'company:' + Math.floor(Math.random() * 100000).toString(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While it is . very unlikely, it is possible that we generate two identical ID's here. Is that okay? |
||
hqLocation: '1600 Amphitheatre Parkway Mountain View, CA 94043' | ||
}; | ||
} | ||
exports.generateCompany = generateCompany; | ||
// [END basic_company] | ||
|
||
// [START create_company] | ||
/** | ||
* Create a new company. | ||
* @param {Object} client Instance of google.jobs module. | ||
* @param {Object} companyInfo Object containing fields of 'Company' resource. | ||
* @returns {Promise.Object} Promise containing 'data' field of response. | ||
*/ | ||
function createCompany(client, companyInfo) { | ||
assert(companyInfo, '\'companyInfo\' argument is required.'); | ||
// Check required fields. | ||
assert(companyInfo.displayName, '\'displayName\' field is required.'); | ||
assert(companyInfo.distributorCompanyId, '\'distributorCompanyId\' field is required.'); | ||
|
||
return new Promise((resolve, reject) => { | ||
client.companies.create({ resource: companyInfo }, {}, null).then((response) => { | ||
assert(response.status === 200, 'Received response code: ' + response.status); | ||
assert(response.statusText === 'OK', 'Received status: ' + response.statusText); | ||
assert(response.data, '\'data\' field not populated in response.'); | ||
resolve(response.data); | ||
}); | ||
}); | ||
} | ||
// [END create_company] | ||
exports.createCompany = createCompany; | ||
|
||
// [START get_company] | ||
/** | ||
* Get a company. | ||
* @param {Object} client Instance of google.jobs module. | ||
* @param {string} companyName Name of company (value of 'name' field). | ||
* @returns {Promise.Object} Promise containing 'data' field of response. | ||
*/ | ||
function getCompany(client, companyName) { | ||
assert(companyName, 'companyName argument is required.'); | ||
return new Promise((resolve, reject) => { | ||
client.companies.get({ name: companyName }, {}, null).then((response) => { | ||
assert(response.status === 200, 'Received response code: ' + response.status); | ||
assert(response.statusText === 'OK', 'Received status: ' + response.statusText); | ||
resolve(response.data); | ||
}); | ||
}); | ||
} | ||
// [END get_company] | ||
exports.getCompany = getCompany; | ||
|
||
// [START update_company] | ||
/** | ||
* Update a company. | ||
* @param {Object} client Instance of google.jobs module. | ||
* @param {string} companyName Name of company (value of 'name' field). | ||
* @param {*} companyInfo Object containing fields of 'Company' resource. | ||
*/ | ||
function updateCompany(client, companyName, companyInfo) { | ||
assert(companyName, '\'companyName\' argument is required.'); | ||
assert(companyInfo, '\'companyInfo\' argument is required.'); | ||
|
||
return new Promise((resolve, reject) => { | ||
client.companies.patch({ name: companyName, resource: companyInfo }, {}, null).then((response) => { | ||
assert(response.status === 200, 'Received response code: ' + response.status); | ||
assert(response.statusText === 'OK', 'Received status: ' + response.statusText); | ||
resolve(response.data); | ||
}); | ||
}); | ||
} | ||
// [END update_company] | ||
exports.updateCompany = updateCompany; | ||
|
||
// [START delete_company] | ||
/** | ||
* Delete a company. | ||
* @param {Object} client Instance of google.jobs module. | ||
* @param {string} companyName Name of company (value of 'name' field). | ||
*/ | ||
function deleteCompany(client, companyName) { | ||
assert(companyName, 'companyName argument is required.'); | ||
return new Promise((resolve, reject) => { | ||
client.companies.delete({ name: companyName }, {}, null).then((response) => { | ||
assert(response.status === 200, 'Received response code: ' + response.status); | ||
assert(response.statusText === 'OK', 'Received status: ' + response.statusText); | ||
resolve(response.data); | ||
}); | ||
}); | ||
} | ||
// [END delete_company] | ||
exports.deleteCompany = deleteCompany; | ||
|
||
/** | ||
* Main entry point function. | ||
*/ | ||
function main() { | ||
getClient().then((jobsClient) => { | ||
assert(jobsClient, 'jobs instance not found.'); | ||
|
||
let companyInfo = generateCompany(); | ||
// Create a company. | ||
createCompany(jobsClient, companyInfo).then((info) => { | ||
const companyName = info.name; | ||
console.log('Company name:', companyName); | ||
|
||
// Get company. | ||
getCompany(jobsClient, companyName).then((info) => { | ||
// Set 'website' field. | ||
companyInfo.website = 'https://www.foobar.com'; | ||
// Update company. | ||
updateCompany(jobsClient, companyName, companyInfo).then((info) => { | ||
assert(companyInfo.website === info.website, '\'website\' field did not get added.'); | ||
console.log(info); | ||
|
||
// Delete company. | ||
deleteCompany(jobsClient, companyName); | ||
}); | ||
}); | ||
}); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
throw err; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. throwing the exception again may not be needed here? |
||
}); | ||
} | ||
|
||
if (require.main === module) { | ||
main(); | ||
} | ||
// [END basicCompanySample] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/** | ||
* Copyright 2017, Google, Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2018 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 | ||
* | ||
* http://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. | ||
*/ | ||
'use strict'; | ||
// [START basicJobSample] | ||
|
||
const assert = require('assert'); | ||
const companySample = require('./basicCompanySample.js'); | ||
const getClient = require('./jobsClient.js').getClient; | ||
|
||
// [START basic_job] | ||
/** | ||
* Generate data for a job. | ||
* @param {string} companyName Name of company (value of 'name' field). | ||
* @returns {Object} Object containing fields of 'Job' resource. | ||
*/ | ||
function generateJob(companyName) { | ||
assert(companyName, '\'companyName\' argument is required.'); | ||
return { | ||
requisitionId: 'jobWithRequiredFields:' + Math.floor(Math.random() * 100000).toString(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. requisitionId is not guaranteed to be unique with this approach. |
||
jobTitle: 'System administrator', | ||
description: 'Maintain IT network.', | ||
companyName: companyName, | ||
applicationUrls: [ 'https://www.foobar.com' ] | ||
}; | ||
} | ||
exports.generateJob = generateJob; | ||
// [END basic_job] | ||
|
||
// [START create_job] | ||
/** | ||
* Create a new job. | ||
* @param {Object} client Instance of google.jobs module. | ||
* @param {Object} jobInfo Object containing fields of 'Job' resource. | ||
* @returns {Promise.Object} Promise containing 'data' field of response. | ||
*/ | ||
function createJob(client, jobInfo) { | ||
assert(jobInfo, '\'jobInfo\' argument is required.'); | ||
|
||
return new Promise((resolve, reject) => { | ||
client.jobs.create({ resource: { job: jobInfo } }, {}, null).then((response) => { | ||
assert(response.status === 200, 'Received response code: ' + response.status); | ||
assert(response.statusText === 'OK', 'Received status: ' + response.statusText); | ||
assert(response.data, '\'data\' field not populated in response.'); | ||
resolve(response.data); | ||
}); | ||
}); | ||
} | ||
exports.createJob = createJob; | ||
// [END create_job] | ||
|
||
// [START create_job] | ||
/** | ||
* Get a job. | ||
* @param {Object} client Instance of google.jobs module. | ||
* @param {string} jobName Job name. | ||
* @returns {Promise.Object} Promise containing 'data' field of response. | ||
*/ | ||
function getJob(client, jobName) { | ||
assert(jobName, '\'jobName\' argument is required.'); | ||
|
||
return new Promise((resolve, reject) => { | ||
client.jobs.get({ name: jobName }, {}, null).then((response) => { | ||
assert(response.status === 200, 'Received response code: ' + response.status); | ||
assert(response.statusText === 'OK', 'Received status: ' + response.statusText); | ||
resolve(response.data); | ||
}); | ||
}); | ||
} | ||
exports.getJob = getJob; | ||
// [END create_job] | ||
|
||
// [START update_job] | ||
/** | ||
* Update a job. | ||
* @param {Object} client Instance of google.jobs module. | ||
* @param {string} jobName Job name. | ||
* @param {Object} jobInfo Object containing fields of 'Job' resource. | ||
* @returns {Promise.Object} Promise containing 'data' field of response. | ||
*/ | ||
function updateJob(client, jobName, jobInfo) { | ||
assert(jobName, '\'jobName\' argument is required.'); | ||
assert(jobInfo, '\'jobInfo\' argument is required.'); | ||
|
||
return new Promise((resolve, reject) => { | ||
client.jobs.patch({ name: jobName, resource: { job: jobInfo } }, {}, null).then((response) => { | ||
assert(response.status === 200, 'Received response code: ' + response.status); | ||
assert(response.statusText === 'OK', 'Received status: ' + response.statusText); | ||
assert(response.data, '\'data\' field not populated in response.'); | ||
resolve(response.data); | ||
}); | ||
}); | ||
} | ||
exports.updateJob = updateJob; | ||
// [END update_job] | ||
|
||
// [START delete_job] | ||
/** | ||
* Delete a job. | ||
* @param {string} jobName Job name. | ||
* @returns {Promise.Object} Promise containing 'data' field of response. | ||
*/ | ||
function deleteJob(client, jobName) { | ||
assert(jobName, '\'jobName\' argument is required.'); | ||
return new Promise((resolve, reject) => { | ||
client.jobs.delete({ name: jobName }, {}, null).then((response) => { | ||
assert(response.status === 200, 'Received response code: ' + response.status); | ||
assert(response.statusText === 'OK', 'Received status: ' + response.statusText); | ||
resolve(response.data); | ||
}); | ||
}); | ||
} | ||
exports.deleteJob = deleteJob; | ||
// [END delete_job] | ||
|
||
/** | ||
* Main entry point function. | ||
*/ | ||
function main() { | ||
getClient().then((jobsClient) => { | ||
assert(jobsClient, 'jobs instance not found.'); | ||
|
||
companySample.createCompany(jobsClient, companySample.generateCompany()).then((companyInfo) => { | ||
const companyName = companyInfo.name; | ||
console.log('Company name:', companyName); | ||
|
||
let jobInfo = generateJob(companyName); | ||
// Create job. | ||
createJob(jobsClient, jobInfo).then((info) => { | ||
const jobName = info.name; | ||
console.log('Job name:', jobName); | ||
|
||
// Get job. | ||
getJob(jobsClient, jobName).then((jobInfo) => { | ||
jobInfo.department = 'IT'; | ||
jobInfo.description = 'Manage company network. Manage server infrastructure.'; | ||
|
||
// Update job. | ||
updateJob(jobsClient, jobName, jobInfo).then((info) => { | ||
assert(jobInfo.department === info.department); | ||
assert(jobInfo.description === info.description); | ||
|
||
// Delete job. | ||
deleteJob(jobsClient, jobName); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
if (require.main === module) { | ||
main(); | ||
} | ||
// [END basicJobSample] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.