Skip to content

Commit 52b2a28

Browse files
authored
feat!: Declarative function signatures for python (#160)
1 parent 455affd commit 52b2a28

File tree

16 files changed

+531
-143
lines changed

16 files changed

+531
-143
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ build/
66
dist/
77
.coverage
88
.vscode/
9+
.idea/
910
function_output.json
1011
serverlog_stderr.txt
1112
serverlog_stdout.txt

Diff for: README.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,40 @@ curl localhost:8080
111111
# Output: Hello world!
112112
```
113113

114+
### Quickstart: Register your function using decorator
115+
116+
Create an `main.py` file with the following contents:
117+
118+
```python
119+
import functions_framework
120+
121+
@functions_framework.cloudevent
122+
def hello_cloudevent(cloudevent):
123+
return f"Received event with ID: {cloudevent['id']} and data {cloudevent.data}"
124+
125+
@functions_framework.http
126+
def hello_http(request):
127+
return "Hello world!"
128+
129+
```
130+
131+
Run the following command to run `hello_http` target locally:
132+
133+
```sh
134+
functions-framework --target=hello_http
135+
```
136+
137+
Open http://localhost:8080/ in your browser and see *Hello world!*.
138+
139+
Run the following command to run `hello_cloudevent` target locally:
140+
141+
```sh
142+
functions-framework --target=hello_cloudevent
143+
```
144+
145+
More info on sending [CloudEvents](http://cloudevents.io) payloads, see [`examples/cloud_run_cloudevents`](examples/cloud_run_cloudevents/) instruction.
146+
147+
114148
### Quickstart: Error handling
115149

116150
The framework includes an error handler that is similar to the
@@ -337,7 +371,7 @@ For more details on this signature type, check out the Google Cloud Functions do
337371

338372
## Advanced Examples
339373

340-
More advanced guides can be found in the [`examples/`](https://github.com/GoogleCloudPlatform/functions-framework-python/blob/master/examples/) directory.
374+
More advanced guides can be found in the [`examples/`](examples/) directory.
341375
You can also find examples on using the CloudEvent Python SDK [here](https://github.com/cloudevents/sdk-python).
342376

343377
## Contributing

Diff for: examples/cloud_run_cloudevents/main.py

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
# This sample creates a function using the CloudEvents SDK
1616
# (https://github.com/cloudevents/sdk-python)
17-
import sys
1817

1918

2019
def hello(cloudevent):

Diff for: examples/cloud_run_cloudevents/send_cloudevent.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@
1515
# limitations under the License.
1616
from cloudevents.http import CloudEvent, to_structured
1717
import requests
18-
import json
1918

2019

2120
# Create a cloudevent using https://github.com/cloudevents/sdk-python
22-
# Note we only need source and type because the cloudevents constructor by
23-
# default will set "specversion" to the most recent cloudevent version (e.g. 1.0)
24-
# and "id" to a generated uuid.uuid4 string.
21+
# Note we only need source and type because the cloudevents constructor by
22+
# default will set "specversion" to the most recent cloudevent version (e.g. 1.0)
23+
# and "id" to a generated uuid.uuid4 string.
2524
attributes = {
2625
"Content-Type": "application/json",
2726
"source": "from-galaxy-far-far-away",
28-
"type": "cloudevent.greet.you"
27+
"type": "cloudevent.greet.you",
2928
}
30-
data = {"name":"john"}
29+
data = {"name": "john"}
3130

3231
event = CloudEvent(attributes, data)
3332

Diff for: examples/cloud_run_decorator/Dockerfile

+17
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-slim
4+
5+
# Copy local code to the container image.
6+
ENV APP_HOME /app
7+
ENV PYTHONUNBUFFERED TRUE
8+
9+
WORKDIR $APP_HOME
10+
COPY . .
11+
12+
# Install production dependencies.
13+
RUN pip install functions-framework
14+
RUN pip install -r requirements.txt
15+
16+
# Run the web service on container startup.
17+
CMD exec functions-framework --target=hello_http

Diff for: examples/cloud_run_decorator/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## How to run this locally
2+
3+
This guide shows how to run `hello_http` target locally.
4+
To test with `hello_cloudevent`, change the target accordingly in Dockerfile.
5+
6+
Build the Docker image:
7+
8+
```commandline
9+
docker build -t decorator_example .
10+
```
11+
12+
Run the image and bind the correct ports:
13+
14+
```commandline
15+
docker run --rm -p 8080:8080 -e PORT=8080 decorator_example
16+
```
17+
18+
Send requests to this function using `curl` from another terminal window:
19+
20+
```sh
21+
curl localhost:8080
22+
# Output: Hello world!
23+
```

Diff for: examples/cloud_run_decorator/main.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2021 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+
# This sample creates a function using the CloudEvents SDK
16+
# (https://github.com/cloudevents/sdk-python)
17+
import functions_framework
18+
19+
20+
@functions_framework.cloudevent
21+
def hello_cloudevent(cloudevent):
22+
return f"Received event with ID: {cloudevent['id']} and data {cloudevent.data}"
23+
24+
25+
@functions_framework.http
26+
def hello_http(request):
27+
return "Hello world!"

Diff for: examples/cloud_run_decorator/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Optionally include additional dependencies here

Diff for: examples/cloud_run_http/main.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
1516
def hello(request):
1617
return "Hello world!"

Diff for: setup.cfg

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[isort]
2-
multi_line_output=3
3-
include_trailing_comma=True
4-
force_grid_wrap=0
5-
use_parentheses=True
6-
line_length=88
7-
lines_between_types=1
8-
combine_as_imports=True
9-
default_section=THIRDPARTY
10-
known_first_party=functions_framework,google.cloud.functions
2+
multi_line_output = 3
3+
include_trailing_comma = True
4+
force_grid_wrap = 0
5+
use_parentheses = True
6+
line_length = 88
7+
lines_between_types = 1
8+
combine_as_imports = True
9+
default_section = THIRDPARTY
10+
known_first_party = functions_framework, google.cloud.functions

0 commit comments

Comments
 (0)