Skip to content

Commit a28eca0

Browse files
xinyunh0929chenyumic
authored andcommitted
Added Cloud Job Discovery API client samples (GoogleCloudPlatform#1546)
* First version of CJD samples * Add more samples * Add more samples * Add more samples * Add more samples * Add quickstart and remove the deprecated sample * Remove unused dependency and adjust the indentation * Fix the lint issue * Fix the lint issue * Fix the lint issue * Move thes samples from /jobs/cjd_sample to /jobs * Modify the quickstart test * Add tests, modify tags, remove env and some other changes * format the code * Fx the issue in the sample and test * Remove all the extra empty lines * Fix the lint issue
1 parent 7169f29 commit a28eca0

27 files changed

+1869
-0
lines changed

jobs/api_client/README.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Job Discovery API Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=jobs/cjd_sample/README.rst
8+
9+
10+
This directory contains samples for Google Cloud Job Discovery API.
11+
The `Google Job Discovery API` is part of Google for Jobs - a Google-wide commitment to help people find jobs more easily. Job Discovery provides plug and play access to Google’s search and machine learning capabilities, enabling the entire recruiting ecosystem - company career sites, job boards, applicant tracking systems, and staffing agencies to improve job site engagement and candidate conversion.
12+
13+
14+
15+
.. _Google Cloud Job Discovery API: https://cloud.google.com/job-discovery/docs/
16+
17+
Setup
18+
-------------------------------------------------------------------------------
19+
20+
21+
Authentication
22+
++++++++++++++
23+
24+
This sample requires you to have authentication setup. Refer to the
25+
`Authentication Getting Started Guide`_ for instructions on setting up
26+
credentials for applications.
27+
28+
.. _Authentication Getting Started Guide:
29+
https://cloud.google.com/docs/authentication/getting-started
30+
31+
Install Dependencies
32+
++++++++++++++++++++
33+
34+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
35+
36+
.. code-block:: bash
37+
38+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
39+
40+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
41+
42+
.. _Python Development Environment Setup Guide:
43+
https://cloud.google.com/python/setup
44+
45+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
46+
47+
.. code-block:: bash
48+
49+
$ virtualenv env
50+
$ source env/bin/activate
51+
52+
#. Install the dependencies needed to run the samples.
53+
54+
.. code-block:: bash
55+
56+
$ pip install -r requirements.txt
57+
58+
.. _pip: https://pip.pypa.io/
59+
.. _virtualenv: https://virtualenv.pypa.io/
60+
61+
Samples
62+
-------------------------------------------------------------------------------
63+
64+
Quickstart
65+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
66+
67+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
68+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=jobs/cjd_sample/base_company_sample.py,jobs/cjd_sample/README.rst
69+
70+
71+
To run this sample:
72+
73+
.. code-block:: bash
74+
75+
$ python base_company_sample.py
76+
77+
78+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

jobs/api_client/README.rst.in

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Job Discovery API
5+
short_name: CJD
6+
url: https://cloud.google.com/job-discovery/docs/
7+
description: >
8+
`Cloud Job Discovery` is a service that brings machine learning to your job
9+
search experience, returning high quality results to job seekers far beyond
10+
the limitations of typical keyword-based methods. Once integrated with your
11+
job content, Cloud Job Discovery automatically detects and infers various
12+
kinds of data, such as related titles, seniority, and industry.
13+
14+
setup:
15+
- auth
16+
- install_deps
17+
18+
samples:
19+
- name: AutoCompleteSample
20+
file: auto_complete_sample.py
21+
- name: BaseCompanySample
22+
file: base_company_sample.py
23+
- name: BaseJobSample
24+
file: base_job_sample.py
25+
- name: BatchOperationSample
26+
file: batch_operation_sample.py
27+
- name: CommuteSearchSample
28+
file: commute_search_sample.py
29+
- name: CustomAttributeSample
30+
file: custom_attribute_sample.py
31+
- name: EmailAlertSearchSample
32+
file: email_alert_search_sample.py
33+
- name: FeaturedJobSearchSample
34+
file: featured_job_search_sample.py
35+
- name: GeneraSearchSample
36+
file: general_search_sample.py
37+
- name: HistogramSample
38+
file: histogram_sample.py
39+
- name: LocationSearchSample
40+
file: location_search_sample.py
41+
42+
folder: jobs/cjd_sample
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import time
18+
19+
# [START instantiate]
20+
from googleapiclient.discovery import build
21+
22+
client_service = build('jobs', 'v2')
23+
# [END instantiate]
24+
25+
26+
# [START auto_complete_job_title]
27+
def job_title_auto_complete(client_service, query, company_name):
28+
complete = client_service.v2().complete(
29+
query=query, languageCode='en-US', type='JOB_TITLE', pageSize=10)
30+
if company_name is not None:
31+
complete.companyName = company_name
32+
33+
results = complete.execute()
34+
print(results)
35+
# [END auto_complete_job_title]
36+
37+
38+
# [START auto_complete_default]
39+
def auto_complete_default(client_service, query, company_name):
40+
complete = client_service.v2().complete(
41+
query=query, languageCode='en-US', pageSize=10)
42+
if company_name is not None:
43+
complete.companyName = company_name
44+
45+
results = complete.execute()
46+
print(results)
47+
# [END auto_complete_default]
48+
49+
50+
def run_sample():
51+
import base_company_sample
52+
import base_job_sample
53+
54+
company_to_be_created = base_company_sample.generate_company()
55+
company_created = base_company_sample.create_company(
56+
client_service, company_to_be_created)
57+
company_name = company_created.get('name')
58+
59+
job_to_be_created = base_job_sample.generate_job_with_required_fields(
60+
company_name)
61+
job_to_be_created.update({'job_title': 'Software engineer'})
62+
job_name = base_job_sample.create_job(client_service,
63+
job_to_be_created).get('name')
64+
65+
# Wait several seconds for post processing
66+
time.sleep(10)
67+
auto_complete_default(client_service, 'goo', company_name)
68+
auto_complete_default(client_service, 'sof', company_name)
69+
job_title_auto_complete(client_service, 'sof', company_name)
70+
71+
base_job_sample.delete_job(client_service, job_name)
72+
base_company_sample.delete_company(client_service, company_name)
73+
74+
75+
if __name__ == '__main__':
76+
run_sample()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2018 Google LLC. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def test_auto_complete_sample(capsys):
17+
import auto_complete_sample
18+
import re
19+
20+
auto_complete_sample.run_sample()
21+
out, _ = capsys.readouterr()
22+
expected = (
23+
'.*completionResults.*'
24+
'suggestion.*Google.*type.*COMPANY_NAME.*\n'
25+
'.*completionResults.*'
26+
'suggestion.*Software Engineer.*type.*JOB_TITLE.*\n'
27+
'.*completionResults.*'
28+
'suggestion.*Software Engineer.*type.*JOB_TITLE.*\n'
29+
)
30+
assert re.search(expected, out)
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import random
18+
import string
19+
20+
# [START jobs_instantiate]
21+
from googleapiclient.discovery import build
22+
from googleapiclient.errors import Error
23+
24+
client_service = build('jobs', 'v2')
25+
# [END jobs_instantiate]
26+
27+
28+
# [START jobs_basic_company]
29+
def generate_company():
30+
# distributor company id should be a unique Id in your system.
31+
distributor_company_id = 'company:' + ''.join(
32+
random.choice(string.ascii_uppercase + string.digits)
33+
for _ in range(16))
34+
35+
display_name = 'Google'
36+
hq_location = '1600 Amphitheatre Parkway Mountain View, CA 94043'
37+
38+
company = {
39+
'display_name': display_name,
40+
'distributor_company_id': distributor_company_id,
41+
'hq_location': hq_location
42+
}
43+
print('Company generated: %s' % company)
44+
return company
45+
# [END jobs_basic_company]
46+
47+
48+
# [START jobs_create_company]
49+
def create_company(client_service, company_to_be_created):
50+
try:
51+
company_created = client_service.companies().create(
52+
body=company_to_be_created).execute()
53+
print('Company created: %s' % company_created)
54+
return company_created
55+
except Error as e:
56+
print('Got exception while creating company')
57+
raise e
58+
# [END jobs_create_company]
59+
60+
61+
# [START jobs_get_company]
62+
def get_company(client_service, company_name):
63+
try:
64+
company_existed = client_service.companies().get(
65+
name=company_name).execute()
66+
print('Company existed: %s' % company_existed)
67+
return company_existed
68+
except Error as e:
69+
print('Got exception while getting company')
70+
raise e
71+
# [END jobs_get_company]
72+
73+
74+
# [START jobs_update_company]
75+
def update_company(client_service, company_name, company_to_be_updated):
76+
try:
77+
company_updated = client_service.companies().patch(
78+
name=company_name, body=company_to_be_updated).execute()
79+
print('Company updated: %s' % company_updated)
80+
return company_updated
81+
except Error as e:
82+
print('Got exception while updating company')
83+
raise e
84+
# [END jobs_update_company]
85+
86+
87+
# [START jobs_update_company_with_field_mask]
88+
def update_company_with_field_mask(client_service, company_name,
89+
company_to_be_updated, field_mask):
90+
try:
91+
company_updated = client_service.companies().patch(
92+
name=company_name,
93+
body=company_to_be_updated,
94+
updateCompanyFields=field_mask).execute()
95+
print('Company updated: %s' % company_updated)
96+
return company_updated
97+
except Error as e:
98+
print('Got exception while updating company with field mask')
99+
raise e
100+
# [END jobs_update_company_with_field_mask]
101+
102+
103+
# [START jobs_delete_company]
104+
def delete_company(client_service, company_name):
105+
try:
106+
client_service.companies().delete(name=company_name).execute()
107+
print('Company deleted')
108+
except Error as e:
109+
print('Got exception while deleting company')
110+
raise e
111+
# [END jobs_delete_company]
112+
113+
114+
def run_sample():
115+
# Construct a company
116+
company_to_be_created = generate_company()
117+
118+
# Create a company
119+
company_created = create_company(client_service, company_to_be_created)
120+
121+
# Get a company
122+
company_name = company_created.get('name')
123+
get_company(client_service, company_name)
124+
125+
# Update a company
126+
company_to_be_updated = company_created
127+
company_to_be_updated.update({'website': 'https://elgoog.im/'})
128+
update_company(client_service, company_name, company_to_be_updated)
129+
130+
# Update a company with field mask
131+
update_company_with_field_mask(
132+
client_service, company_name, {
133+
'displayName': 'changedTitle',
134+
'distributorCompanyId': company_created.get('distributorCompanyId')
135+
}, 'displayName')
136+
137+
# Delete a company
138+
delete_company(client_service, company_name)
139+
140+
141+
if __name__ == '__main__':
142+
run_sample()

0 commit comments

Comments
 (0)