|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | | -# Copyright 2017 Google Inc. |
| 3 | +# Copyright 2018 Google Inc. |
4 | 4 | # |
5 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
6 | 6 | # you may not use this file except in compliance with the License. |
|
21 | 21 | """ |
22 | 22 |
|
23 | 23 | import argparse |
24 | | -import base64 |
25 | 24 |
|
26 | 25 |
|
27 | | -# [START cloud_tasks_create_task] |
28 | 26 | def create_task(project, queue, location): |
| 27 | + # [START tasks_create_task] |
29 | 28 | """Create a task for a given queue with an arbitrary payload.""" |
30 | 29 |
|
31 | | - import googleapiclient.discovery |
| 30 | + from google.cloud import tasks_v2beta2 |
32 | 31 |
|
33 | 32 | # Create a client. |
34 | | - client = googleapiclient.discovery.build('cloudtasks', 'v2beta2') |
| 33 | + client = tasks_v2beta2.CloudTasksClient() |
35 | 34 |
|
36 | | - # Prepare the payload. |
37 | | - payload = 'a message for the recipient' |
38 | | - |
39 | | - # The API expects base64 encoding of the payload, so encode the unicode |
40 | | - # `payload` object into a byte string and base64 encode it. |
41 | | - base64_encoded_payload = base64.b64encode(payload.encode()) |
42 | | - |
43 | | - # The request body object will be emitted in JSON, which requires |
44 | | - # unicode objects, so convert the byte string to unicode (still base64). |
45 | | - converted_payload = base64_encoded_payload.decode() |
| 35 | + # Prepare the payload of type bytes. |
| 36 | + payload = 'a message for the recipient'.encode() |
46 | 37 |
|
47 | 38 | # Construct the request body. |
48 | 39 | task = { |
49 | | - 'task': { |
50 | | - 'pullMessage': { |
51 | | - 'payload': converted_payload |
52 | | - } |
| 40 | + 'pull_message': { |
| 41 | + 'payload': payload, |
53 | 42 | } |
54 | 43 | } |
55 | 44 |
|
56 | 45 | # Construct the fully qualified queue name. |
57 | | - queue_name = 'projects/{}/locations/{}/queues/{}'.format( |
58 | | - project, location, queue) |
| 46 | + parent = client.queue_path(project, location, queue) |
59 | 47 |
|
60 | 48 | # Use the client to build and send the task. |
61 | | - response = client.projects().locations().queues().tasks().create( |
62 | | - parent=queue_name, body=task).execute() |
| 49 | + response = client.create_task(parent, task) |
63 | 50 |
|
64 | | - print('Created task {}'.format(response['name'])) |
| 51 | + print('Created task: {}'.format(response.name)) |
65 | 52 | return response |
66 | | -# [END cloud_tasks_create_task] |
| 53 | + # [END tasks_create_task] |
67 | 54 |
|
68 | 55 |
|
69 | | -# [START cloud_tasks_lease_and_acknowledge_task] |
70 | 56 | def lease_task(project, queue, location): |
| 57 | + # [START tasks_lease_and_acknowledge_task] |
71 | 58 | """Lease a single task from a given queue for 10 minutes.""" |
72 | 59 |
|
73 | | - import googleapiclient.discovery |
| 60 | + from google.cloud import tasks_v2beta2 |
74 | 61 |
|
75 | 62 | # Create a client. |
76 | | - client = googleapiclient.discovery.build('cloudtasks', 'v2beta2') |
| 63 | + client = tasks_v2beta2.CloudTasksClient() |
77 | 64 |
|
78 | | - duration_seconds = '600s' |
79 | | - lease_options = { |
80 | | - 'maxTasks': 1, |
81 | | - 'leaseDuration': duration_seconds, |
82 | | - 'responseView': 'FULL' |
83 | | - } |
| 65 | + # Construct the fully qualified queue name. |
| 66 | + parent = client.queue_path(project, location, queue) |
| 67 | + |
| 68 | + lease_duration = {'seconds': 600} |
84 | 69 |
|
85 | | - queue_name = 'projects/{}/locations/{}/queues/{}'.format( |
86 | | - project, location, queue) |
| 70 | + # Send lease request to client. |
| 71 | + response = client.lease_tasks( |
| 72 | + parent, lease_duration, max_tasks=1, response_view='FULL') |
87 | 73 |
|
88 | | - response = client.projects().locations().queues().tasks().lease( |
89 | | - parent=queue_name, body=lease_options).execute() |
| 74 | + task = response.tasks[0] |
90 | 75 |
|
91 | | - print('Leased task {}'.format(response)) |
92 | | - return response['tasks'][0] |
| 76 | + print('Leased task {}'.format(task.name)) |
| 77 | + return task |
93 | 78 |
|
94 | 79 |
|
95 | 80 | def acknowledge_task(task): |
96 | 81 | """Acknowledge a given task.""" |
97 | 82 |
|
98 | | - import googleapiclient.discovery |
| 83 | + from google.cloud import tasks_v2beta2 |
99 | 84 |
|
100 | 85 | # Create a client. |
101 | | - client = googleapiclient.discovery.build('cloudtasks', 'v2beta2') |
| 86 | + client = tasks_v2beta2.CloudTasksClient() |
102 | 87 |
|
103 | | - body = {'scheduleTime': task['scheduleTime']} |
104 | | - client.projects().locations().queues().tasks().acknowledge( |
105 | | - name=task['name'], body=body).execute() |
| 88 | + # Send request to client to acknowledge task. |
| 89 | + client.acknowledge_task(task.name, task.schedule_time) |
106 | 90 |
|
107 | | - print('Acknowledged task {}'.format(task['name'])) |
108 | | - # [END cloud_tasks_lease_and_acknowledge_task] |
| 91 | + print('Acknowledged task {}'.format(task.name)) |
| 92 | + # [END tasks_lease_and_acknowledge_task] |
109 | 93 |
|
110 | 94 |
|
111 | 95 | if __name__ == '__main__': |
|
0 commit comments