|
| 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 datetime |
| 18 | + |
| 19 | + |
| 20 | +def create_http_task(project, |
| 21 | + queue, |
| 22 | + location, |
| 23 | + url, |
| 24 | + service_account_email, |
| 25 | + payload=None, |
| 26 | + in_seconds=None): |
| 27 | + # [START cloud_tasks_create_http_task_with_token] |
| 28 | + """Create a task for a given queue with an arbitrary payload.""" |
| 29 | + |
| 30 | + from google.cloud import tasks_v2beta3 |
| 31 | + from google.protobuf import timestamp_pb2 |
| 32 | + |
| 33 | + # Create a client. |
| 34 | + client = tasks_v2beta3.CloudTasksClient() |
| 35 | + |
| 36 | + # TODO(developer): Uncomment these lines and replace with your values. |
| 37 | + # project = 'my-project-id' |
| 38 | + # queue = 'my-appengine-queue' |
| 39 | + # location = 'us-central1' |
| 40 | + # url = 'https://example.com/example_task_handler' |
| 41 | + # payload = 'hello' |
| 42 | + |
| 43 | + # Construct the fully qualified queue name. |
| 44 | + parent = client.queue_path(project, location, queue) |
| 45 | + |
| 46 | + # Construct the request body. |
| 47 | + task = { |
| 48 | + 'http_request': { # Specify the type of request. |
| 49 | + 'http_method': 'POST', |
| 50 | + 'url': url, # The full url path that the task will be sent to. |
| 51 | + 'oidc_token': { |
| 52 | + 'service_account_email': service_account_email |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if payload is not None: |
| 58 | + # The API expects a payload of type bytes. |
| 59 | + converted_payload = payload.encode() |
| 60 | + |
| 61 | + # Add the payload to the request. |
| 62 | + task['http_request']['body'] = converted_payload |
| 63 | + |
| 64 | + if in_seconds is not None: |
| 65 | + # Convert "seconds from now" into an rfc3339 datetime string. |
| 66 | + d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds) |
| 67 | + |
| 68 | + # Create Timestamp protobuf. |
| 69 | + timestamp = timestamp_pb2.Timestamp() |
| 70 | + timestamp.FromDatetime(d) |
| 71 | + |
| 72 | + # Add the timestamp to the tasks. |
| 73 | + task['schedule_time'] = timestamp |
| 74 | + |
| 75 | + # Use the client to build and send the task. |
| 76 | + response = client.create_task(parent, task) |
| 77 | + |
| 78 | + print('Created task {}'.format(response.name)) |
| 79 | + return response |
| 80 | +# [END cloud_tasks_create_http_task_with_token] |
0 commit comments