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

Commit

Permalink
tasks: added json content-type request [(#4473)](GoogleCloudPlatform/…
Browse files Browse the repository at this point in the history
…python-docs-samples#4473)

- added json payload compatibility
- fix imports and code block used on https://cloud.google.com/tasks/docs/creating-http-target-tasks#python

## Description

Fixes #<ISSUE-NUMBER>

Note: It's a good idea to open an issue first for discussion.

## Checklist
- [ ] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md)
- [ ] README is updated to include [all relevant information](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#readme-file)
- [ ] **Tests** pass:   `nox -s py-3.6` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup))
- [ ] **Lint** pass:   `nox -s lint` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup))
- [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones)
- [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones)
- [ ] Please **merge** this PR for me once it is approved.
- [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.github/CODEOWNERS) with the codeowners for this sample
  • Loading branch information
jlsneto committed Aug 11, 2020
1 parent 05ce0c5 commit bc21141
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions samples/snippets/create_http_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from __future__ import print_function

import argparse
import datetime


def create_http_task(project,
Expand All @@ -30,6 +29,8 @@ def create_http_task(project,

from google.cloud import tasks_v2
from google.protobuf import timestamp_pb2
import datetime
import json

# Create a client.
client = tasks_v2.CloudTasksClient()
Expand All @@ -39,7 +40,7 @@ def create_http_task(project,
# queue = 'my-queue'
# location = 'us-central1'
# url = 'https://example.com/task_handler'
# payload = 'hello'
# payload = 'hello' or {'param': 'value'} for application/json

# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)
Expand All @@ -52,6 +53,12 @@ def create_http_task(project,
}
}
if payload is not None:
if isinstance(payload, dict):
# Convert dict to JSON string
payload = json.dumps(payload)
# specify http content-type to application/json
task['http_request']['headers'] = {'Content-type': 'application/json'}

# The API expects a payload of type bytes.
converted_payload = payload.encode()

Expand All @@ -77,8 +84,8 @@ def create_http_task(project,
response = client.create_task(parent, task)

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


if __name__ == '__main__':
Expand Down

0 comments on commit bc21141

Please sign in to comment.