Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Merge branch 'add-snippets-samples'
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 committed Sep 25, 2020
2 parents 75717c9 + 57dea03 commit d2572da
Show file tree
Hide file tree
Showing 47 changed files with 2,152 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .kokoro/samples/python3.6/common.cfg
Expand Up @@ -13,6 +13,12 @@ env_vars: {
value: "py-3.6"
}

# Declare build specific Cloud project.
env_vars: {
key: "BUILD_SPECIFIC_GCLOUD_PROJECT"
value: "python-docs-samples-tests-py36"
}

env_vars: {
key: "TRAMPOLINE_BUILD_FILE"
value: "github/python-talent/.kokoro/test-samples.sh"
Expand Down
6 changes: 6 additions & 0 deletions .kokoro/samples/python3.7/common.cfg
Expand Up @@ -13,6 +13,12 @@ env_vars: {
value: "py-3.7"
}

# Declare build specific Cloud project.
env_vars: {
key: "BUILD_SPECIFIC_GCLOUD_PROJECT"
value: "python-docs-samples-tests-py37"
}

env_vars: {
key: "TRAMPOLINE_BUILD_FILE"
value: "github/python-talent/.kokoro/test-samples.sh"
Expand Down
6 changes: 6 additions & 0 deletions .kokoro/samples/python3.8/common.cfg
Expand Up @@ -24,6 +24,12 @@ env_vars: {
value: "gcr.io/cloud-devrel-kokoro-resources/python-samples-testing-docker"
}

# Declare build specific Cloud project.
env_vars: {
key: "BUILD_SPECIFIC_GCLOUD_PROJECT"
value: "python-docs-samples-tests-py38"
}

# Download secrets for samples
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/python-docs-samples"

Expand Down
6 changes: 6 additions & 0 deletions samples/snippets/README.rst
@@ -0,0 +1,6 @@
To run the sample, you need to enable the API at: https://console.cloud.google.com/apis/library/jobs.googleapis.com

To run the sample, you need to have the following roles:

* `Talent Solution Job Editor`
* `Talent Solution Profile Editor`
87 changes: 87 additions & 0 deletions samples/snippets/conftest.py
@@ -0,0 +1,87 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# 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 os
import uuid

from google.api_core.exceptions import NotFound

import pytest

import job_search_create_company
import job_search_create_job
import job_search_create_tenant
import job_search_delete_company
import job_search_delete_job
import job_search_delete_tenant

PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]


@pytest.fixture(scope="module")
def tenant():
tenant_ext_unique_id = "TEST_TENANT_{}".format(uuid.uuid4())
# create a temporary tenant
tenant_name = job_search_create_tenant.create_tenant(
PROJECT_ID, tenant_ext_unique_id
)

# extract company id
tenant_id = tenant_name.split("/")[-1]

yield tenant_id

try:
job_search_delete_tenant.delete_tenant(PROJECT_ID, tenant_id)
except NotFound as e:
print("Ignoring NotFound upon cleanup, details: {}".format(e))


@pytest.fixture(scope="module")
def company(tenant):
company_ext_id = "COMPANY_EXT_ID_{}".format(uuid.uuid4())

# create a temporary company
company_name = job_search_create_company.create_company(
PROJECT_ID, tenant, "Test Company Name", company_ext_id
)

# extract company id
company_id = company_name.split("/")[-1]

yield company_id

try:
job_search_delete_company.delete_company(PROJECT_ID, tenant, company_id)
except NotFound as e:
print("Ignoring NotFound upon cleanup, details: {}".format(e))


@pytest.fixture(scope="module")
def job(tenant, company):
post_unique_id = "TEST_POST_{}".format(uuid.uuid4().hex)[:20]
# create a temporary job
job_name = job_search_create_job.create_job(
PROJECT_ID, tenant, company, post_unique_id, "www.jobUrl.com"
)

# extract company id
job_id = job_name.split("/")[-1]

yield job_id

try:
job_search_delete_job.delete_job(PROJECT_ID, tenant, job_id)
except NotFound as e:
print("Ignoring NotFound upon cleanup, details: {}".format(e))
54 changes: 54 additions & 0 deletions samples/snippets/job_search_autocomplete_job_title.py
@@ -0,0 +1,54 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START job_search_autocomplete_job_title]

from google.cloud import talent_v4beta1
import six


def complete_query(project_id, tenant_id, query):
"""Complete job title given partial text (autocomplete)"""

client = talent_v4beta1.CompletionClient()

# project_id = 'Your Google Cloud Project ID'
# tenant_id = 'Your Tenant ID (using tenancy is optional)'
# query = '[partially typed job title]'

if isinstance(project_id, six.binary_type):
project_id = project_id.decode("utf-8")
if isinstance(tenant_id, six.binary_type):
tenant_id = tenant_id.decode("utf-8")
if isinstance(query, six.binary_type):
query = query.decode("utf-8")

parent = f"projects/{project_id}/tenants/{tenant_id}"

request = talent_v4beta1.CompleteQueryRequest(
parent=parent,
query=query,
page_size=5, # limit for number of results
language_codes=["en-US"], # language code
)
response = client.complete_query(request=request)
for result in response.completion_results:
print(f"Suggested title: {result.suggestion}")
# Suggestion type is JOB_TITLE or COMPANY_TITLE
print(
f"Suggestion type: {talent_v4beta1.CompleteQueryRequest.CompletionType(result.type).name}"
)


# [END job_search_autocomplete_job_title]
25 changes: 25 additions & 0 deletions samples/snippets/job_search_autocomplete_job_title_test.py
@@ -0,0 +1,25 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# 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 os

import job_search_autocomplete_job_title

PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]


def test_autocomplete_job_title(capsys, tenant):
job_search_autocomplete_job_title.complete_query(PROJECT_ID, tenant, "Software")
out, _ = capsys.readouterr()
assert "Suggested title:" in out
133 changes: 133 additions & 0 deletions samples/snippets/job_search_batch_create_jobs.py
@@ -0,0 +1,133 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START job_search_batch_create_jobs]

from google.cloud import talent
import six


def batch_create_jobs(
project_id,
tenant_id,
company_name_one,
requisition_id_one,
title_one,
description_one,
job_application_url_one,
address_one,
language_code_one,
company_name_two,
requisition_id_two,
title_two,
description_two,
job_application_url_two,
address_two,
language_code_two,
):
"""
Batch Create Jobs
Args:
project_id Your Google Cloud Project ID
tenant_id Identifier of the Tenant
"""

client = talent.JobServiceClient()

# project_id = 'Your Google Cloud Project ID'
# tenant_id = 'Your Tenant ID (using tenancy is optional)'
# company_name_one = 'Company name, e.g. projects/your-project/companies/company-id'
# requisition_id_one = 'Job requisition ID, aka Posting ID. Unique per job.'
# title_one = 'Software Engineer'
# description_one = 'This is a description of this <i>wonderful</i> job!'
# job_application_url_one = 'https://www.example.org/job-posting/123'
# address_one = '1600 Amphitheatre Parkway, Mountain View, CA 94043'
# language_code_one = 'en-US'
# company_name_two = 'Company name, e.g. projects/your-project/companies/company-id'
# requisition_id_two = 'Job requisition ID, aka Posting ID. Unique per job.'
# title_two = 'Quality Assurance'
# description_two = 'This is a description of this <i>wonderful</i> job!'
# job_application_url_two = 'https://www.example.org/job-posting/123'
# address_two = '111 8th Avenue, New York, NY 10011'
# language_code_two = 'en-US'

if isinstance(project_id, six.binary_type):
project_id = project_id.decode("utf-8")
if isinstance(tenant_id, six.binary_type):
tenant_id = tenant_id.decode("utf-8")
if isinstance(company_name_one, six.binary_type):
company_name_one = company_name_one.decode("utf-8")
if isinstance(requisition_id_one, six.binary_type):
requisition_id_one = requisition_id_one.decode("utf-8")
if isinstance(title_one, six.binary_type):
title_one = title_one.decode("utf-8")
if isinstance(description_one, six.binary_type):
description_one = description_one.decode("utf-8")
if isinstance(job_application_url_one, six.binary_type):
job_application_url_one = job_application_url_one.decode("utf-8")
if isinstance(address_one, six.binary_type):
address_one = address_one.decode("utf-8")
if isinstance(language_code_one, six.binary_type):
language_code_one = language_code_one.decode("utf-8")
if isinstance(company_name_two, six.binary_type):
company_name_two = company_name_two.decode("utf-8")
if isinstance(requisition_id_two, six.binary_type):
requisition_id_two = requisition_id_two.decode("utf-8")
if isinstance(title_two, six.binary_type):
title_two = title_two.decode("utf-8")
if isinstance(description_two, six.binary_type):
description_two = description_two.decode("utf-8")
if isinstance(job_application_url_two, six.binary_type):
job_application_url_two = job_application_url_two.decode("utf-8")
if isinstance(address_two, six.binary_type):
address_two = address_two.decode("utf-8")
if isinstance(language_code_two, six.binary_type):
language_code_two = language_code_two.decode("utf-8")
parent = f"projects/{project_id}/tenants/{tenant_id}"
uris = [job_application_url_one]
application_info = {"uris": uris}
addresses = [address_one]
jobs_element = {
"company": company_name_one,
"requisition_id": requisition_id_one,
"title": title_one,
"description": description_one,
"application_info": application_info,
"addresses": addresses,
"language_code": language_code_one,
}
uris_2 = [job_application_url_two]
application_info_2 = {"uris": uris_2}
addresses_2 = [address_two]
jobs_element_2 = {
"company": company_name_two,
"requisition_id": requisition_id_two,
"title": title_two,
"description": description_two,
"application_info": application_info_2,
"addresses": addresses_2,
"language_code": language_code_two,
}
jobs = [jobs_element, jobs_element_2]

operation = client.batch_create_jobs(parent=parent, jobs=jobs)

print("Waiting for operation to complete...")
response = operation.result(90)

print("Batch response: {}".format(response))


# [END job_search_batch_create_jobs]

0 comments on commit d2572da

Please sign in to comment.