-
Notifications
You must be signed in to change notification settings - Fork 2k
NodeJs samples for Cloud Job discovery api - part1 #685
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
Closed
Closed
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5d3df7c
First CJD sample
xinyunh0929 52caacb
Add NodeJs samples for Company API
1dc59be
Add code samples for Company api
43b5fd4
Add code samples for Job api
d665693
Added search sample and annotations for documentation
dd44dfa
Fix lint errors
e33019f
Fix search sample and search test
7b4e5da
Merge branch 'master' into master
555f761
Moved tests from mocha to ava
e5f2aa7
Merge branch 'master' of https://github.com/rshelar/nodejs-docs-samples
f825712
Merge branch 'master' of https://github.com/rshelar/nodejs-docs-samples
a5ac75f
Fix Copyright label and use uuid to generate random names
03a542d
Merge branch 'master' of https://github.com/rshelar/nodejs-docs-samples
67aec6c
Update quickstart.js
xinyunh0929 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| /** | ||
| * 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(), | ||
| 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; | ||
| }); | ||
| } | ||
|
|
||
| if (require.main === module) { | ||
| main(); | ||
| } | ||
| // [END basicCompanySample] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| /** | ||
| * Copyright 2017, Google, Inc. | ||
|
Contributor
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(), | ||
| 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] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.