From 6d23aaec6883d5720155257199c4a6364ae693b1 Mon Sep 17 00:00:00 2001 From: JCIV-SE Date: Wed, 2 Jan 2019 12:05:18 -0500 Subject: [PATCH 1/8] Jobs sample & test start --- samples/jobs/README.md | 10 +++++ samples/jobs/company.js | 64 +++++++++++++++++++++++++++++++ samples/jobs/jobs.js | 60 +++++++++++++++++++++++++++++ test/samples/test.samples.jobs.ts | 39 +++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 samples/jobs/README.md create mode 100644 samples/jobs/company.js create mode 100644 samples/jobs/jobs.js create mode 100644 test/samples/test.samples.jobs.ts diff --git a/samples/jobs/README.md b/samples/jobs/README.md new file mode 100644 index 00000000000..6a2a7bc770b --- /dev/null +++ b/samples/jobs/README.md @@ -0,0 +1,10 @@ +# Jobs v3 API Samples + +These samples allow you to perform CRUD company operations, + +## Running the samples + +### **Note: Node.js version 8 or greater is requared to run samples.** +``` +// Le code here +``` diff --git a/samples/jobs/company.js b/samples/jobs/company.js new file mode 100644 index 00000000000..039fdc3252e --- /dev/null +++ b/samples/jobs/company.js @@ -0,0 +1,64 @@ +// Copyright 2016, 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'; + +const {google} = require('googleapis'); +const sampleClient = require('../sampleclient'); +const API_KEY = '841a6581e6dc9f7e0daabcce7211073498a8f16d '; + +const talent = google.jobs({ + version: 'v3', + auth: sampleClient.oAuth2Client, +}); + +/** + * Create a company. + */ + +// const create = talent.projects.companies.create(request, (error, result) => { +// if (error) console.error(error); +// else console.log('company created', result); +// }); + +async function runSample() { + const res = await talent.projects.companies.create({ + parent: `projects/${PROJECT_ID}`, + requestBody: { + displayName: 'ABC co.', + externalId: '12345', + }, + }); + console.log(res.data); + return res.data; +} + +const scopes = ['https://www.googleapis.com/auth/jobs']; +sampleClient + .authenticate(scopes) + .then(runSample) + .catch(console.error); + +module.exports = { + runSample, + client: sampleClient.oAuth2Client, +}; + +/** + * Current Resources: + * 1. https://github.com/googleapis/google-api-nodejs-client/issues/1305 + * 2. https://github.com/googleapis/google-api-nodejs-client/issues/1296 + * 3. https://github.com/googleapis/google-api-nodejs-client#specifying-request-body + * 4. https://console.cloud.google.com/talent-solution/connect-service-accounts?project=eloquent-ratio-225420 + * 5. https://cloud.google.com/talent-solution/job-search/docs/reference/rest/v3/projects.companies/create + */ diff --git a/samples/jobs/jobs.js b/samples/jobs/jobs.js new file mode 100644 index 00000000000..ea70c8d6d25 --- /dev/null +++ b/samples/jobs/jobs.js @@ -0,0 +1,60 @@ +// Copyright 2016, 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'; + +const {google} = require('googleapis'); +const sampleClient = require('../sampleclient'); + +//! BELOW: AUTH NEEDS FINALIZING +// initialize the Youtube API library +const talent = google.jobs({ + version: 'v3', + auth: sampleClient.oAuth2Client, +}); +//! BELOW: NOT MODIFIED +// a very simple example of getting data from a playlist +async function runSample() { + // the first query will return data with an etag + const res = await getPlaylistData(null); + const etag = res.data.etag; + console.log(`etag: ${etag}`); + + // the second query will (likely) return no data, and an HTTP 304 + // since the If-None-Match header was set with a matching eTag + const res2 = await getPlaylistData(etag); + console.log(res2.status); +} + +async function getPlaylistData(etag) { + // Create custom HTTP headers for the request to enable use of eTags + const headers = {}; + if (etag) { + headers['If-None-Match'] = etag; + } + const res = await youtube.playlists.list({ + part: 'id,snippet', + id: 'PLIivdWyY5sqIij_cgINUHZDMnGjVx3rxi', + headers: headers, + }); + console.log('Status code: ' + res.status); + console.log(res.data); + return res; +} + +const scopes = ['https://www.googleapis.com/auth/youtube']; + +sampleClient + .authenticate(scopes) + .then(runSample) + .catch(console.error); diff --git a/test/samples/test.samples.jobs.ts b/test/samples/test.samples.jobs.ts new file mode 100644 index 00000000000..11382382add --- /dev/null +++ b/test/samples/test.samples.jobs.ts @@ -0,0 +1,39 @@ +// Copyright 2018, Google, 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. + +import * as assert from 'assert'; +import * as nock from 'nock'; +import {Utils} from './../utils'; + +nock.disableNetConnect(); + +// tslint:disable: no-any +const samples: any = { + companies: require('../../../samples/jobs/companies'), + jobs: require('../../../samples/jobs/companies') +}; + +describe('Talent API Samples', () => { + afterEach(() => { + nock.cleanAll(); + }); + + it('should do something pew pew', async () => { + const scope = + nock(Utils.baseUrl).get(`/customsearch/v1?cx=cx&q=q`).reply(200, {}); //Change + const options = {cx: 'cx', q: 'q', auth: 'key'}; + const data = await samples.list.runSample(options); + assert(data); + scope.done(); + }); +}); From 06179e0357bcb0c268182f3b395adaac68a1f3f3 Mon Sep 17 00:00:00 2001 From: JCIV-SE Date: Mon, 7 Jan 2019 13:49:26 -0500 Subject: [PATCH 2/8] Talent API Simple Sample --- README.md | 2 +- samples/jobs/README.md | 3 +- samples/jobs/company.js | 64 ------------------------------- samples/jobs/jobs.js | 59 +++++++++++++--------------- samples/sampleclient.js | 1 + src/apis/jobs/README.md | 4 ++ test/samples/test.samples.jobs.ts | 39 ------------------- 7 files changed, 34 insertions(+), 138 deletions(-) delete mode 100644 samples/jobs/company.js delete mode 100644 test/samples/test.samples.jobs.ts diff --git a/README.md b/README.md index 4427c5caa87..d561a811b67 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ The are three primary ways to authenticate to Google APIs. Some service support To learn more about the authentication client, see the [Google Auth Library](https://github.com/googleapis/google-auth-library-nodejs). ### OAuth2 client -This client comes with an [OAuth2][oauth] client that allows you to retrieve an access token and refreshes the token and retry the request seamlessly The basics of Google's OAuth2 implementation is explained on [Google Authorization and Authentication documentation][authdocs]. +This client comes with an [OAuth2][oauth] client that allows you to retrieve an access token and refreshes the token and retry the request seamlessly. The basics of Google's OAuth2 implementation is explained on [Google Authorization and Authentication documentation][authdocs]. In the following examples, you may need a `CLIENT_ID`, `CLIENT_SECRET` and `REDIRECT_URL`. You can find these pieces of information by going to the [Developer Console][devconsole], clicking your project --> APIs & auth --> credentials. diff --git a/samples/jobs/README.md b/samples/jobs/README.md index 6a2a7bc770b..bf5e800918c 100644 --- a/samples/jobs/README.md +++ b/samples/jobs/README.md @@ -4,7 +4,8 @@ These samples allow you to perform CRUD company operations, ## Running the samples -### **Note: Node.js version 8 or greater is requared to run samples.** +### **Note: Node.js version 8 or greater is required to run samples.** + ``` // Le code here ``` diff --git a/samples/jobs/company.js b/samples/jobs/company.js deleted file mode 100644 index 039fdc3252e..00000000000 --- a/samples/jobs/company.js +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2016, 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'; - -const {google} = require('googleapis'); -const sampleClient = require('../sampleclient'); -const API_KEY = '841a6581e6dc9f7e0daabcce7211073498a8f16d '; - -const talent = google.jobs({ - version: 'v3', - auth: sampleClient.oAuth2Client, -}); - -/** - * Create a company. - */ - -// const create = talent.projects.companies.create(request, (error, result) => { -// if (error) console.error(error); -// else console.log('company created', result); -// }); - -async function runSample() { - const res = await talent.projects.companies.create({ - parent: `projects/${PROJECT_ID}`, - requestBody: { - displayName: 'ABC co.', - externalId: '12345', - }, - }); - console.log(res.data); - return res.data; -} - -const scopes = ['https://www.googleapis.com/auth/jobs']; -sampleClient - .authenticate(scopes) - .then(runSample) - .catch(console.error); - -module.exports = { - runSample, - client: sampleClient.oAuth2Client, -}; - -/** - * Current Resources: - * 1. https://github.com/googleapis/google-api-nodejs-client/issues/1305 - * 2. https://github.com/googleapis/google-api-nodejs-client/issues/1296 - * 3. https://github.com/googleapis/google-api-nodejs-client#specifying-request-body - * 4. https://console.cloud.google.com/talent-solution/connect-service-accounts?project=eloquent-ratio-225420 - * 5. https://cloud.google.com/talent-solution/job-search/docs/reference/rest/v3/projects.companies/create - */ diff --git a/samples/jobs/jobs.js b/samples/jobs/jobs.js index ea70c8d6d25..293afc0d986 100644 --- a/samples/jobs/jobs.js +++ b/samples/jobs/jobs.js @@ -16,45 +16,38 @@ const {google} = require('googleapis'); const sampleClient = require('../sampleclient'); -//! BELOW: AUTH NEEDS FINALIZING -// initialize the Youtube API library -const talent = google.jobs({ +const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT; +const jobService = google.jobs({ version: 'v3', auth: sampleClient.oAuth2Client, }); -//! BELOW: NOT MODIFIED -// a very simple example of getting data from a playlist -async function runSample() { - // the first query will return data with an etag - const res = await getPlaylistData(null); - const etag = res.data.etag; - console.log(`etag: ${etag}`); - - // the second query will (likely) return no data, and an HTTP 304 - // since the If-None-Match header was set with a matching eTag - const res2 = await getPlaylistData(etag); - console.log(res2.status); -} -async function getPlaylistData(etag) { - // Create custom HTTP headers for the request to enable use of eTags - const headers = {}; - if (etag) { - headers['If-None-Match'] = etag; - } - const res = await youtube.playlists.list({ - part: 'id,snippet', - id: 'PLIivdWyY5sqIij_cgINUHZDMnGjVx3rxi', - headers: headers, +async function runSample() { + const res = await jobService.projects.companies.create({ + parent: `project/${PROJECT_ID}`, + requestBody: { + company: { + displayName: 'ABC co.', + externalId: '12345', + }, + }, }); - console.log('Status code: ' + res.status); console.log(res.data); - return res; + return res.data; } -const scopes = ['https://www.googleapis.com/auth/youtube']; +if (module === require.main) { + const scopes = [ + 'https://www.googleapis.com/auth/jobs', + 'https://www.googleapis.com/auth/cloud-platform', + ]; + sampleClient + .authenticate(scopes) + .then(runSample) + .catch(console.error); +} -sampleClient - .authenticate(scopes) - .then(runSample) - .catch(console.error); +module.exports = { + runSample, + client: sampleClient.oAuth2Client, +}; diff --git a/samples/sampleclient.js b/samples/sampleclient.js index 86663260ba9..e0c1412890a 100644 --- a/samples/sampleclient.js +++ b/samples/sampleclient.js @@ -31,6 +31,7 @@ let keys = { }; if (fs.existsSync(keyPath)) { const keyFile = require(keyPath); + console.log('keyFile: ', keyFile); keys = keyFile.installed || keyFile.web; } diff --git a/src/apis/jobs/README.md b/src/apis/jobs/README.md index 9f537bcfd6b..3778c63bdfe 100644 --- a/src/apis/jobs/README.md +++ b/src/apis/jobs/README.md @@ -13,6 +13,10 @@ $ npm install @google/jobs ## Usage All documentation and usage information can be found on [GitHub](https://github.com/google/google-api-nodejs-client). +## v3 Samples + +You can find samples of v3 of the Talent Solution API [here](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/jobs/v3) + ## License This library is licensed under Apache 2.0. Full license text is available in [LICENSE](https://github.com/google/google-api-nodejs-client/blob/master/LICENSE). diff --git a/test/samples/test.samples.jobs.ts b/test/samples/test.samples.jobs.ts deleted file mode 100644 index 11382382add..00000000000 --- a/test/samples/test.samples.jobs.ts +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2018, Google, 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. - -import * as assert from 'assert'; -import * as nock from 'nock'; -import {Utils} from './../utils'; - -nock.disableNetConnect(); - -// tslint:disable: no-any -const samples: any = { - companies: require('../../../samples/jobs/companies'), - jobs: require('../../../samples/jobs/companies') -}; - -describe('Talent API Samples', () => { - afterEach(() => { - nock.cleanAll(); - }); - - it('should do something pew pew', async () => { - const scope = - nock(Utils.baseUrl).get(`/customsearch/v1?cx=cx&q=q`).reply(200, {}); //Change - const options = {cx: 'cx', q: 'q', auth: 'key'}; - const data = await samples.list.runSample(options); - assert(data); - scope.done(); - }); -}); From 5d5ba1bcb1070bf69dbe1d58beb1bc4b19c14e28 Mon Sep 17 00:00:00 2001 From: JCIV-SE Date: Mon, 7 Jan 2019 13:56:48 -0500 Subject: [PATCH 3/8] Cleaning logs --- samples/sampleclient.js | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/sampleclient.js b/samples/sampleclient.js index e0c1412890a..86663260ba9 100644 --- a/samples/sampleclient.js +++ b/samples/sampleclient.js @@ -31,7 +31,6 @@ let keys = { }; if (fs.existsSync(keyPath)) { const keyFile = require(keyPath); - console.log('keyFile: ', keyFile); keys = keyFile.installed || keyFile.web; } From a27530741b1ab11db652b59062416b23d3c9c4f2 Mon Sep 17 00:00:00 2001 From: Joe Cervino Date: Mon, 7 Jan 2019 14:19:06 -0500 Subject: [PATCH 4/8] Delete Sample README --- samples/jobs/README.md | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 samples/jobs/README.md diff --git a/samples/jobs/README.md b/samples/jobs/README.md deleted file mode 100644 index bf5e800918c..00000000000 --- a/samples/jobs/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Jobs v3 API Samples - -These samples allow you to perform CRUD company operations, - -## Running the samples - -### **Note: Node.js version 8 or greater is required to run samples.** - -``` -// Le code here -``` From 69d517e169e9170317d323de069bace46e843055 Mon Sep 17 00:00:00 2001 From: Joe Cervino Date: Mon, 7 Jan 2019 17:51:59 -0500 Subject: [PATCH 5/8] Changes to 'OAuth 2 client' section --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d561a811b67..d18b243b3dd 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ The are three primary ways to authenticate to Google APIs. Some service support To learn more about the authentication client, see the [Google Auth Library](https://github.com/googleapis/google-auth-library-nodejs). ### OAuth2 client -This client comes with an [OAuth2][oauth] client that allows you to retrieve an access token and refreshes the token and retry the request seamlessly. The basics of Google's OAuth2 implementation is explained on [Google Authorization and Authentication documentation][authdocs]. +This client comes with an [OAuth2][oauth] client that allows you to retrieve an access token, refresh it, and retry the request seamlessly. The basics of Google's OAuth2 implementation is explained on [Google Authorization and Authentication documentation][authdocs]. In the following examples, you may need a `CLIENT_ID`, `CLIENT_SECRET` and `REDIRECT_URL`. You can find these pieces of information by going to the [Developer Console][devconsole], clicking your project --> APIs & auth --> credentials. From f7cb56de4bd616e1cbb8552c975adc77a5998f9a Mon Sep 17 00:00:00 2001 From: JCIV-SE Date: Mon, 14 Jan 2019 14:14:15 -0500 Subject: [PATCH 6/8] Samples: Talent API Sample Revision & Test --- samples/jobs/jobs.js | 4 +-- test/samples/test.samples.jobs.ts | 44 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 test/samples/test.samples.jobs.ts diff --git a/samples/jobs/jobs.js b/samples/jobs/jobs.js index 293afc0d986..673d7e0c9e8 100644 --- a/samples/jobs/jobs.js +++ b/samples/jobs/jobs.js @@ -16,15 +16,15 @@ const {google} = require('googleapis'); const sampleClient = require('../sampleclient'); -const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT; const jobService = google.jobs({ version: 'v3', auth: sampleClient.oAuth2Client, }); async function runSample() { + const projectId = await google.auth.getProjectId(); const res = await jobService.projects.companies.create({ - parent: `project/${PROJECT_ID}`, + parent: `project/${projectId}`, requestBody: { company: { displayName: 'ABC co.', diff --git a/test/samples/test.samples.jobs.ts b/test/samples/test.samples.jobs.ts new file mode 100644 index 00000000000..3e0ae2ad14d --- /dev/null +++ b/test/samples/test.samples.jobs.ts @@ -0,0 +1,44 @@ +// Copyright 2018, Google, 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. + +import * as assert from 'assert'; +import * as nock from 'nock'; +import {Utils} from '../utils'; + +nock.disableNetConnect(); + +// tslint:disable: no-any +const samples: any = { + jobs: require('../../../samples/jobs/jobs.js'), +}; + +for (const p in samples) { + if (samples[p]) { + samples.jobs.client.credentials = {access_token: 'not-a-token'}; + } +} + +describe('Talent API Samples', () => { + afterEach(() => { + nock.cleanAll(); + }); + + it('should create a company', async () => { + const scope = nock(Utils.baseUrl) + .post(`/v3/jobs/project/project-id/companies`) + .reply(200, {}); + const data = await samples.jobs.runSample(); + assert(data); + scope.done(); + }); +}); From c4c4ba9cced9081de62219a958a3df9cde1be90f Mon Sep 17 00:00:00 2001 From: JCIV-SE Date: Fri, 25 Jan 2019 15:11:45 -0500 Subject: [PATCH 7/8] docs(samples): Talent API test .skip --- test/samples/test.samples.jobs.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/samples/test.samples.jobs.ts b/test/samples/test.samples.jobs.ts index 3e0ae2ad14d..395478af0ae 100644 --- a/test/samples/test.samples.jobs.ts +++ b/test/samples/test.samples.jobs.ts @@ -28,14 +28,14 @@ for (const p in samples) { } } -describe('Talent API Samples', () => { +describe.skip('Talent API Samples', () => { afterEach(() => { nock.cleanAll(); }); it('should create a company', async () => { const scope = nock(Utils.baseUrl) - .post(`/v3/jobs/project/project-id/companies`) + .post(`/v3/jobs/project/${process.env.GCLOUD_PROJECT}/companies`) .reply(200, {}); const data = await samples.jobs.runSample(); assert(data); From 4068727ce65a85561ecd175ccb7e9ff1c7059133 Mon Sep 17 00:00:00 2001 From: JCIV-SE Date: Tue, 29 Jan 2019 17:07:23 -0500 Subject: [PATCH 8/8] --Lint fix-- --- test/samples/test.samples.jobs.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/samples/test.samples.jobs.ts b/test/samples/test.samples.jobs.ts index 395478af0ae..5bbd9ffd0ef 100644 --- a/test/samples/test.samples.jobs.ts +++ b/test/samples/test.samples.jobs.ts @@ -34,9 +34,10 @@ describe.skip('Talent API Samples', () => { }); it('should create a company', async () => { - const scope = nock(Utils.baseUrl) - .post(`/v3/jobs/project/${process.env.GCLOUD_PROJECT}/companies`) - .reply(200, {}); + const scope = + nock(Utils.baseUrl) + .post(`/v3/jobs/project/${process.env.GCLOUD_PROJECT}/companies`) + .reply(200, {}); const data = await samples.jobs.runSample(); assert(data); scope.done();