Skip to content

Commit 8fe1db1

Browse files
fix: restore/update cloud-functions-python-http (GoogleCloudPlatform#9721)
Co-authored-by: Dan Lee <[email protected]>
1 parent d23c8f6 commit 8fe1db1

File tree

7 files changed

+140
-1
lines changed

7 files changed

+140
-1
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
# Self-service
8585
# ---* Shared with DEE Teams
8686
/functions/**/* @GoogleCloudPlatform/functions-framework-google @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
87+
/codelabs/**/* @GoogleCloudPlatform/codelabs-contributors @GoogleCloudPlatform/python-samples-reviewers
8788
/composer/**/* @GoogleCloudPlatform/cloud-dpes-composer @GoogleCloudPlatform/python-samples-reviewers
8889
/pubsub/**/* @GoogleCloudPlatform/api-pubsub-and-pubsublite @GoogleCloudPlatform/python-samples-reviewers
8990
/pubsublite/**/* @GoogleCloudPlatform/api-pubsub-and-pubsublite @GoogleCloudPlatform/python-samples-reviewers
@@ -114,4 +115,4 @@
114115
# BEGIN - pending clarification
115116
/memorystore/**/* @GoogleCloudPlatform/python-samples-reviewers
116117
/opencensus/**/* @GoogleCloudPlatform/python-samples-reviewers
117-
# END - pending clarification
118+
# END - pending clarification

codelabs/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Resources in this folder
2+
3+
- This folder contains resources used in Google Cloud codelabs on [Google Developers Codelabs](https://codelabs.developers.google.com/?category=cloud).
4+
- Each folder is named after the codelab ID. Example:
5+
- Folder: `cloud-functions-python-http`
6+
- Codelab: <https://codelabs.developers.google.com/codelabs/cloud-functions-python-http>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file specifies files that are *not* uploaded to Google Cloud
2+
# using gcloud. It follows the same syntax as .gitignore, with the addition of
3+
# "#!include" directives (which insert the entries of the given .gitignore-style
4+
# file at that point).
5+
#
6+
# For more information, run:
7+
# $ gcloud topic gcloudignore
8+
#
9+
__pycache__/**
10+
.gcloudignore
11+
test_*.py
12+
web_app.py
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2023 Google LLC
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: flask.Request) -> flask.Response:
19+
"""HTTP Cloud Function.
20+
21+
Returns:
22+
- "Hello World! 👋"
23+
"""
24+
response = "Hello World! 👋"
25+
26+
return flask.Response(response, mimetype="text/plain")
27+
28+
29+
def hello_name(request: flask.Request) -> flask.Response:
30+
"""HTTP Cloud Function.
31+
32+
Returns:
33+
- "Hello {NAME}! 🚀" if "name=NAME" is defined in the GET request
34+
- "Hello World! 🚀" otherwise
35+
"""
36+
name = request.args.get("name", "World")
37+
response = f"Hello {name}! 🚀"
38+
39+
return flask.Response(response, mimetype="text/plain")
40+
41+
42+
def python_powered(request: flask.Request) -> flask.Response:
43+
"""HTTP Cloud Function.
44+
45+
Returns:
46+
- The official "Python Powered" logo
47+
"""
48+
return flask.send_file("python-powered.png")
61.1 KB
Loading
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2023 Google LLC
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+
request = unittest.mock.Mock()
24+
25+
response = main.hello_world(request)
26+
assert response.status_code == 200
27+
assert response.get_data(as_text=True) == "Hello World! 👋"
28+
29+
def test_hello_name_no_name(self):
30+
request = unittest.mock.Mock(args={})
31+
32+
response = main.hello_name(request)
33+
assert response.status_code == 200
34+
assert response.get_data(as_text=True) == "Hello World! 🚀"
35+
36+
def test_hello_name_with_name(self):
37+
name = "FirstName LastName"
38+
request = unittest.mock.Mock(args={"name": name})
39+
40+
response = main.hello_name(request)
41+
assert response.status_code == 200
42+
assert response.get_data(as_text=True) == f"Hello {name}! 🚀"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2023 Google LLC
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+
app = flask.Flask(__name__)
20+
21+
22+
@app.get("/")
23+
def index():
24+
return main.python_powered(flask.request)
25+
26+
27+
if __name__ == "__main__":
28+
# Local development only
29+
# Run "python web_app.py" and open http://localhost:8080
30+
app.run(host="localhost", port=8080, debug=True)

0 commit comments

Comments
 (0)