|
| 1 | +# Copyright 2019 Google LLC All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import print_function |
| 16 | + |
| 17 | +import argparse |
| 18 | + |
| 19 | + |
| 20 | +def create_http_task(project, |
| 21 | + queue, |
| 22 | + location, |
| 23 | + url, |
| 24 | + payload=None, |
| 25 | + in_seconds=None, |
| 26 | + task_name=None): |
| 27 | + # [START cloud_tasks_create_http_task] |
| 28 | + """Create a task for a given queue with an arbitrary payload.""" |
| 29 | + |
| 30 | + from google.cloud import tasks_v2 |
| 31 | + from google.protobuf import timestamp_pb2 |
| 32 | + import datetime |
| 33 | + import json |
| 34 | + |
| 35 | + # Create a client. |
| 36 | + client = tasks_v2.CloudTasksClient() |
| 37 | + |
| 38 | + # TODO(developer): Uncomment these lines and replace with your values. |
| 39 | + # project = 'my-project-id' |
| 40 | + # queue = 'my-queue' |
| 41 | + # location = 'us-central1' |
| 42 | + # url = 'https://example.com/task_handler' |
| 43 | + # payload = 'hello' or {'param': 'value'} for application/json |
| 44 | + |
| 45 | + # Construct the fully qualified queue name. |
| 46 | + parent = client.queue_path(project, location, queue) |
| 47 | + |
| 48 | + # Construct the request body. |
| 49 | + task = { |
| 50 | + 'http_request': { # Specify the type of request. |
| 51 | + 'http_method': 'POST', |
| 52 | + 'url': url # The full url path that the task will be sent to. |
| 53 | + } |
| 54 | + } |
| 55 | + if payload is not None: |
| 56 | + if isinstance(payload, dict): |
| 57 | + # Convert dict to JSON string |
| 58 | + payload = json.dumps(payload) |
| 59 | + # specify http content-type to application/json |
| 60 | + task['http_request']['headers'] = {'Content-type': 'application/json'} |
| 61 | + |
| 62 | + # The API expects a payload of type bytes. |
| 63 | + converted_payload = payload.encode() |
| 64 | + |
| 65 | + # Add the payload to the request. |
| 66 | + task['http_request']['body'] = converted_payload |
| 67 | + |
| 68 | + if in_seconds is not None: |
| 69 | + # Convert "seconds from now" into an rfc3339 datetime string. |
| 70 | + d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds) |
| 71 | + |
| 72 | + # Create Timestamp protobuf. |
| 73 | + timestamp = timestamp_pb2.Timestamp() |
| 74 | + timestamp.FromDatetime(d) |
| 75 | + |
| 76 | + # Add the timestamp to the tasks. |
| 77 | + task['schedule_time'] = timestamp |
| 78 | + |
| 79 | + if task_name is not None: |
| 80 | + # Add the name to tasks. |
| 81 | + task['name'] = task_name |
| 82 | + |
| 83 | + # Use the client to build and send the task. |
| 84 | + response = client.create_task(parent, task) |
| 85 | + |
| 86 | + print('Created task {}'.format(response.name)) |
| 87 | + # [END cloud_tasks_create_http_task] |
| 88 | + return response |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + parser = argparse.ArgumentParser( |
| 93 | + description=create_http_task.__doc__, |
| 94 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 95 | + |
| 96 | + parser.add_argument( |
| 97 | + '--project', |
| 98 | + help='Project of the queue to add the task to.', |
| 99 | + required=True, |
| 100 | + ) |
| 101 | + |
| 102 | + parser.add_argument( |
| 103 | + '--queue', |
| 104 | + help='ID (short name) of the queue to add the task to.', |
| 105 | + required=True, |
| 106 | + ) |
| 107 | + |
| 108 | + parser.add_argument( |
| 109 | + '--location', |
| 110 | + help='Location of the queue to add the task to.', |
| 111 | + required=True, |
| 112 | + ) |
| 113 | + |
| 114 | + parser.add_argument( |
| 115 | + '--url', |
| 116 | + help='The full url path that the request will be sent to.', |
| 117 | + required=True, |
| 118 | + ) |
| 119 | + |
| 120 | + parser.add_argument( |
| 121 | + '--payload', |
| 122 | + help='Optional payload to attach to the push queue.' |
| 123 | + ) |
| 124 | + |
| 125 | + parser.add_argument( |
| 126 | + '--in_seconds', type=int, |
| 127 | + help='The number of seconds from now to schedule task attempt.' |
| 128 | + ) |
| 129 | + |
| 130 | + parser.add_argument( |
| 131 | + '--task_name', |
| 132 | + help='Task name of the task to create' |
| 133 | + ) |
| 134 | + args = parser.parse_args() |
| 135 | + |
| 136 | + create_http_task( |
| 137 | + args.project, args.queue, args.location, args.url, |
| 138 | + args.payload, args.in_seconds, args.task_name) |
0 commit comments