Skip to content

Commit efb0e84

Browse files
authored
docs: update README to use declarative function signatures (#171)
* docs: update README to use declarative function signatures Reduce redundancy in quickstart examples. * Fix newline
1 parent e44416e commit efb0e84

File tree

1 file changed

+33
-55
lines changed

1 file changed

+33
-55
lines changed

README.md

+33-55
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,19 @@ pip install functions-framework
4949
Or, for deployment, add the Functions Framework to your `requirements.txt` file:
5050

5151
```
52-
functions-framework==2.3.0
52+
functions-framework==3.*
5353
```
5454

5555
## Quickstarts
5656

57-
### Quickstart: Hello, World on your local machine
57+
### Quickstart: HTTP Function (Hello World)
5858

5959
Create an `main.py` file with the following contents:
6060

6161
```python
62+
import functions_framework
63+
64+
@functions_framework.http
6265
def hello(request):
6366
return "Hello world!"
6467
```
@@ -67,30 +70,6 @@ def hello(request):
6770
6871
Run the following command:
6972

70-
```sh
71-
functions-framework --target=hello
72-
```
73-
74-
Open http://localhost:8080/ in your browser and see *Hello world!*.
75-
76-
77-
### Quickstart: Set up a new project
78-
79-
Create a `main.py` file with the following contents:
80-
81-
```python
82-
def hello(request):
83-
return "Hello world!"
84-
```
85-
86-
Now install the Functions Framework:
87-
88-
```sh
89-
pip install functions-framework
90-
```
91-
92-
Use the `functions-framework` command to start the built-in local development server:
93-
9473
```sh
9574
functions-framework --target hello --debug
9675
* Serving Flask app "hello" (lazy loading)
@@ -101,17 +80,19 @@ functions-framework --target hello --debug
10180
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
10281
```
10382

104-
(You can also use `functions-framework-python` if you potentially have multiple
83+
(You can also use `functions-framework-python` if you have multiple
10584
language frameworks installed).
10685

107-
Send requests to this function using `curl` from another terminal window:
86+
Open http://localhost:8080/ in your browser and see *Hello world!*.
87+
88+
Or send requests to this function using `curl` from another terminal window:
10889

10990
```sh
11091
curl localhost:8080
11192
# Output: Hello world!
11293
```
11394

114-
### Quickstart: Register your function using decorator
95+
### Quickstart: CloudEvent Function
11596

11697
Create an `main.py` file with the following contents:
11798

@@ -120,27 +101,37 @@ import functions_framework
120101

121102
@functions_framework.cloud_event
122103
def hello_cloud_event(cloud_event):
123-
return f"Received event with ID: {cloud_event['id']} and data {cloud_event.data}"
124-
125-
@functions_framework.http
126-
def hello_http(request):
127-
return "Hello world!"
128-
104+
print(f"Received event with ID: {cloud_event['id']} and data {cloud_event.data}")
129105
```
130106

131-
Run the following command to run `hello_http` target locally:
107+
> Your function is passed a single [CloudEvent](https://github.com/cloudevents/sdk-python/blob/master/cloudevents/sdk/event/v1.py) parameter.
108+
109+
Run the following command to run `hello_cloud_event` target locally:
132110

133111
```sh
134-
functions-framework --target=hello_http
112+
functions-framework --target=hello_cloud_event
135113
```
136114

137-
Open http://localhost:8080/ in your browser and see *Hello world!*.
138-
139-
Run the following command to run `hello_cloud_event` target locally:
115+
In a different terminal, `curl` the Functions Framework server:
140116

141117
```sh
142-
functions-framework --target=hello_cloud_event
118+
curl -X POST localhost:8080 \
119+
-H "Content-Type: application/cloudevents+json" \
120+
-d '{
121+
"specversion" : "1.0",
122+
"type" : "example.com.cloud.event",
123+
"source" : "https://example.com/cloudevents/pull",
124+
"subject" : "123",
125+
"id" : "A234-1234-1234",
126+
"time" : "2018-04-05T17:31:00Z",
127+
"data" : "hello world"
128+
}'
129+
```
130+
131+
Output from the terminal running `functions-framework`:
143132
```
133+
Received event with ID: A234-1234-1234 and data hello world
134+
```
144135

145136
More info on sending [CloudEvents](http://cloudevents.io) payloads, see [`examples/cloud_run_cloud_events`](examples/cloud_run_cloud_events/) instruction.
146137

@@ -333,7 +324,7 @@ You can configure the Functions Framework using command-line flags or environmen
333324
| `--debug` | `DEBUG` | A flag that allows to run functions-framework to run in debug mode, including live reloading. Default: `False` |
334325
| `--dry-run` | `DRY_RUN` | A flag that allows for testing the function build from the configuration without creating a server. Default: `False` |
335326

336-
## Enable Google Cloud Functions Events
327+
## Enable Google Cloud Function Events
337328

338329
The Functions Framework can unmarshall incoming
339330
Google Cloud Functions [event](https://cloud.google.com/functions/docs/concepts/events-triggers#events) payloads to `event` and `context` objects.
@@ -356,19 +347,6 @@ documentation on
356347

357348
See the [running example](examples/cloud_run_event).
358349

359-
## Enable CloudEvents
360-
361-
The Functions framework can also unmarshall incoming [CloudEvents](http://cloudevents.io) payloads to the `cloud_event` object. This will be passed as a [CloudEvent](https://github.com/cloudevents/sdk-python) to your function when it receives a request. Note that your function must use the `CloudEvents`-style function signature:
362-
363-
```python
364-
def hello(cloud_event):
365-
print(f"Received event with ID: {cloud_event['id']}")
366-
```
367-
368-
To enable automatic unmarshalling, set the function signature type to `cloudevent` using the `--signature-type` command-line flag or the `FUNCTION_SIGNATURE_TYPE` environment variable. By default, the HTTP signature type will be used and automatic event unmarshalling will be disabled.
369-
370-
For more details on this signature type, check out the Google Cloud Functions documentation on [background functions](https://cloud.google.com/functions/docs/writing/background#cloud_pubsub_example).
371-
372350
## Advanced Examples
373351

374352
More advanced guides can be found in the [`examples/`](examples/) directory.

0 commit comments

Comments
 (0)