From 3eccb3f9584274c08cda622b7571130126a8218d Mon Sep 17 00:00:00 2001 From: Andrew Gorcester Date: Fri, 22 Sep 2017 16:53:11 -0700 Subject: [PATCH 1/3] Update Task Queue samples for beta --- .../tasks/create_app_engine_queue_task.py | 14 +++++------- appengine/flexible/tasks/main.py | 6 ++--- tasks/pull_queue_snippets.py | 22 +++++++++---------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/appengine/flexible/tasks/create_app_engine_queue_task.py b/appengine/flexible/tasks/create_app_engine_queue_task.py index 92ef564fcd76..c263cb9891ad 100644 --- a/appengine/flexible/tasks/create_app_engine_queue_task.py +++ b/appengine/flexible/tasks/create_app_engine_queue_task.py @@ -31,16 +31,16 @@ def seconds_from_now_to_rfc3339_datetime(seconds): def create_task(project, queue, location, payload=None, in_seconds=None): """Create a task for a given queue with an arbitrary payload.""" + from googleapiclient import discovery + # Create a client. - DISCOVERY_URL = ( - 'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2') client = discovery.build( - 'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL) + 'cloudtasks', 'v2beta2') url = '/log_payload' body = { 'task': { - 'app_engine_task_target': { + 'app_engine_http_request': { 'http_method': 'POST', 'relative_url': url } @@ -50,7 +50,7 @@ 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_task_target']['payload'] = base64.b64encode( + body['task']['app_engine_http_request']['payload'] = base64.b64encode( payload.encode()).decode() if in_seconds is not None: @@ -65,10 +65,6 @@ def create_task(project, queue, location, payload=None, in_seconds=None): response = client.projects().locations().queues().tasks().create( parent=queue_name, body=body).execute() - # By default CreateTaskRequest.responseView is BASIC, so not all - # information is retrieved by default because some data, such as payloads, - # might be desirable to return only when needed because of its large size - # or because of the sensitivity of data that it contains. print('Created task {}'.format(response['name'])) return response diff --git a/appengine/flexible/tasks/main.py b/appengine/flexible/tasks/main.py index c2e20439fdc4..3ca3d8f8474f 100644 --- a/appengine/flexible/tasks/main.py +++ b/appengine/flexible/tasks/main.py @@ -24,9 +24,9 @@ @app.route('/log_payload', methods=['POST']) def log_payload(): """Log the request payload.""" - payload = request.data or "empty payload" - logging.warn(payload) - return 'Logged request payload: {}'.format(payload) + payload = request.get_data(as_text=True) or '(empty payload)' + print('Received task with payload: {}'.format(payload)) + return 'Printed task payload: {}'.format(payload) @app.route('/') diff --git a/tasks/pull_queue_snippets.py b/tasks/pull_queue_snippets.py index 1aa0f8af5e6c..800fed7a2e4c 100644 --- a/tasks/pull_queue_snippets.py +++ b/tasks/pull_queue_snippets.py @@ -23,21 +23,19 @@ import argparse import base64 -from googleapiclient import discovery - def create_task(project, queue, location): """Create a task for a given queue with an arbitrary payload.""" - DISCOVERY_URL = ( - 'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2') + # Create a client. + from googleapiclient import discovery client = discovery.build( - 'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL) + 'cloudtasks', 'v2beta2') payload = 'a message for the recipient' task = { 'task': { - 'pull_task_target': { + 'pull_message': { 'payload': base64.b64encode(payload.encode()).decode() } } @@ -56,10 +54,10 @@ def create_task(project, queue, location): def pull_task(project, queue, location): """Pull a single task from a given queue and lease it for 10 minutes.""" - DISCOVERY_URL = ( - 'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2') + # Create a client. + from googleapiclient import discovery client = discovery.build( - 'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL) + 'cloudtasks', 'v2beta2') duration_seconds = '600s' pull_options = { @@ -81,10 +79,10 @@ def pull_task(project, queue, location): def acknowledge_task(task): """Acknowledge a given task.""" - DISCOVERY_URL = ( - 'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2') + # Create a client. + from googleapiclient import discovery client = discovery.build( - 'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL) + 'cloudtasks', 'v2beta2') body = {'scheduleTime': task['scheduleTime']} client.projects().locations().queues().tasks().acknowledge( From 3944287f74bd2d11dfef16ab53cc80ab9557a5db Mon Sep 17 00:00:00 2001 From: Andrew Gorcester Date: Fri, 22 Sep 2017 17:04:19 -0700 Subject: [PATCH 2/3] Respond to linter complaints --- appengine/flexible/tasks/create_app_engine_queue_task.py | 2 -- appengine/flexible/tasks/main.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/appengine/flexible/tasks/create_app_engine_queue_task.py b/appengine/flexible/tasks/create_app_engine_queue_task.py index c263cb9891ad..40ad9490934f 100644 --- a/appengine/flexible/tasks/create_app_engine_queue_task.py +++ b/appengine/flexible/tasks/create_app_engine_queue_task.py @@ -19,8 +19,6 @@ import datetime import json -from googleapiclient import discovery - def seconds_from_now_to_rfc3339_datetime(seconds): """Return an RFC 3339 datetime string for a number of seconds from now.""" diff --git a/appengine/flexible/tasks/main.py b/appengine/flexible/tasks/main.py index 3ca3d8f8474f..0bcf6fe0645c 100644 --- a/appengine/flexible/tasks/main.py +++ b/appengine/flexible/tasks/main.py @@ -14,8 +14,6 @@ """App Engine app to serve as an endpoint for App Engine queue samples.""" -import logging - from flask import Flask, request app = Flask(__name__) From aa6c226c185f69e22cac323776f6433d169259c1 Mon Sep 17 00:00:00 2001 From: Andrew Gorcester Date: Mon, 25 Sep 2017 10:48:08 -0700 Subject: [PATCH 3/3] Use full import to get client --- .../tasks/create_app_engine_queue_task.py | 5 ++--- tasks/pull_queue_snippets.py | 18 +++++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/appengine/flexible/tasks/create_app_engine_queue_task.py b/appengine/flexible/tasks/create_app_engine_queue_task.py index 40ad9490934f..66cac1ffb56a 100644 --- a/appengine/flexible/tasks/create_app_engine_queue_task.py +++ b/appengine/flexible/tasks/create_app_engine_queue_task.py @@ -29,11 +29,10 @@ def seconds_from_now_to_rfc3339_datetime(seconds): def create_task(project, queue, location, payload=None, in_seconds=None): """Create a task for a given queue with an arbitrary payload.""" - from googleapiclient import discovery + import googleapiclient.discovery # Create a client. - client = discovery.build( - 'cloudtasks', 'v2beta2') + client = googleapiclient.discovery.build('cloudtasks', 'v2beta2') url = '/log_payload' body = { diff --git a/tasks/pull_queue_snippets.py b/tasks/pull_queue_snippets.py index 800fed7a2e4c..803849b24f89 100644 --- a/tasks/pull_queue_snippets.py +++ b/tasks/pull_queue_snippets.py @@ -27,10 +27,10 @@ def create_task(project, queue, location): """Create a task for a given queue with an arbitrary payload.""" + import googleapiclient.discovery + # Create a client. - from googleapiclient import discovery - client = discovery.build( - 'cloudtasks', 'v2beta2') + client = googleapiclient.discovery.build('cloudtasks', 'v2beta2') payload = 'a message for the recipient' task = { @@ -54,10 +54,10 @@ def create_task(project, queue, location): def pull_task(project, queue, location): """Pull a single task from a given queue and lease it for 10 minutes.""" + import googleapiclient.discovery + # Create a client. - from googleapiclient import discovery - client = discovery.build( - 'cloudtasks', 'v2beta2') + client = googleapiclient.discovery.build('cloudtasks', 'v2beta2') duration_seconds = '600s' pull_options = { @@ -79,10 +79,10 @@ def pull_task(project, queue, location): def acknowledge_task(task): """Acknowledge a given task.""" + import googleapiclient.discovery + # Create a client. - from googleapiclient import discovery - client = discovery.build( - 'cloudtasks', 'v2beta2') + client = googleapiclient.discovery.build('cloudtasks', 'v2beta2') body = {'scheduleTime': task['scheduleTime']} client.projects().locations().queues().tasks().acknowledge(