8000 codelabs/functions: create 'Python Powered' codelab (#2262) · askmeegs/python-docs-samples@55bb04f · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit 55bb04f

Browse files
ditheacodes
authored andcommitted
codelabs/functions: create 'Python Powered' codelab (GoogleCloudPlatform#2262)
* codelabs/functions: create 'Python Powered' codelab * Add license headers * Import modules, not members
1 parent ca3f242 commit 55bb04f

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2019 Google Inc. 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 flask
16+
17+
import main
18+
19+
20+
app = flask.Flask(__name__)
21+
22+
23+
@app.route("/")
24+
def index():
25+
return main.python_powered(flask.request)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2017 Google Inc. 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 flask
16+
17+
18+
def hello_world(request):
19+
"""HTTP Cloud Function.
20+
Args:
21+
request (flask.Request): The request object.
22+
<http://flask.pocoo.org/docs/1.0/api/#flask.Request>
23+
Returns:
24+
The response text, or any set of values that can be turned into a
25+
Response object using `make_response`
26+
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
27+
"""
28+
return "Hello World!"
29+
30+
31+
def hello_name(request):
32+
"""HTTP Cloud Function.
33+
Args:
34+
request (flask.Request): The request object.
35+
<http://flask.pocoo.org/docs/1.0/api/#flask.Request>
36+
Returns:
37+
The response text, or any set of values that can be turned into a
38+
Response object using `make_response`
39+
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
40+
"""
41+
request_args = request.args
42+
43+
if request_args and "name" in request_args:
44+
name = request_args["name"]
45+
else:
46+
name = "World"
47+
return "Hello {}!".format(flask.escape(name))
48+
49+
50+
def python_powered(request):
51+
"""HTTP Cloud Function.
52+
Args:
53+
request (flask.Request): The request object.
54+
<http://flask.pocoo.org/docs/1.0/api/#flask.Request>
55+
Returns:
56+
The response file, a JPG image that says "Python Powered"
57+
"""
58+
return flask.send_file("python_powered.jpg", mimetype="image/jpg")
Loading
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2017 Google Inc. 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 unittest
16+
import unittest.mock
17+
18+
import main
19+
20+
21+
class TestHello(unittest.TestCase):
22+
def test_hello_world(self):
23+
req = unittest.mock.Mock()
24+
25+
# Call tested function
26+
assert main.hello_world(req) == "Hello World!"
27+
28+
def test_hello_name_no_name(self):
29+
req = unittest.mock.Mock(args={})
30+
31+
# Call tested function
32+
assert main.hello_name(req) == "Hello World!"
33+
34+
def test_hello_name_with_name(self):
35+
name = "test"
36+
req = unittest.mock.Mock(args={"name": name})
37+
38+
# Call tested function
39+
assert main.hello_name(req) == "Hello {}!".format(name)

0 commit comments

Comments
 (0)
0