1818import datetime
1919
2020
21- # [START cloud_tasks_appengine_create_task]
2221def create_task (project , queue , location , payload = None , in_seconds = None ):
22+ # [START cloud_tasks_appengine_create_task]
2323 """Create a task for a given queue with an arbitrary payload."""
2424
25- from google .cloud import tasks_v2beta2
25+ from google .cloud import tasks_v2beta3
2626 from google .protobuf import timestamp_pb2
2727
2828 # Create a client.
29- client = tasks_v2beta2 .CloudTasksClient ()
29+ client = tasks_v2beta3 .CloudTasksClient ()
30+
31+ # TODO(developer): Uncomment these lines and replace with your values.
32+ # project = 'my-project-id'
33+ # queue = 'my-appengine-queue'
34+ # location = 'us-central1'
35+ # payload = 'hello'
36+
37+ # Construct the fully qualified queue name.
38+ parent = client .queue_path (project , location , queue )
3039
3140 # Construct the request body.
3241 task = {
3342 'app_engine_http_request' : { # Specify the type of request.
3443 'http_method' : 'POST' ,
35- 'relative_url ' : '/example_task_handler'
44+ 'relative_uri ' : '/example_task_handler'
3645 }
3746 }
3847 if payload is not None :
3948 # The API expects a payload of type bytes.
4049 converted_payload = payload .encode ()
4150
4251 # Add the payload to the request.
43- task ['app_engine_http_request' ]['payload ' ] = converted_payload
52+ task ['app_engine_http_request' ]['body ' ] = converted_payload
4453
4554 if in_seconds is not None :
4655 # Convert "seconds from now" into an rfc3339 datetime string.
@@ -50,12 +59,9 @@ def create_task(project, queue, location, payload=None, in_seconds=None):
5059 timestamp = timestamp_pb2 .Timestamp ()
5160 timestamp .FromDatetime (d )
5261
53- # Add the rfc3339 datetime string to the request .
62+ # Add the timestamp to the tasks .
5463 task ['schedule_time' ] = timestamp
5564
56- # Construct the fully qualified queue name.
57- parent = client .queue_path (project , location , queue )
58-
5965 # Use the client to build and send the task.
6066 response = client .create_task (parent , task )
6167
0 commit comments