1919import datetime
2020import json
2121
22- from googleapiclient import discovery
23-
2422
2523def seconds_from_now_to_rfc3339_datetime (seconds ):
2624 """Return an RFC 3339 datetime string for a number of seconds from now."""
@@ -31,16 +29,15 @@ def seconds_from_now_to_rfc3339_datetime(seconds):
3129def create_task (project , queue , location , payload = None , in_seconds = None ):
3230 """Create a task for a given queue with an arbitrary payload."""
3331
32+ import googleapiclient .discovery
33+
3434 # Create a client.
35- DISCOVERY_URL = (
36- 'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2' )
37- client = discovery .build (
38- 'cloudtasks' , 'v2beta2' , discoveryServiceUrl = DISCOVERY_URL )
35+ client = googleapiclient .discovery .build ('cloudtasks' , 'v2beta2' )
3936
4037 url = '/log_payload'
4138 body = {
4239 'task' : {
43- 'app_engine_task_target ' : {
40+ 'app_engine_http_request ' : {
4441 'http_method' : 'POST' ,
4542 'relative_url' : url
4643 }
@@ -50,7 +47,7 @@ def create_task(project, queue, location, payload=None, in_seconds=None):
5047 if payload is not None :
5148 # Payload is a string (unicode), and must be encoded for base64.
5249 # The finished request body is JSON, which requires unicode.
53- body ['task' ]['app_engine_task_target ' ]['payload' ] = base64 .b64encode (
50+ body ['task' ]['app_engine_http_request ' ]['payload' ] = base64 .b64encode (
5451 payload .encode ()).decode ()
5552
5653 if in_seconds is not None :
@@ -65,10 +62,6 @@ def create_task(project, queue, location, payload=None, in_seconds=None):
6562 response = client .projects ().locations ().queues ().tasks ().create (
6663 parent = queue_name , body = body ).execute ()
6764
68- # By default CreateTaskRequest.responseView is BASIC, so not all
69- # information is retrieved by default because some data, such as payloads,
70- # might be desirable to return only when needed because of its large size
71- # or because of the sensitivity of data that it contains.
7265 print ('Created task {}' .format (response ['name' ]))
7366 return response
7467
0 commit comments