Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4693f7b

Browse filesBrowse files
committed
add authenticated task sample
1 parent 426b01c commit 4693f7b
Copy full SHA for 4693f7b

File tree

Expand file treeCollapse file tree

6 files changed

+131
-5
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+131
-5
lines changed

‎appengine/flexible/tasks/Dockerfile

Copy file name to clipboard
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Use the official Python image.
2+
# https://hub.docker.com/_/python
3+
FROM python:3.7
4+
5+
# Copy local code to the container image.
6+
ENV APP_HOME /app
7+
WORKDIR $APP_HOME
8+
COPY . .
9+
10+
# Install production dependencies.
11+
RUN pip install Flask gunicorn
12+
13+
# Run the web service on container startup. Here we use the gunicorn
14+
# webserver, with one worker process and 8 threads.
15+
# For environments with multiple CPU cores, increase the number of workers
16+
# to be equal to the cores available.
17+
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app

‎appengine/flexible/tasks/create_http_task_test.py

Copy file name to clipboardExpand all lines: appengine/flexible/tasks/create_http_task_test.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'my-appengine-queue')
2222

2323

24-
def test_create_task():
25-
url = 'https://' + TEST_PROJECT_ID + '.appspot.com/example_task_handler'
24+
def test_create_http_task():
25+
url = 'https://example.com/example_task_handler'
2626
result = create_http_task.create_http_task(
2727
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION, url)
2828
assert TEST_QUEUE_NAME in result.name
+80Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2019 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import print_function
16+
17+
import argparse
18+
import datetime
19+
20+
21+
def create_http_task(project,
22+
queue,
23+
location,
24+
url,
25+
payload=None,
26+
in_seconds=None):
27+
# [START cloud_tasks_create_http_task_with_token]
28+
"""Create a task for a given queue with an arbitrary payload."""
29+
30+
from google.cloud import tasks_v2beta3
31+
from google.protobuf import timestamp_pb2
32+
33+
# Create a client.
34+
client = tasks_v2beta3.CloudTasksClient()
35+
36+
# TODO(developer): Uncomment these lines and replace with your values.
37+
# project = 'my-project-id'
38+
# queue = 'my-appengine-queue'
39+
# location = 'us-central1'
40+
# url = 'https://<project-id>.appspot.com/example_task_handler'
41+
# payload = 'hello'
42+
43+
# Construct the fully qualified queue name.
44+
parent = client.queue_path(project, location, queue)
45+
46+
# Construct the request body.
47+
task = {
48+
'http_request': { # Specify the type of request.
49+
'http_method': 'POST',
50+
'url': url, # The full url path that the task will be sent to.
51+
'oidc_token': {
52+
'service_account_email':
53+
'<client_id>@<project_id>.iam.gserviceaccount.com'
54+
}
55+
}
56+
}
57+
if payload is not None:
58+
# The API expects a payload of type bytes.
59+
converted_payload = payload.encode()
60+
61+
# Add the payload to the request.
62+
task['http_request']['body'] = converted_payload
63+
64+
if in_seconds is not None:
65+
# Convert "seconds from now" into an rfc3339 datetime string.
66+
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)
67+
68+
# Create Timestamp protobuf.
69+
timestamp = timestamp_pb2.Timestamp()
70+
timestamp.FromDatetime(d)
71+
72+
# Add the timestamp to the tasks.
73+
task['schedule_time'] = timestamp
74+
75+
# Use the client to build and send the task.
76+
response = client.create_task(parent, task)
77+
78+
print('Created task {}'.format(response.name))
79+
return response
80+
# [END cloud_tasks_create_http_task_with_token]
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2019 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import create_http_task_with_token
18+
19+
TEST_PROJECT_ID = os.getenv('GCLOUD_PROJECT')
20+
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
21+
TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'my-appengine-queue')
22+
23+
24+
def test_create_http_task_with_token():
25+
url = 'https://example.com/example_task_handler'
26+
result = create_http_task_with_token.create_http_task(
27+
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION, url)
28+
assert TEST_QUEUE_NAME in result.name

‎appengine/flexible/tasks/main.py

Copy file name to clipboardExpand all lines: appengine/flexible/tasks/main.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
"""App Engine app to serve as an endpoint for App Engine queue samples."""
16-
16+
import os
1717
# [START cloud_tasks_appengine_quickstart]
1818
from flask import Flask, request
1919

@@ -38,4 +38,5 @@ def hello():
3838
if __name__ == '__main__':
3939
# This is used when running locally. Gunicorn is used to run the
4040
# application on Google App Engine. See entrypoint in app.yaml.
41-
app.run(host='127.0.0.1', port=8080, debug=True)
41+
port = os.environ.get('PORT', 8080)
42+
app.run(debug=True, host='0.0.0.0', port=int(port))
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Flask==1.0.2
22
gunicorn==19.9.0
3-
google-cloud-tasks==0.6.0
3+
google-cloud-tasks==0.7.0

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.