Skip to content

Commit

Permalink
samples: add Job Search v4 samples (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
saumyasahu-bot authored and telpirion committed Nov 9, 2022
1 parent 648d8a4 commit fc77a40
Show file tree
Hide file tree
Showing 22 changed files with 1,555 additions and 4 deletions.
12 changes: 9 additions & 3 deletions talent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
"*.js"
],
"scripts": {
"test": "mocha --timeout 600000"
"test": "mocha --timeout 600000",
"lint": "eslint src --ext .ts",
"lint:fix": "npm run lint -- --fix"
},
"dependencies": {
"@google-cloud/talent": "^3.0.0"
"@google-cloud/talent": "^3.0.0",
"uuid": "^8.3.1",
"yargs": "^16.0.3"
},
"devDependencies": {
"@types/mocha": "^8.0.0",
"@types/node": "^12.0.0",
"chai": "^4.2.0",
"mocha": "^8.0.0"
"mocha": "^8.1.3"
}
}
94 changes: 94 additions & 0 deletions talent/snippet/job_search_autocomplete_job_title.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2020 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
//
// https://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 job_search_autocomplete_job_title]

const talent = require('@google-cloud/talent').v4;

/**
* Complete job title given partial text (autocomplete)
*
* @param projectId {string} Your Google Cloud Project ID
* @param tenantId {string} Identifier of the TenantId
*/
function sampleCompleteQuery(
projectId,
tenantId,
query,
numResults,
languageCode
) {
const client = new talent.CompletionClient();
// const projectId = 'Your Google Cloud Project ID';
// const tenantId = 'Your Tenant ID (using tenancy is optional)';
// const query = '[partially typed job title]';
// const numResults = 5;
// const languageCode = 'en-US';
const formattedParent = client.tenantPath(projectId, tenantId);
const languageCodes = [languageCode];
const request = {
parent: formattedParent,
query: query,
pageSize: numResults,
languageCodes: languageCodes,
};
client
.completeQuery(request)
.then(responses => {
const response = responses[0];
for (const result of response.completionResults) {
console.log(`Suggested title: ${result.suggestion}`);
// Suggestion type is JOB_TITLE or COMPANY_TITLE
console.log(`Suggestion type: ${result.type}`);
}
})
.catch(err => {
console.error(err);
});
}

// [END job_search_autocomplete_job_title]
// tslint:disable-next-line:no-any

const argv = require('yargs')
.option('project_id', {
default: 'Your Google Cloud Project ID',
string: true,
})
.option('tenant_id', {
default: 'Your Tenant ID (using tenancy is optional)',
string: true,
})
.option('query', {
default: '[partially typed job title]',
string: true,
})
.option('num_results', {
default: 5,
number: true,
})
.option('language_code', {
default: 'en-US',
string: true,
}).argv;

sampleCompleteQuery(
argv.project_id,
argv.tenant_id,
argv.query,
argv.num_results,
argv.language_code
);
63 changes: 63 additions & 0 deletions talent/snippet/job_search_batch_delete_job.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2020 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
//
// https://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 job_search_batch_delete_job]

const talent = require('@google-cloud/talent').v4;

/**
* Batch delete jobs using a filter
*
* @param projectId {string} Your Google Cloud Project ID
* @param tenantId {string} Identifier of the Tenantd
* @param filter {string} The filter string specifies the jobs to be deleted.
* For example:
* companyName = "projects/api-test-project/companies/123" AND equisitionId = "req-1"
*/
function sampleBatchDeleteJobs(projectId, tenantId, filter) {
const client = new talent.JobServiceClient();
// const projectId = 'Your Google Cloud Project ID';
// const tenantId = 'Your Tenant ID (using tenancy is optional)';
// const filter = '[Query]';
const formattedParent = client.tenantPath(projectId, tenantId);
const request = {
parent: formattedParent,
filter: filter,
};
client.batchDeleteJobs(request).catch(err => {
console.error(err);
});
console.log('Batch deleted jobs from filter');
}

// [END job_search_batch_delete_job]
// tslint:disable-next-line:no-any

const argv = require('yargs')
.option('project_id', {
default: 'Your Google Cloud Project ID',
string: true,
})
.option('tenant_id', {
default: 'Your Tenant ID (using tenancy is optional)',
string: true,
})
.option('filter', {
default: '[Query]',
string: true,
}).argv;

sampleBatchDeleteJobs(argv.project_id, argv.tenant_id, argv.filter);
91 changes: 91 additions & 0 deletions talent/snippet/job_search_commute_search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2020 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
//
// https://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 job_search_commute_search]

const talent = require('@google-cloud/talent').v4;

/** Search Jobs using commute distance */
function sampleSearchJobs(projectId, tenantId) {
const client = new talent.JobServiceClient();
// Iterate over all elements.
// const projectId = 'Your Google Cloud Project ID';
// const tenantId = 'Your Tenant ID (using tenancy is optional)';
const formattedParent = client.tenantPath(projectId, tenantId);
const domain = 'www.example.com';
const sessionId = 'Hashed session identifier';
const userId = 'Hashed user identifier';
const requestMetadata = {
domain: domain,
sessionId: sessionId,
userId: userId,
};
const commuteMethod = 'TRANSIT';
const seconds = 1800;
const travelDuration = {
seconds: seconds,
};
const latitude = 37.422408;
const longitude = 122.084068;
const startCoordinates = {
latitude: latitude,
longitude: longitude,
};
const commuteFilter = {
commuteMethod: commuteMethod,
travelDuration: travelDuration,
startCoordinates: startCoordinates,
};
const jobQuery = {
commuteFilter: commuteFilter,
};
const request = {
parent: formattedParent,
requestMetadata: requestMetadata,
jobQuery: jobQuery,
};

client
.searchJobs(request)
.then(responses => {
const resources = responses[0];
for (const resource of resources) {
console.log(`Job summary: ${resource.jobSummary}`);
console.log(`Job title snippet: ${resource.jobTitleSnippet}`);
const job = resource.job;
console.log(`Job name: ${job.name}`);
console.log(`Job title: ${job.title}`);
}
})
.catch(err => {
console.error(err);
});
}

// [END job_search_commute_search]
// tslint:disable-next-line:no-any

const argv = require('yargs')
.option('project_id', {
default: 'Your Google Cloud Project ID',
string: true,
})
.option('tenant_id', {
default: 'Your Tenant ID (using tenancy is optional)',
string: true,
}).argv;

sampleSearchJobs(argv.project_id, argv.tenant_id);
106 changes: 106 additions & 0 deletions talent/snippet/job_search_create_client_event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2020 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
//
// https://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 job_search_create_client_event]

const talent = require('@google-cloud/talent').v4;

/**
* Creates a client event
*
* @param projectId {string} Your Google Cloud Project ID
* @param tenantId {string} Identifier of the Tenant
* @param requestId {string} A unique ID generated in the API responses.
* Value should be set to the request_id from an API response.
* @param eventId {string} A unique identifier, generated by the client application
*/
function sampleCreateClientEvent(projectId, tenantId, requestId, eventId) {
const client = new talent.EventServiceClient();
// const projectId = 'Your Google Cloud Project ID';
// const tenantId = 'Your Tenant ID (using tenancy is optional)';
// const requestId = '[request_id from ResponseMetadata]';
// const eventId = '[Set this to a unique identifier]';
const formattedParent = client.tenantPath(projectId, tenantId);

// The timestamp of the event as seconds of UTC time since Unix epoch
// For more information on how to create google.protobuf.Timestamps
// See: https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/timestamp.proto
const seconds = 0;
const createTime = {
seconds: seconds,
};

// The type of event attributed to the behavior of the end user
const type = 'VIEW';

// List of job names associated with this event
const jobsElement = 'projects/[Project ID]/tenants/[Tenant ID]/jobs/[Job ID]';
const jobsElement2 =
'projects/[Project ID]/tenants/[Tenant ID]/jobs/[Job ID]';
const jobs = [jobsElement, jobsElement2];
const jobEvent = {
type: type,
jobs: jobs,
};
const clientEvent = {
requestId: requestId,
eventId: eventId,
createTime: createTime,
jobEvent: jobEvent,
};
const request = {
parent: formattedParent,
clientEvent: clientEvent,
};
client
.createClientEvent(request)
.then(responses => {
const response = responses[0];
console.log('Created client event');
console.log(`Response: ${response}`);
})
.catch(err => {
console.error(err);
});
}

// [END job_search_create_client_event]
// tslint:disable-next-line:no-any

const argv = require('yargs')
.option('project_id', {
default: 'Your Google Cloud Project ID',
string: true,
})
.option('tenant_id', {
default: 'Your Tenant ID (using tenancy is optional)',
string: true,
})
.option('request_id', {
default: '[request_id from ResponseMetadata]',
string: true,
})
.option('event_id', {
default: '[Set this to a unique identifier]',
string: true,
}).argv;

sampleCreateClientEvent(
argv.project_id,
argv.tenant_id,
argv.request_id,
argv.event_id
);
Loading

0 comments on commit fc77a40

Please sign in to comment.