Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions jobs/api_client/README.rst
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/
42 changes: 42 additions & 0 deletions jobs/api_client/README.rst.in
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
76 changes: 76 additions & 0 deletions jobs/api_client/auto_complete_sample.py
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

@michaelawyu michaelawyu Jul 17, 2018

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

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()
30 changes: 30 additions & 0 deletions jobs/api_client/auto_complete_sample_test.py
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)
142 changes: 142 additions & 0 deletions jobs/api_client/base_company_sample.py
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()
Loading