Skip to content
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

docs(samples): Talent API Sample Revision & Test #1546

Merged
merged 20 commits into from Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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.

Expand Down
53 changes: 53 additions & 0 deletions samples/jobs/jobs.js
@@ -0,0 +1,53 @@
// 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 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/${projectId}`,
requestBody: {
company: {
displayName: 'ABC co.',
externalId: '12345',
},
},
});
console.log(res.data);
return res.data;
}

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);
}

module.exports = {
runSample,
client: sampleClient.oAuth2Client,
};
4 changes: 4 additions & 0 deletions src/apis/jobs/README.md
Expand Up @@ -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).

Expand Down
44 changes: 44 additions & 0 deletions 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`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! I think this is the problem. For this url, can you use ${process.env.GCLOUD_PROJECT}?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I tried it several different ways though and I'm still getting:

FetchError: request to https://jobs.googleapis.com/v3/project/project-id/companies failed, 
reason: Nock: Disallowed net connect for "jobs.googleapis.com:443/v3/project/project-id/companies"

🎊

I accepted the invitation (thanks!), I'll try pushing a branch upstream once auth allows it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh weird. Share your updated code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Here's version 1:

  it('should create a company', async () => {
    const scope = nock(Utils.baseUrl)
                      .post(`/v3/jobs/project/${process.env.GCLOUD_PROJECT}/companies`)

version 2:

  it('should create a company', async () => {
    const scope = nock(Utils.baseUrl)
                      .post(`${process.env.GCLOUD_PROJECT}`)

I tried a few other ways but the above two are the ones that make sense

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, v1 looks right to me

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some logging revealed that GCLOUD_PROJECT is undefined until we go into runSample's scope where we call on the auth service to generate a project Id - copying the same code ( const projectId = await google.auth.getProjectId();) into the test's scope gives us undefined too so.. any chance we can erase this test and push the sample up anyway? A bit anarchistic but I noticed there are a few other APIs without them

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the test, just put a .skip on it :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Done!

.reply(200, {});
const data = await samples.jobs.runSample();
assert(data);
scope.done();
});
});