-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Check in Cloud Job Discovery client code samples for python #1546
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ca002fa
First version of CJD samples
xinyunh0929 e2e34b8
Add more samples
xinyunh0929 7963e55
Add more samples
xinyunh0929 dbc7429
Add more samples
xinyunh0929 9e449a3
Add more samples
xinyunh0929 05120ae
Add quickstart and remove the deprecated sample
xinyunh0929 5a5ae21
Remove unused dependency and adjust the indentation
xinyunh0929 2ede384
Fix the lint issue
xinyunh0929 7cf9d0f
Fix the lint issue
xinyunh0929 85d20b0
Fix the lint issue
xinyunh0929 3410e74
Move thes samples from /jobs/cjd_sample to /jobs
c23b12b
Modify the quickstart test
776987d
Add tests, modify tags, remove env and some other changes
99a735a
format the code
eabb451
Fx the issue in the sample and test
966a271
Remove all the extra empty lines
8e7f27c
Fix the lint issue
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,78 @@ | ||
| .. This file is automatically generated. Do not edit this file directly. | ||
|
|
||
| Google Cloud Job Discovery API Python Samples | ||
| =============================================================================== | ||
|
|
||
| .. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
| :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 | ||
|
|
||
|
|
||
| This directory contains samples for Google Cloud Job Discovery API. | ||
| 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. | ||
|
|
||
|
|
||
|
|
||
| .. _Google Cloud Job Discovery API: https://cloud.google.com/job-discovery/docs/ | ||
|
|
||
| Setup | ||
| ------------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| Authentication | ||
| ++++++++++++++ | ||
|
|
||
| This sample requires you to have authentication setup. Refer to the | ||
| `Authentication Getting Started Guide`_ for instructions on setting up | ||
| credentials for applications. | ||
|
|
||
| .. _Authentication Getting Started Guide: | ||
| https://cloud.google.com/docs/authentication/getting-started | ||
|
|
||
| Install Dependencies | ||
| ++++++++++++++++++++ | ||
|
|
||
| #. Clone python-docs-samples and change directory to the sample directory you want to use. | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git | ||
|
|
||
| #. 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. | ||
|
|
||
| .. _Python Development Environment Setup Guide: | ||
| https://cloud.google.com/python/setup | ||
|
|
||
| #. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| $ virtualenv env | ||
| $ source env/bin/activate | ||
|
|
||
| #. Install the dependencies needed to run the samples. | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| $ pip install -r requirements.txt | ||
|
|
||
| .. _pip: https://pip.pypa.io/ | ||
| .. _virtualenv: https://virtualenv.pypa.io/ | ||
|
|
||
| Samples | ||
| ------------------------------------------------------------------------------- | ||
|
|
||
| Quickstart | ||
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
|
||
| .. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
| :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 | ||
|
|
||
|
|
||
| To run this sample: | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| $ python base_company_sample.py | ||
|
|
||
|
|
||
| .. _Google Cloud SDK: https://cloud.google.com/sdk/ |
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,42 @@ | ||
| # This file is used to generate README.rst | ||
|
|
||
| product: | ||
| name: Google Cloud Job Discovery API | ||
| short_name: CJD | ||
| url: https://cloud.google.com/job-discovery/docs/ | ||
| description: > | ||
| `Cloud Job Discovery` is a service that brings machine learning to your job | ||
| search experience, returning high quality results to job seekers far beyond | ||
| the limitations of typical keyword-based methods. Once integrated with your | ||
| job content, Cloud Job Discovery automatically detects and infers various | ||
| kinds of data, such as related titles, seniority, and industry. | ||
|
|
||
| setup: | ||
| - auth | ||
| - install_deps | ||
|
|
||
| samples: | ||
| - name: AutoCompleteSample | ||
| file: auto_complete_sample.py | ||
| - name: BaseCompanySample | ||
| file: base_company_sample.py | ||
| - name: BaseJobSample | ||
| file: base_job_sample.py | ||
| - name: BatchOperationSample | ||
| file: batch_operation_sample.py | ||
| - name: CommuteSearchSample | ||
| file: commute_search_sample.py | ||
| - name: CustomAttributeSample | ||
| file: custom_attribute_sample.py | ||
| - name: EmailAlertSearchSample | ||
| file: email_alert_search_sample.py | ||
| - name: FeaturedJobSearchSample | ||
| file: featured_job_search_sample.py | ||
| - name: GeneraSearchSample | ||
| file: general_search_sample.py | ||
| - name: HistogramSample | ||
| file: histogram_sample.py | ||
| - name: LocationSearchSample | ||
| file: location_search_sample.py | ||
|
|
||
| folder: jobs/cjd_sample |
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,76 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2016 Google Inc. All Rights Reserved. | ||
| # | ||
| # 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 time | ||
|
|
||
| # [START instantiate] | ||
| from googleapiclient.discovery import build | ||
|
|
||
| client_service = build('jobs', 'v2') | ||
| # [END instantiate] | ||
|
|
||
|
|
||
| # [START auto_complete_job_title] | ||
| def job_title_auto_complete(client_service, query, company_name): | ||
| complete = client_service.v2().complete( | ||
| query=query, languageCode='en-US', type='JOB_TITLE', pageSize=10) | ||
| if company_name is not None: | ||
| complete.companyName = company_name | ||
|
|
||
| results = complete.execute() | ||
| print(results) | ||
| # [END auto_complete_job_title] | ||
|
|
||
|
|
||
| # [START auto_complete_default] | ||
| def auto_complete_default(client_service, query, company_name): | ||
| complete = client_service.v2().complete( | ||
| query=query, languageCode='en-US', pageSize=10) | ||
| if company_name is not None: | ||
| complete.companyName = company_name | ||
|
|
||
| results = complete.execute() | ||
| print(results) | ||
| # [END auto_complete_default] | ||
|
|
||
|
|
||
| def run_sample(): | ||
| import base_company_sample | ||
| import base_job_sample | ||
|
|
||
| company_to_be_created = base_company_sample.generate_company() | ||
| company_created = base_company_sample.create_company( | ||
| client_service, company_to_be_created) | ||
| company_name = company_created.get('name') | ||
|
|
||
| job_to_be_created = base_job_sample.generate_job_with_required_fields( | ||
| company_name) | ||
| job_to_be_created.update({'job_title': 'Software engineer'}) | ||
| job_name = base_job_sample.create_job(client_service, | ||
| job_to_be_created).get('name') | ||
|
|
||
| # Wait several seconds for post processing | ||
| time.sleep(10) | ||
| auto_complete_default(client_service, 'goo', company_name) | ||
| auto_complete_default(client_service, 'sof', company_name) | ||
| job_title_auto_complete(client_service, 'sof', company_name) | ||
|
|
||
| base_job_sample.delete_job(client_service, job_name) | ||
| base_company_sample.delete_company(client_service, company_name) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| run_sample() | ||
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,30 @@ | ||
| # Copyright 2018 Google LLC. All Rights Reserved. | ||
| # | ||
| # 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. | ||
|
|
||
|
|
||
| def test_auto_complete_sample(capsys): | ||
| import auto_complete_sample | ||
| import re | ||
|
|
||
| auto_complete_sample.run_sample() | ||
| out, _ = capsys.readouterr() | ||
| expected = ( | ||
| '.*completionResults.*' | ||
| 'suggestion.*Google.*type.*COMPANY_NAME.*\n' | ||
| '.*completionResults.*' | ||
| 'suggestion.*Software Engineer.*type.*JOB_TITLE.*\n' | ||
| '.*completionResults.*' | ||
| 'suggestion.*Software Engineer.*type.*JOB_TITLE.*\n' | ||
| ) | ||
| assert re.search(expected, out) |
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,142 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2018 Google LLC. All Rights Reserved. | ||
| # | ||
| # 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 random | ||
| import string | ||
|
|
||
| # [START jobs_instantiate] | ||
| from googleapiclient.discovery import build | ||
| from googleapiclient.errors import Error | ||
|
|
||
| client_service = build('jobs', 'v2') | ||
| # [END jobs_instantiate] | ||
|
|
||
|
|
||
| # [START jobs_basic_company] | ||
| def generate_company(): | ||
| # distributor company id should be a unique Id in your system. | ||
| distributor_company_id = 'company:' + ''.join( | ||
| random.choice(string.ascii_uppercase + string.digits) | ||
| for _ in range(16)) | ||
|
|
||
| display_name = 'Google' | ||
| hq_location = '1600 Amphitheatre Parkway Mountain View, CA 94043' | ||
|
|
||
| company = { | ||
| 'display_name': display_name, | ||
| 'distributor_company_id': distributor_company_id, | ||
| 'hq_location': hq_location | ||
| } | ||
| print('Company generated: %s' % company) | ||
| return company | ||
| # [END jobs_basic_company] | ||
|
|
||
|
|
||
| # [START jobs_create_company] | ||
| def create_company(client_service, company_to_be_created): | ||
| try: | ||
| company_created = client_service.companies().create( | ||
| body=company_to_be_created).execute() | ||
| print('Company created: %s' % company_created) | ||
| return company_created | ||
| except Error as e: | ||
| print('Got exception while creating company') | ||
| raise e | ||
| # [END jobs_create_company] | ||
|
|
||
|
|
||
| # [START jobs_get_company] | ||
| def get_company(client_service, company_name): | ||
| try: | ||
| company_existed = client_service.companies().get( | ||
| name=company_name).execute() | ||
| print('Company existed: %s' % company_existed) | ||
| return company_existed | ||
| except Error as e: | ||
| print('Got exception while getting company') | ||
| raise e | ||
| # [END jobs_get_company] | ||
|
|
||
|
|
||
| # [START jobs_update_company] | ||
| def update_company(client_service, company_name, company_to_be_updated): | ||
| try: | ||
| company_updated = client_service.companies().patch( | ||
| name=company_name, body=company_to_be_updated).execute() | ||
| print('Company updated: %s' % company_updated) | ||
| return company_updated | ||
| except Error as e: | ||
| print('Got exception while updating company') | ||
| raise e | ||
| # [END jobs_update_company] | ||
|
|
||
|
|
||
| # [START jobs_update_company_with_field_mask] | ||
| def update_company_with_field_mask(client_service, company_name, | ||
| company_to_be_updated, field_mask): | ||
| try: | ||
| company_updated = client_service.companies().patch( | ||
| name=company_name, | ||
| body=company_to_be_updated, | ||
| updateCompanyFields=field_mask).execute() | ||
| print('Company updated: %s' % company_updated) | ||
| return company_updated | ||
| except Error as e: | ||
| print('Got exception while updating company with field mask') | ||
| raise e | ||
| # [END jobs_update_company_with_field_mask] | ||
|
|
||
|
|
||
| # [START jobs_delete_company] | ||
| def delete_company(client_service, company_name): | ||
| try: | ||
| client_service.companies().delete(name=company_name).execute() | ||
| print('Company deleted') | ||
| except Error as e: | ||
| print('Got exception while deleting company') | ||
| raise e | ||
| # [END jobs_delete_company] | ||
|
|
||
|
|
||
| def run_sample(): | ||
| # Construct a company | ||
| company_to_be_created = generate_company() | ||
|
|
||
| # Create a company | ||
| company_created = create_company(client_service, company_to_be_created) | ||
|
|
||
| # Get a company | ||
| company_name = company_created.get('name') | ||
| get_company(client_service, company_name) | ||
|
|
||
| # Update a company | ||
| company_to_be_updated = company_created | ||
| company_to_be_updated.update({'website': 'https://elgoog.im/'}) | ||
| update_company(client_service, company_name, company_to_be_updated) | ||
|
|
||
| # Update a company with field mask | ||
| update_company_with_field_mask( | ||
| client_service, company_name, { | ||
| 'displayName': 'changedTitle', | ||
| 'distributorCompanyId': company_created.get('distributorCompanyId') | ||
| }, 'displayName') | ||
|
|
||
| # Delete a company | ||
| delete_company(client_service, company_name) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| run_sample() |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Please remove extra empty line(s) throughout the code.
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.
Done.