Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Add comments and region tags to Cloud Tasks samples [(#1271)](GoogleC…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsg authored and Jon Wayne Parrott committed Dec 12, 2017
1 parent c292cfe commit 2477d24
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion samples/snippets/pull_queue_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import base64


# [START cloud_tasks_create_task]
def create_task(project, queue, location):
"""Create a task for a given queue with an arbitrary payload."""

Expand All @@ -32,25 +33,40 @@ def create_task(project, queue, location):
# Create a client.
client = googleapiclient.discovery.build('cloudtasks', 'v2beta2')

# Prepare the payload.
payload = 'a message for the recipient'

# 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()

# Construct the request body.
task = {
'task': {
'pull_message': {
'payload': base64.b64encode(payload.encode()).decode()
'payload': converted_payload
}
}
}

# Construct the fully qualified queue name.
queue_name = 'projects/{}/locations/{}/queues/{}'.format(
project, location, queue)

# Use the client to build and send the task.
response = client.projects().locations().queues().tasks().create(
parent=queue_name, body=task).execute()

print('Created task {}'.format(response['name']))
return response
# [END cloud_tasks_create_task]


# [START cloud_tasks_pull_task]
def pull_task(project, queue, location):
"""Pull a single task from a given queue and lease it for 10 minutes."""

Expand All @@ -74,6 +90,7 @@ def pull_task(project, queue, location):

print('Pulled task {}'.format(response))
return response['tasks'][0]
# [END cloud_tasks_pull_task]


def acknowledge_task(task):
Expand Down

0 comments on commit 2477d24

Please sign in to comment.