|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2018 Google LLC. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import random |
| 18 | +import string |
| 19 | + |
| 20 | +# [START jobs_instantiate] |
| 21 | +from googleapiclient.discovery import build |
| 22 | +from googleapiclient.errors import Error |
| 23 | + |
| 24 | +client_service = build('jobs', 'v2') |
| 25 | +# [END jobs_instantiate] |
| 26 | + |
| 27 | + |
| 28 | +# [START jobs_basic_company] |
| 29 | +def generate_company(): |
| 30 | + # distributor company id should be a unique Id in your system. |
| 31 | + distributor_company_id = 'company:' + ''.join( |
| 32 | + random.choice(string.ascii_uppercase + string.digits) |
| 33 | + for _ in range(16)) |
| 34 | + |
| 35 | + display_name = 'Google' |
| 36 | + hq_location = '1600 Amphitheatre Parkway Mountain View, CA 94043' |
| 37 | + |
| 38 | + company = { |
| 39 | + 'display_name': display_name, |
| 40 | + 'distributor_company_id': distributor_company_id, |
| 41 | + 'hq_location': hq_location |
| 42 | + } |
| 43 | + print('Company generated: %s' % company) |
| 44 | + return company |
| 45 | +# [END jobs_basic_company] |
| 46 | + |
| 47 | + |
| 48 | +# [START jobs_create_company] |
| 49 | +def create_company(client_service, company_to_be_created): |
| 50 | + try: |
| 51 | + company_created = client_service.companies().create( |
| 52 | + body=company_to_be_created).execute() |
| 53 | + print('Company created: %s' % company_created) |
| 54 | + return company_created |
| 55 | + except Error as e: |
| 56 | + print('Got exception while creating company') |
| 57 | + raise e |
| 58 | +# [END jobs_create_company] |
| 59 | + |
| 60 | + |
| 61 | +# [START jobs_get_company] |
| 62 | +def get_company(client_service, company_name): |
| 63 | + try: |
| 64 | + company_existed = client_service.companies().get( |
| 65 | + name=company_name).execute() |
| 66 | + print('Company existed: %s' % company_existed) |
| 67 | + return company_existed |
| 68 | + except Error as e: |
| 69 | + print('Got exception while getting company') |
| 70 | + raise e |
| 71 | +# [END jobs_get_company] |
| 72 | + |
| 73 | + |
| 74 | +# [START jobs_update_company] |
| 75 | +def update_company(client_service, company_name, company_to_be_updated): |
| 76 | + try: |
| 77 | + company_updated = client_service.companies().patch( |
| 78 | + name=company_name, body=company_to_be_updated).execute() |
| 79 | + print('Company updated: %s' % company_updated) |
| 80 | + return company_updated |
| 81 | + except Error as e: |
| 82 | + print('Got exception while updating company') |
| 83 | + raise e |
| 84 | +# [END jobs_update_company] |
| 85 | + |
| 86 | + |
| 87 | +# [START jobs_update_company_with_field_mask] |
| 88 | +def update_company_with_field_mask(client_service, company_name, |
| 89 | + company_to_be_updated, field_mask): |
| 90 | + try: |
| 91 | + company_updated = client_service.companies().patch( |
| 92 | + name=company_name, |
| 93 | + body=company_to_be_updated, |
| 94 | + updateCompanyFields=field_mask).execute() |
| 95 | + print('Company updated: %s' % company_updated) |
| 96 | + return company_updated |
| 97 | + except Error as e: |
| 98 | + print('Got exception while updating company with field mask') |
| 99 | + raise e |
| 100 | +# [END jobs_update_company_with_field_mask] |
| 101 | + |
| 102 | + |
| 103 | +# [START jobs_delete_company] |
| 104 | +def delete_company(client_service, company_name): |
| 105 | + try: |
| 106 | + client_service.companies().delete(name=company_name).execute() |
| 107 | + print('Company deleted') |
| 108 | + except Error as e: |
| 109 | + print('Got exception while deleting company') |
| 110 | + raise e |
| 111 | +# [END jobs_delete_company] |
| 112 | + |
| 113 | + |
| 114 | +def run_sample(): |
| 115 | + # Construct a company |
| 116 | + company_to_be_created = generate_company() |
| 117 | + |
| 118 | + # Create a company |
| 119 | + company_created = create_company(client_service, company_to_be_created) |
| 120 | + |
| 121 | + # Get a company |
| 122 | + company_name = company_created.get('name') |
| 123 | + get_company(client_service, company_name) |
| 124 | + |
| 125 | + # Update a company |
| 126 | + company_to_be_updated = company_created |
| 127 | + company_to_be_updated.update({'website': 'https://elgoog.im/'}) |
| 128 | + update_company(client_service, company_name, company_to_be_updated) |
| 129 | + |
| 130 | + # Update a company with field mask |
| 131 | + update_company_with_field_mask( |
| 132 | + client_service, company_name, { |
| 133 | + 'displayName': 'changedTitle', |
| 134 | + 'distributorCompanyId': company_created.get('distributorCompanyId') |
| 135 | + }, 'displayName') |
| 136 | + |
| 137 | + # Delete a company |
| 138 | + delete_company(client_service, company_name) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + run_sample() |
0 commit comments