diff --git a/jobs/api_client/README.rst b/jobs/api_client/README.rst new file mode 100755 index 00000000000..d9efd858528 --- /dev/null +++ b/jobs/api_client/README.rst @@ -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/ \ No newline at end of file diff --git a/jobs/api_client/README.rst.in b/jobs/api_client/README.rst.in new file mode 100755 index 00000000000..d96831be7b0 --- /dev/null +++ b/jobs/api_client/README.rst.in @@ -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 \ No newline at end of file diff --git a/jobs/api_client/auto_complete_sample.py b/jobs/api_client/auto_complete_sample.py new file mode 100755 index 00000000000..e96b0dcb2f6 --- /dev/null +++ b/jobs/api_client/auto_complete_sample.py @@ -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() diff --git a/jobs/api_client/auto_complete_sample_test.py b/jobs/api_client/auto_complete_sample_test.py new file mode 100755 index 00000000000..05866c4a967 --- /dev/null +++ b/jobs/api_client/auto_complete_sample_test.py @@ -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) diff --git a/jobs/api_client/base_company_sample.py b/jobs/api_client/base_company_sample.py new file mode 100755 index 00000000000..4eb1d96dee7 --- /dev/null +++ b/jobs/api_client/base_company_sample.py @@ -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() diff --git a/jobs/api_client/base_company_sample_test.py b/jobs/api_client/base_company_sample_test.py new file mode 100755 index 00000000000..b07c8623f7b --- /dev/null +++ b/jobs/api_client/base_company_sample_test.py @@ -0,0 +1,28 @@ +# 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_base_company_sample(capsys): + import base_company_sample + import re + + base_company_sample.run_sample() + out, _ = capsys.readouterr() + expected = ('.*Company generated:.*\n' + '.*Company created:.*\n' + '.*Company existed:.*\n' + '.*Company updated:.*elgoog.*\n' + '.*Company updated:.*changedTitle.*\n' + '.*Company deleted.*\n') + assert re.search(expected, out, re.DOTALL) diff --git a/jobs/api_client/base_job_sample.py b/jobs/api_client/base_job_sample.py new file mode 100755 index 00000000000..3f623311d9e --- /dev/null +++ b/jobs/api_client/base_job_sample.py @@ -0,0 +1,153 @@ +#!/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 random +import string + +# [START instantiate] +from googleapiclient.discovery import build +from googleapiclient.errors import Error + +client_service = build('jobs', 'v2') +# [END instantiate] + + +# [START basic_job] +def generate_job_with_required_fields(company_name): + # Requisition id should be a unique Id in your system. + requisition_id = 'job_with_required_fields:' + ''.join( + random.choice(string.ascii_uppercase + string.digits) + for _ in range(16)) + + job_title = 'Software Engineer' + application_urls = ['http://careers.google.com'] + description = ('Design, develop, test, deploy, maintain and improve ' + 'software.') + + job = { + 'requisition_id': requisition_id, + 'job_title': job_title, + 'application_urls': application_urls, + 'description': description, + 'company_name': company_name + } + print('Job generated: %s' % job) + return job +# [END basic_job] + + +# [START create_job] +def create_job(client_service, job_to_be_created): + try: + request = {'job': job_to_be_created} + job_created = client_service.jobs().create(body=request).execute() + print('Job created: %s' % job_created) + return job_created + except Error as e: + print('Got exception while creating job') + raise e +# [END create_job] + + +# [START get_job] +def get_job(client_service, job_name): + try: + job_existed = client_service.jobs().get(name=job_name).execute() + print('Job existed: %s' % job_existed) + return job_existed + except Error as e: + print('Got exception while getting job') + raise e +# [END get_job] + + +# [START update_job] +def update_job(client_service, job_name, job_to_be_updated): + try: + request = {'job': job_to_be_updated} + job_updated = client_service.jobs().patch( + name=job_name, body=request).execute() + print('Job updated: %s' % job_updated) + return job_updated + except Error as e: + print('Got exception while updating job') + raise e +# [END update_job] + + +# [START update_job_with_field_mask] +def update_job_with_field_mask(client_service, job_name, job_to_be_updated, + field_mask): + try: + request = {'job': job_to_be_updated, 'update_job_fields': field_mask} + job_updated = client_service.jobs().patch( + name=job_name, body=request).execute() + print('Job updated: %s' % job_updated) + return job_updated + except Error as e: + print('Got exception while updating job with field mask') + raise e +# [END update_job_with_field_mask] + + +# [START delete_job] +def delete_job(client_service, job_name): + try: + client_service.jobs().delete(name=job_name).execute() + print('Job deleted') + except Error as e: + print('Got exception while deleting job') + raise e +# [END delete_job] + + +def run_sample(): + import base_company_sample + + # Create a company before creating jobs + 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') + + # Construct a job + job_to_be_created = generate_job_with_required_fields(company_name) + + # Create a job + job_created = create_job(client_service, job_to_be_created) + + # Get a job + job_name = job_created.get('name') + get_job(client_service, job_name) + + # Update a job + job_to_be_updated = job_created + job_to_be_updated.update({'description': 'changedDescription'}) + update_job(client_service, job_name, job_to_be_updated) + + # Update a job with field mask + update_job_with_field_mask(client_service, job_name, + {'job_title': 'changedJobTitle'}, 'job_title') + + # Delete a job + delete_job(client_service, job_name) + + # Delete company only after cleaning all jobs under this company + base_company_sample.delete_company(client_service, company_name) + + +if __name__ == '__main__': + run_sample() diff --git a/jobs/api_client/base_job_sample_test.py b/jobs/api_client/base_job_sample_test.py new file mode 100755 index 00000000000..5f7d8bb1d17 --- /dev/null +++ b/jobs/api_client/base_job_sample_test.py @@ -0,0 +1,28 @@ +# 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_base_job_sample(capsys): + import base_job_sample + import re + + base_job_sample.run_sample() + out, _ = capsys.readouterr() + expected = ('.*Job generated:.*\n' + '.*Job created:.*\n' + '.*Job existed:.*\n' + '.*Job updated:.*changedDescription.*\n' + '.*Job updated:.*changedJobTitle.*\n' + '.*Job deleted.*\n') + assert re.search(expected, out) diff --git a/jobs/api_client/batch_operation_sample.py b/jobs/api_client/batch_operation_sample.py new file mode 100755 index 00000000000..80395b4c3b5 --- /dev/null +++ b/jobs/api_client/batch_operation_sample.py @@ -0,0 +1,132 @@ +#!/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. + +# [START instantiate] +from googleapiclient.discovery import build + +client_service = build('jobs', 'v2') +# [END instantiate] + + +# [START batch_job_create] +def batch_job_create(client_service, company_name): + import base_job_sample + created_jobs = [] + + def job_create_callback(request_id, response, exception): + if exception is not None: + print('Got exception while creating job: %s' % exception) + pass + else: + print('Job created: %s' % response) + created_jobs.append(response) + pass + + batch = client_service.new_batch_http_request() + job_to_be_created1 = base_job_sample.generate_job_with_required_fields( + company_name) + request1 = {'job': job_to_be_created1} + batch.add( + client_service.jobs().create(body=request1), + callback=job_create_callback) + + job_to_be_created2 = base_job_sample.generate_job_with_required_fields( + company_name) + request2 = {'job': job_to_be_created2} + batch.add( + client_service.jobs().create(body=request2), + callback=job_create_callback) + batch.execute() + + return created_jobs +# [END batch_job_create] + + +# [START batch_job_update] +def batch_job_update(client_service, jobs_to_be_updated): + updated_jobs = [] + + def job_update_callback(request_id, response, exception): + if exception is not None: + print('Got exception while updating job: %s' % exception) + pass + else: + print('Job updated: %s' % response) + updated_jobs.append(response) + pass + + batch = client_service.new_batch_http_request() + for index in range(0, len(jobs_to_be_updated)): + job_to_be_updated = jobs_to_be_updated[index] + job_to_be_updated.update({'job_title': 'Engineer in Mountain View'}) + request = {'job': job_to_be_updated} + if index % 2 == 0: + batch.add( + client_service.jobs().patch( + name=job_to_be_updated.get('name'), body=request), + callback=job_update_callback) + else: + request.update({'update_job_fields': 'jobTitle'}) + batch.add( + client_service.jobs().patch( + name=job_to_be_updated.get('name'), body=request), + callback=job_update_callback) + + batch.execute() + + return updated_jobs +# [END batch_job_update] + + +# [START batch_job_delete] +def batch_job_delete(client_service, jobs_to_be_deleted): + + def job_delete_callback(request_id, response, exception): + if exception is not None: + print('Got exception while deleting job: %s' % exception) + pass + else: + print('Job deleted') + pass + + batch = client_service.new_batch_http_request() + for job_to_be_deleted in jobs_to_be_deleted: + batch.add( + client_service.jobs().delete(name=job_to_be_deleted.get('name')), + callback=job_delete_callback) + + batch.execute() +# [END batch_job_delete] + + +def run_sample(): + import base_company_sample + + # Create a company before creating jobs + 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') + + created_jobs = batch_job_create(client_service, company_name) + updated_jobs = batch_job_update(client_service, created_jobs) + batch_job_delete(client_service, updated_jobs) + + base_company_sample.delete_company(client_service, company_name) + + +if __name__ == '__main__': + run_sample() diff --git a/jobs/api_client/batch_operation_sample_test.py b/jobs/api_client/batch_operation_sample_test.py new file mode 100755 index 00000000000..7628c759cbe --- /dev/null +++ b/jobs/api_client/batch_operation_sample_test.py @@ -0,0 +1,27 @@ +# 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_batch_operation_sample(capsys): + import batch_operation_sample + import re + + batch_operation_sample.run_sample() + out, _ = capsys.readouterr() + expected = ( + '.*Company generated:.*Company created:.*.*Job created:.*Job ' + 'created:.*.*Job updated:.*Engineer in Mountain View.*Job ' + 'updated:.*Engineer in Mountain View.*.*Job deleted.*Job ' + 'deleted.*.*Company deleted.*') + assert re.search(expected, out, re.DOTALL) diff --git a/jobs/api_client/commute_search_sample.py b/jobs/api_client/commute_search_sample.py new file mode 100755 index 00000000000..bab127b95ba --- /dev/null +++ b/jobs/api_client/commute_search_sample.py @@ -0,0 +1,80 @@ +#!/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 commute_search] +def commute_search(client_service, company_name): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + start_location = {'latitude': 37.422408, 'longitude': -122.085609} + commute_preference = { + 'road_traffic': 'TRAFFIC_FREE', + 'method': 'TRANSIT', + 'travel_time': '1000s', + 'start_location': start_location + } + job_query = {'commute_filter': commute_preference} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'query': job_query, + 'request_metadata': request_metadata, + 'job_view': 'FULL', + 'enable_precise_result_size': True + } + response = client_service.jobs().search(body=request).execute() + print(response) +# [END commute_search] + + +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({ + 'locations': ['1600 Amphitheatre Pkwy, Mountain View, CA 94043'] + }) + job_name = base_job_sample.create_job(client_service, + job_to_be_created).get('name') + + # Wait several seconds for post processing + time.sleep(10) + commute_search(client_service, 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() diff --git a/jobs/api_client/commute_search_sample_test.py b/jobs/api_client/commute_search_sample_test.py new file mode 100755 index 00000000000..ee99a308b96 --- /dev/null +++ b/jobs/api_client/commute_search_sample_test.py @@ -0,0 +1,23 @@ +# 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_commute_search_sample(capsys): + import commute_search_sample + import re + + commute_search_sample.run_sample() + out, _ = capsys.readouterr() + expected = ('.*matchingJobs.*1600 Amphitheatre Pkwy.*') + assert re.search(expected, out) diff --git a/jobs/api_client/custom_attribute_sample.py b/jobs/api_client/custom_attribute_sample.py new file mode 100755 index 00000000000..817475225e6 --- /dev/null +++ b/jobs/api_client/custom_attribute_sample.py @@ -0,0 +1,156 @@ +#!/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 random +import string +import time + +# [START instantiate] +from googleapiclient.discovery import build + +client_service = build('jobs', 'v2') +# [END instantiate] + + +# [START custom_attribute_job] +def generate_job_with_custom_attributes(company_name): + # Requisition id should be a unique Id in your system. + requisition_id = 'job_with_custom_attributes:' + ''.join( + random.choice(string.ascii_uppercase + string.digits) + for _ in range(16)) + + job_title = 'Software Engineer' + application_urls = ['http://careers.google.com'] + description = ('Design, develop, test, deploy, maintain and improve ' + 'software.') + + custom_attributes = { + 'someFieldName1': { + 'string_values': { + 'values': ['value1'] + }, + 'filterable': True + }, + 'someFieldName2': { + 'long_value': 256, + 'filterable': True + } + } + + job = { + 'company_name': company_name, + 'requisition_id': requisition_id, + 'job_title': job_title, + 'application_urls': application_urls, + 'description': description, + 'custom_attributes': custom_attributes + } + print('Job generated: %s' % job) + return job +# [END custom_attribute_job] + + +# [START custom_attribute_filter_string_value] +def custom_attribute_filter_string_value(client_service): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + + custom_attribute_filter = 'NOT EMPTY(someFieldName1)' + job_query = {'custom_attribute_filter': custom_attribute_filter} + request = { + 'request_metadata': request_metadata, + 'query': job_query, + 'job_view': 'FULL' + } + + response = client_service.jobs().search(body=request).execute() + print(response) +# [END custom_attribute_filter_string_value] + + +# [START custom_attribute_filter_long_value] +def custom_attribute_filter_long_value(client_service): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + + custom_attribute_filter = ('(255 <= someFieldName2) AND' + ' (someFieldName2 <= 257)') + job_query = {'custom_attribute_filter': custom_attribute_filter} + request = { + 'request_metadata': request_metadata, + 'query': job_query, + 'job_view': 'FULL' + } + + response = client_service.jobs().search(body=request).execute() + print(response) +# [END custom_attribute_filter_long_value] + + +# [START custom_attribute_filter_multi_attributes] +def custom_attribute_filter_multi_attributes(client_service): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + + custom_attribute_filter = ( + '(someFieldName1 = "value1") AND ((255 <= someFieldName2) OR ' + '(someFieldName2 <= 213))') + job_query = {'custom_attribute_filter': custom_attribute_filter} + request = { + 'request_metadata': request_metadata, + 'query': job_query, + 'job_view': 'FULL' + } + + response = client_service.jobs().search(body=request).execute() + print(response) +# [END custom_attribute_filter_multi_attributes] + + +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 = generate_job_with_custom_attributes(company_name) + job_name = base_job_sample.create_job(client_service, + job_to_be_created).get('name') + + # Wait several seconds for post processing + time.sleep(10) + custom_attribute_filter_string_value(client_service) + custom_attribute_filter_long_value(client_service) + custom_attribute_filter_multi_attributes(client_service) + + base_job_sample.delete_job(client_service, job_name) + base_company_sample.delete_company(client_service, company_name) + + +if __name__ == '__main__': + run_sample() diff --git a/jobs/api_client/custom_attribute_sample_test.py b/jobs/api_client/custom_attribute_sample_test.py new file mode 100755 index 00000000000..286ae1dcca3 --- /dev/null +++ b/jobs/api_client/custom_attribute_sample_test.py @@ -0,0 +1,26 @@ +# 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_custom_attribute_sample(capsys): + import custom_attribute_sample + import re + + custom_attribute_sample.run_sample() + out, _ = capsys.readouterr() + expected = ('.*Job created:.*job_with_custom_attributes.*\n' + '.*matchingJobs.*job_with_custom_attributes.*\n' + '.*matchingJobs.*job_with_custom_attributes.*\n' + '.*matchingJobs.*job_with_custom_attributes.*\n') + assert re.search(expected, out, re.DOTALL) diff --git a/jobs/api_client/email_alert_search_sample.py b/jobs/api_client/email_alert_search_sample.py new file mode 100755 index 00000000000..342c28568bf --- /dev/null +++ b/jobs/api_client/email_alert_search_sample.py @@ -0,0 +1,67 @@ +#!/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 search_for_alerts] +def search_for_alerts(client_service, company_name): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + request = { + 'mode': 'JOB_SEARCH', + 'request_metadata': request_metadata, + } + if company_name is not None: + request.update({'query': {'company_names': [company_name]}}) + response = client_service.jobs().searchForAlert(body=request).execute() + print(response) +# [END search_for_alerts] + + +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_name = base_job_sample.create_job(client_service, + job_to_be_created).get('name') + + # Wait several seconds for post processing + time.sleep(10) + search_for_alerts(client_service, 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() diff --git a/jobs/api_client/email_alert_search_sample_test.py b/jobs/api_client/email_alert_search_sample_test.py new file mode 100755 index 00000000000..1f7e61b6317 --- /dev/null +++ b/jobs/api_client/email_alert_search_sample_test.py @@ -0,0 +1,23 @@ +# 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_email_alert_search_sample(capsys): + import email_alert_search_sample + import re + + email_alert_search_sample.run_sample() + out, _ = capsys.readouterr() + expected = ('.*matchingJobs.*') + assert re.search(expected, out) diff --git a/jobs/api_client/featured_job_search_sample.py b/jobs/api_client/featured_job_search_sample.py new file mode 100755 index 00000000000..979c578acff --- /dev/null +++ b/jobs/api_client/featured_job_search_sample.py @@ -0,0 +1,96 @@ +#!/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 random +import string +import time + +# [START instantiate] +from googleapiclient.discovery import build + +client_service = build('jobs', 'v2') +# [END instantiate] + + +# [START featured_job] +def generate_featured_job(company_name): + # Requisition id should be a unique Id in your system. + requisition_id = 'job_with_required_fields:' + ''.join( + random.choice(string.ascii_uppercase + string.digits) + for _ in range(16)) + + job_title = 'Software Engineer' + application_urls = ['http://careers.google.com'] + description = ('Design, develop, test, deploy, maintain and improve ' + 'software.') + + job = { + 'requisition_id': requisition_id, + 'job_title': job_title, + 'application_urls': application_urls, + 'description': description, + 'company_name': company_name, + 'promotion_value': 2 + } + print('Job generated: %s' % job) + return job +# [END featured_job] + + +# [START search_featured_job] +def search_featured_job(client_service, company_name): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + job_query = {'query': 'Software Engineer'} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'mode': 'FEATURED_JOB_SEARCH', + 'request_metadata': request_metadata, + 'query': job_query + } + + response = client_service.jobs().search(body=request).execute() + print(response) +# [END search_featured_job] + + +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 = generate_featured_job(company_name) + job_name = base_job_sample.create_job(client_service, + job_to_be_created).get('name') + + # Wait several seconds for post processing + time.sleep(10) + search_featured_job(client_service, 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() diff --git a/jobs/api_client/featured_job_search_sample_test.py b/jobs/api_client/featured_job_search_sample_test.py new file mode 100755 index 00000000000..3a76529940c --- /dev/null +++ b/jobs/api_client/featured_job_search_sample_test.py @@ -0,0 +1,23 @@ +# 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_featured_job_search_sample(capsys): + import featured_job_search_sample + import re + + featured_job_search_sample.run_sample() + out, _ = capsys.readouterr() + expected = ('.*matchingJobs.*') + assert re.search(expected, out) diff --git a/jobs/api_client/general_search_sample.py b/jobs/api_client/general_search_sample.py new file mode 100755 index 00000000000..a53c25084d4 --- /dev/null +++ b/jobs/api_client/general_search_sample.py @@ -0,0 +1,235 @@ +#!/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 basic_keyword_search] +def basic_keyword_search(client_service, company_name, keyword): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + job_query = {'query': keyword} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'mode': 'JOB_SEARCH', + 'request_metadata': request_metadata, + 'query': job_query, + } + + response = client_service.jobs().search(body=request).execute() + print(response) +# [END basic_keyword_search] + + +# [START category_filter] +def category_search(client_service, company_name, categories): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + job_query = {'categories': categories} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'mode': 'JOB_SEARCH', + 'request_metadata': request_metadata, + 'query': job_query, + } + + response = client_service.jobs().search(body=request).execute() + print(response) +# [END category_filter] + + +# [START employment_types_filter] +def employment_types_search(client_service, company_name, employment_types): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + job_query = {'employment_types': employment_types} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'mode': 'JOB_SEARCH', + 'request_metadata': request_metadata, + 'query': job_query, + } + + response = client_service.jobs().search(body=request).execute() + print(response) +# [END employment_types_filter] + + +# [START date_range_filter] +def date_range_search(client_service, company_name, date_range): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + job_query = {'publish_date_range': date_range} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'mode': 'JOB_SEARCH', + 'request_metadata': request_metadata, + 'query': job_query, + } + + response = client_service.jobs().search(body=request).execute() + print(response) +# [END date_range_filter] + + +# [START language_code_filter] +def language_code_search(client_service, company_name, language_codes): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + job_query = {'language_codes': language_codes} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'mode': 'JOB_SEARCH', + 'request_metadata': request_metadata, + 'query': job_query, + } + + response = client_service.jobs().search(body=request).execute() + print(response) +# [END language_code_filter] + + +# [START company_display_name_filter] +def company_display_name_search(client_service, company_name, + company_display_names): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + job_query = {'company_display_names': company_display_names} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'mode': 'JOB_SEARCH', + 'request_metadata': request_metadata, + 'query': job_query, + } + + response = client_service.jobs().search(body=request).execute() + print(response) +# [END company_display_name_filter] + + +# [START compensation_filter] +def compensation_search(client_service, company_name): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + compensation_range = { + 'max': { + 'currency_code': 'USD', + 'units': 15 + }, + 'min': { + 'currency_code': 'USD', + 'units': 10, + 'nanos': 500000000 + } + } + compensation_filter = { + 'type': 'UNIT_AND_AMOUNT', + 'units': ['HOURLY'], + 'range': compensation_range + } + job_query = {'compensation_filter': compensation_filter} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'mode': 'JOB_SEARCH', + 'request_metadata': request_metadata, + 'query': job_query, + } + + response = client_service.jobs().search(body=request).execute() + print(response) +# [END compensation_filter] + + +def run_sample(): + import base_company_sample + import base_job_sample + + company_to_be_created = base_company_sample.generate_company() + company_to_be_created.update({'display_name': 'Google'}) + 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) + amount = {'currency_code': 'USD', 'units': 12} + compensation_info = { + 'entries': [{ + 'type': 'BASE', + 'unit': 'HOURLY', + 'amount': amount + }] + } + job_to_be_created.update({ + 'job_title': 'Systems Administrator', + 'employment_types': 'FULL_TIME', + 'language_code': 'en-US', + 'compensation_info': compensation_info + }) + job_name = base_job_sample.create_job(client_service, + job_to_be_created).get('name') + + # Wait several seconds for post processing + time.sleep(10) + basic_keyword_search(client_service, company_name, 'Systems Administrator') + category_search(client_service, company_name, ['COMPUTER_AND_IT']) + date_range_search(client_service, company_name, 'PAST_24_HOURS') + employment_types_search(client_service, company_name, + ['FULL_TIME', 'CONTRACTOR', 'PER_DIEM']) + company_display_name_search(client_service, company_name, ['Google']) + compensation_search(client_service, company_name) + language_code_search(client_service, company_name, ['pt-BR', 'en-US']) + + base_job_sample.delete_job(client_service, job_name) + base_company_sample.delete_company(client_service, company_name) + + +if __name__ == '__main__': + run_sample() diff --git a/jobs/api_client/general_search_sample_test.py b/jobs/api_client/general_search_sample_test.py new file mode 100755 index 00000000000..7f1f53f325b --- /dev/null +++ b/jobs/api_client/general_search_sample_test.py @@ -0,0 +1,29 @@ +# 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_general_search_sample(capsys): + import general_search_sample + import re + + general_search_sample.run_sample() + out, _ = capsys.readouterr() + expected = ('.*matchingJobs.*\n' + '.*matchingJobs.*\n' + '.*matchingJobs.*\n' + '.*matchingJobs.*\n' + '.*matchingJobs.*\n' + '.*matchingJobs.*\n' + '.*matchingJobs.*\n') + assert re.search(expected, out, re.DOTALL) diff --git a/jobs/api_client/histogram_sample.py b/jobs/api_client/histogram_sample.py new file mode 100755 index 00000000000..06d942d0e4e --- /dev/null +++ b/jobs/api_client/histogram_sample.py @@ -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 histogram_search] +def histogram_search(client_service, company_name): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + custom_attribute_histogram_facet = { + 'key': 'someFieldName1', + 'string_value_histogram': True + } + histogram_facets = { + 'simple_histogram_facets': ['COMPANY_ID'], + 'custom_attribute_histogram_facets': [custom_attribute_histogram_facet] + } + request = { + 'mode': 'JOB_SEARCH', + 'request_metadata': request_metadata, + 'histogram_facets': histogram_facets + } + if company_name is not None: + request.update({'query': {'company_names': [company_name]}}) + response = client_service.jobs().search(body=request).execute() + print(response) +# [END histogram_search] + + +def run_sample(): + import base_company_sample + import base_job_sample + import custom_attribute_sample as caa + + 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 = caa.generate_job_with_custom_attributes(company_name) + job_name = base_job_sample.create_job(client_service, + job_to_be_created).get('name') + + # Wait several seconds for post processing + time.sleep(10) + histogram_search(client_service, 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() diff --git a/jobs/api_client/histogram_sample_test.py b/jobs/api_client/histogram_sample_test.py new file mode 100755 index 00000000000..11066ec15ae --- /dev/null +++ b/jobs/api_client/histogram_sample_test.py @@ -0,0 +1,23 @@ +# 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_histogram_sample(capsys): + import histogram_sample + import re + + histogram_sample.run_sample() + out, _ = capsys.readouterr() + assert re.search('COMPANY_ID', out) + assert re.search('someFieldName1', out) diff --git a/jobs/api_client/location_search_sample.py b/jobs/api_client/location_search_sample.py new file mode 100755 index 00000000000..a3ad3cb495c --- /dev/null +++ b/jobs/api_client/location_search_sample.py @@ -0,0 +1,177 @@ +#!/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 basic_location_search] +def basic_location_search(client_service, company_name, location, distance): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + location_filter = {'name': location, 'distance_in_miles': distance} + job_query = {'location_filters': [location_filter]} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'query': job_query, + 'request_metadata': request_metadata, + 'mode': 'JOB_SEARCH', + } + response = client_service.jobs().search(body=request).execute() + print(response) +# [END basic_location_search] + + +# [START keyword_location_search] +def keyword_location_search(client_service, company_name, location, distance, + keyword): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + location_filter = {'name': location, 'distance_in_miles': distance} + job_query = {'location_filters': [location_filter], 'query': keyword} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'query': job_query, + 'request_metadata': request_metadata, + 'mode': 'JOB_SEARCH', + } + response = client_service.jobs().search(body=request).execute() + print(response) +# [END keyword_location_search] + + +# [START city_location_search] +def city_location_search(client_service, company_name, location): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + location_filter = {'name': location} + job_query = {'location_filters': [location_filter]} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'query': job_query, + 'request_metadata': request_metadata, + 'mode': 'JOB_SEARCH', + } + response = client_service.jobs().search(body=request).execute() + print(response) +# [END city_location_search] + + +# [START multi_locations_search] +def multi_locations_search(client_service, company_name, location1, distance1, + location2): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + location_filter1 = {'name': location1, 'distance_in_miles': distance1} + location_filter2 = {'name': location2} + job_query = {'location_filters': [location_filter1, location_filter2]} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'query': job_query, + 'request_metadata': request_metadata, + 'mode': 'JOB_SEARCH', + } + response = client_service.jobs().search(body=request).execute() + print(response) +# [END multi_locations_search] + + +# [START broadening_location_search] +def broadening_location_search(client_service, company_name, location): + request_metadata = { + 'user_id': 'HashedUserId', + 'session_id': 'HashedSessionId', + 'domain': 'www.google.com' + } + location_filter = {'name': location} + job_query = {'location_filters': [location_filter]} + if company_name is not None: + job_query.update({'company_names': [company_name]}) + request = { + 'query': job_query, + 'request_metadata': request_metadata, + 'mode': 'JOB_SEARCH', + 'enable_broadening': True + } + response = client_service.jobs().search(body=request).execute() + print(response) +# [END broadening_location_search] + + +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') + + location = 'Mountain View, CA' + distance = 0.5 + keyword = 'Software Engineer' + location2 = 'Synnyvale, CA' + + job_to_be_created = base_job_sample.generate_job_with_required_fields( + company_name) + job_to_be_created.update({'locations': [location], 'job_title': keyword}) + job_name = base_job_sample.create_job(client_service, + job_to_be_created).get('name') + + job_to_be_created2 = base_job_sample.generate_job_with_required_fields( + company_name) + job_to_be_created2.update({'locations': [location2], 'job_title': keyword}) + job_name2 = base_job_sample.create_job(client_service, + job_to_be_created2).get('name') + + # Wait several seconds for post processing + time.sleep(10) + basic_location_search(client_service, company_name, location, distance) + city_location_search(client_service, company_name, location) + broadening_location_search(client_service, company_name, location) + keyword_location_search(client_service, company_name, location, distance, + keyword) + multi_locations_search(client_service, company_name, location, distance, + location2) + + base_job_sample.delete_job(client_service, job_name) + base_job_sample.delete_job(client_service, job_name2) + base_company_sample.delete_company(client_service, company_name) + + +if __name__ == '__main__': + run_sample() diff --git a/jobs/api_client/location_search_sample_test.py b/jobs/api_client/location_search_sample_test.py new file mode 100755 index 00000000000..8971a3c23a9 --- /dev/null +++ b/jobs/api_client/location_search_sample_test.py @@ -0,0 +1,33 @@ +# 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_location_search_sample(capsys): + import location_search_sample + import re + + location_search_sample.run_sample() + out, _ = capsys.readouterr() + expected = ('.*appliedJobLocationFilters.*\n' + '.*appliedJobLocationFilters.*\n' + '.*appliedJobLocationFilters.*\n' + '.*appliedJobLocationFilters.*\n' + '.*appliedJobLocationFilters.*\n') + assert re.search(expected, out, re.DOTALL) + expected = ('.*matchingJobs.*\n' + '.*matchingJobs.*\n' + '.*matchingJobs.*\n' + '.*matchingJobs.*\n' + '.*matchingJobs.*\n') + assert re.search(expected, out, re.DOTALL) diff --git a/jobs/api_client/quickstart.py b/jobs/api_client/quickstart.py new file mode 100755 index 00000000000..d91098f059d --- /dev/null +++ b/jobs/api_client/quickstart.py @@ -0,0 +1,41 @@ +#!/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. + +# [START quickstart] +from googleapiclient.discovery import build +from googleapiclient.errors import Error + +client_service = build('jobs', 'v2') + + +def run_sample(): + try: + list_companies_response = client_service.companies().list().execute() + print('Request Id: %s' % + list_companies_response.get('metadata').get('requestId')) + print('Companies:') + for company in list_companies_response.get('companies'): + print('%s' % company.get('name')) + print('') + + except Error as e: + print('Got exception while listing companies') + raise e + + +if __name__ == '__main__': + run_sample() +# [END quickstart] diff --git a/jobs/api_client/quickstart_test.py b/jobs/api_client/quickstart_test.py new file mode 100644 index 00000000000..e4b47fa3b20 --- /dev/null +++ b/jobs/api_client/quickstart_test.py @@ -0,0 +1,22 @@ +# 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_quickstart(capsys): + import quickstart + + quickstart.run_sample() + out, _ = capsys.readouterr() + expected = 'Request Id:' + assert expected in out diff --git a/jobs/api_client/requirements.txt b/jobs/api_client/requirements.txt new file mode 100755 index 00000000000..500e732f532 --- /dev/null +++ b/jobs/api_client/requirements.txt @@ -0,0 +1,3 @@ +google-api-python-client==1.6.5 +google-auth==1.4.1 +google-auth-httplib2==0.0.3