|
1 | | -# Copyright 2017 Google Inc. All Rights Reserved. |
| 1 | +# Copyright 2018 Google Inc. All Rights Reserved. |
2 | 2 | # |
3 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | # you may not use this file except in compliance with the License. |
|
15 | 15 | from __future__ import print_function |
16 | 16 |
|
17 | 17 | import argparse |
18 | | -import base64 |
19 | 18 | import datetime |
20 | | -import json |
21 | 19 |
|
22 | 20 |
|
23 | 21 | # [START cloud_tasks_appengine_create_task] |
24 | 22 | def create_task(project, queue, location, payload=None, in_seconds=None): |
25 | 23 | """Create a task for a given queue with an arbitrary payload.""" |
26 | 24 |
|
27 | | - import googleapiclient.discovery |
| 25 | + from google.cloud import tasks_v2beta2 |
| 26 | + from google.protobuf import timestamp_pb2 |
28 | 27 |
|
29 | 28 | # Create a client. |
30 | | - client = googleapiclient.discovery.build('cloudtasks', 'v2beta2') |
| 29 | + client = tasks_v2beta2.CloudTasksClient() |
31 | 30 |
|
32 | 31 | # Construct the request body. |
33 | | - url = '/example_task_handler' |
34 | | - body = { |
35 | | - 'task': { |
36 | | - 'appEngineHttpRequest': { # Specify the type of request. |
37 | | - 'httpMethod': 'POST', |
38 | | - 'relativeUrl': url |
| 32 | + task = { |
| 33 | + 'app_engine_http_request': { # Specify the type of request. |
| 34 | + 'http_method': 'POST', |
| 35 | + 'relative_url': '/example_task_handler' |
39 | 36 | } |
40 | | - } |
41 | 37 | } |
42 | | - |
43 | 38 | if payload is not None: |
44 | | - # The API expects base64 encoding of the payload, so encode the unicode |
45 | | - # `payload` object into a byte string and base64 encode it. |
46 | | - base64_encoded_payload = base64.b64encode(payload.encode()) |
47 | | - |
48 | | - # The request body object will be emitted in JSON, which requires |
49 | | - # unicode objects, so convert the byte string to unicode, still base64. |
50 | | - converted_payload = base64_encoded_payload.decode() |
| 39 | + # The API expects a payload of type bytes. |
| 40 | + converted_payload = payload.encode() |
51 | 41 |
|
52 | 42 | # Add the payload to the request. |
53 | | - body['task']['appEngineHttpRequest']['payload'] = converted_payload |
| 43 | + task['app_engine_http_request']['payload'] = converted_payload |
54 | 44 |
|
55 | 45 | if in_seconds is not None: |
56 | 46 | # Convert "seconds from now" into an rfc3339 datetime string. |
57 | 47 | d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds) |
58 | | - scheduled_time = d.isoformat('T') + 'Z' |
| 48 | + |
| 49 | + # Create Timestamp protobuf. |
| 50 | + timestamp = timestamp_pb2.Timestamp() |
| 51 | + timestamp.FromDatetime(d) |
59 | 52 |
|
60 | 53 | # Add the rfc3339 datetime string to the request. |
61 | | - body['task']['scheduleTime'] = scheduled_time |
| 54 | + task['schedule_time'] = timestamp |
62 | 55 |
|
63 | 56 | # Construct the fully qualified queue name. |
64 | | - queue_name = 'projects/{}/locations/{}/queues/{}'.format( |
65 | | - project, location, queue) |
66 | | - |
67 | | - print('Sending task {}'.format(json.dumps(body))) |
| 57 | + parent = client.queue_path(project, location, queue) |
68 | 58 |
|
69 | 59 | # Use the client to build and send the task. |
70 | | - response = client.projects().locations().queues().tasks().create( |
71 | | - parent=queue_name, body=body).execute() |
| 60 | + response = client.create_task(parent, task) |
72 | 61 |
|
73 | | - print('Created task {}'.format(response['name'])) |
| 62 | + print('Created task {}'.format(response.name)) |
74 | 63 | return response |
75 | 64 | # [END cloud_tasks_appengine_create_task] |
76 | 65 |
|
|
0 commit comments