From e07a260cee7f71d047a4b7147c06357dc5188d89 Mon Sep 17 00:00:00 2001 From: Andrew Gorcester Date: Tue, 12 Dec 2017 09:25:25 -0800 Subject: [PATCH] Add comments and region tags to Cloud Tasks samples [(#1271)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1271) --- samples/appengine/flexible/tasks/README.md | 4 +-- .../tasks/create_app_engine_queue_task.py | 33 ++++++++++++------- samples/appengine/flexible/tasks/main.py | 2 ++ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/samples/appengine/flexible/tasks/README.md b/samples/appengine/flexible/tasks/README.md index 60f4b69e..ffab1069 100644 --- a/samples/appengine/flexible/tasks/README.md +++ b/samples/appengine/flexible/tasks/README.md @@ -12,8 +12,8 @@ App Engine queues push tasks to an App Engine HTTP target. This directory contains both the App Engine app to deploy, as well as the snippets to run locally to push tasks to it, which could also be called on App Engine. -`app_engine_queue_snippets.py` is a simple command-line program to create tasks -to be pushed to the App Engine app. +`create_app_engine_queue_task.py` is a simple command-line program to create +tasks to be pushed to the App Engine app. `main.py` is the main App Engine app. This app serves as an endpoint to receive App Engine task attempts. diff --git a/samples/appengine/flexible/tasks/create_app_engine_queue_task.py b/samples/appengine/flexible/tasks/create_app_engine_queue_task.py index 66cac1ff..150ec60a 100644 --- a/samples/appengine/flexible/tasks/create_app_engine_queue_task.py +++ b/samples/appengine/flexible/tasks/create_app_engine_queue_task.py @@ -20,12 +20,7 @@ import json -def seconds_from_now_to_rfc3339_datetime(seconds): - """Return an RFC 3339 datetime string for a number of seconds from now.""" - d = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds) - return d.isoformat('T') + 'Z' - - +# [START cloud_tasks_appengine_create_task] def create_task(project, queue, location, payload=None, in_seconds=None): """Create a task for a given queue with an arbitrary payload.""" @@ -34,10 +29,11 @@ def create_task(project, queue, location, payload=None, in_seconds=None): # Create a client. client = googleapiclient.discovery.build('cloudtasks', 'v2beta2') + # Construct the request body. url = '/log_payload' body = { 'task': { - 'app_engine_http_request': { + 'app_engine_http_request': { # Specify the type of request. 'http_method': 'POST', 'relative_url': url } @@ -45,25 +41,38 @@ def create_task(project, queue, location, payload=None, in_seconds=None): } if payload is not None: - # Payload is a string (unicode), and must be encoded for base64. - # The finished request body is JSON, which requires unicode. - body['task']['app_engine_http_request']['payload'] = base64.b64encode( - payload.encode()).decode() + # The API expects base64 encoding of the payload, so encode the unicode + # `payload` object into a byte string and base64 encode it. + base64_encoded_payload = base64.b64encode(payload.encode()) + + # The request body object will be emitted in JSON, which requires + # unicode objects, so convert the byte string to unicode, still base64. + converted_payload = base64_encoded_payload.decode() + + # Add the payload to the request. + body['task']['app_engine_http_request']['payload'] = converted_payload if in_seconds is not None: - scheduled_time = seconds_from_now_to_rfc3339_datetime(in_seconds) + # Convert "seconds from now" into an rfc3339 datetime string. + d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds) + scheduled_time = d.isoformat('T') + 'Z' + + # Add the rfc3339 datetime string to the request. body['task']['schedule_time'] = scheduled_time + # Construct the fully qualified queue name. queue_name = 'projects/{}/locations/{}/queues/{}'.format( project, location, queue) print('Sending task {}'.format(json.dumps(body))) + # Use the client to build and send the task. response = client.projects().locations().queues().tasks().create( parent=queue_name, body=body).execute() print('Created task {}'.format(response['name'])) return response +# [END cloud_tasks_appengine_create_task] if __name__ == '__main__': diff --git a/samples/appengine/flexible/tasks/main.py b/samples/appengine/flexible/tasks/main.py index 0bcf6fe0..174e8a3f 100644 --- a/samples/appengine/flexible/tasks/main.py +++ b/samples/appengine/flexible/tasks/main.py @@ -14,6 +14,7 @@ """App Engine app to serve as an endpoint for App Engine queue samples.""" +# [START cloud_tasks_appengine_quickstart] from flask import Flask, request app = Flask(__name__) @@ -25,6 +26,7 @@ def log_payload(): payload = request.get_data(as_text=True) or '(empty payload)' print('Received task with payload: {}'.format(payload)) return 'Printed task payload: {}'.format(payload) +# [END cloud_tasks_appengine_quickstart] @app.route('/')