8000 Added cloudevent signature type using cloudevents sdk-python (#78) · jrmfg/functions-framework-python@fccb88c · GitHub
[go: up one dir, main page]

Skip to content

Commit fccb88c

Browse files
cumason123diglasntgrayside
authored
Added cloudevent signature type using cloudevents sdk-python (GoogleCloudPlatform#78)
* Version 1.5.0 (GoogleCloudPlatform#69) * Revert "Version 2.0.0 (GoogleCloudPlatform#67)" This reverts commit f2471b4. * Revert "Add Cloud Events support for GoogleCloudPlatform#55 (GoogleCloudPlatform#56) (GoogleCloudPlatform#64)" This reverts commit 8f3fe35. * Version 1.5.0 * Improve documentation around Dockerfiles (GoogleCloudPlatform#70) * Add a link to an example Dockerfile in the top README.md * Update the inline Dockerfile to match file * Remove explicit gunicorn installation * make readme links absolute, useful Useful for when this readme appears on both github and pypi * added cloudevents 1.0.0 Signed-off-by: Curtis Mason <cumason@google.com> * reverted auto format Signed-off-by: Curtis Mason <cumason@google.com> * lint fixes Signed-off-by: Curtis Mason <cumason@google.com> * changed cloudevents to <=1.0 in setup Signed-off-by: Curtis Mason <cumason@google.com> * made cloudevents==1.0 Signed-off-by: Curtis Mason <cumason@google.com> * added cloudevent_view tests Signed-off-by: Curtis Mason <cumason@google.com> * test lint fixes Signed-off-by: Curtis Mason <cumason@google.com> * implemented try_catch in cloudevent view wrapper Signed-off-by: Curtis Mason <cumason@google.com> * import fix Signed-off-by: Curtis Mason <cumason@google.com> * adjusted cloud_run_cloudevents readme Signed-off-by: Curtis Mason <cumason@google.com> * added elif for cloudevent Signed-off-by: Curtis Mason <cumason@google.com> * adjusted README Signed-off-by: Curtis Mason <cumason@google.com> * upgraded to cloudevents 1.0.1 Signed-off-by: Curtis Mason <cumason@google.com> * import ordering lint fix Signed-off-by: Curtis Mason <cumason@google.com> * removed event from readme cloudevents section Signed-off-by: Curtis Mason <cumason@google.com> * resolved various nits and reverted event code Signed-off-by: Curtis Mason <cumason@google.com> * dockerfile env variables Signed-off-by: Curtis Mason <cumason@google.com> * Update examples/cloud_run_cloudevents/main.py Co-authored-by: Dustin Ingram <di@users.noreply.github.com> Signed-off-by: Curtis Mason <cumason@google.com> * cleaned up test_cloudevent_functions Signed-off-by: Curtis Mason <cumason@google.com> * Update examples/cloud_run_cloudevents/Dockerfile Co-authored-by: Adam Ross <grayside@gmail.com> Signed-off-by: Curtis Mason <cumason@google.com> * tunneled cloud_exceptions in flask abort Signed-off-by: Curtis Mason <cumason@google.com> * Added additional documentation in sample code Signed-off-by: Curtis Mason <cumason@google.com> * added time to tests Signed-off-by: Curtis Mason <cumason@google.com> * Update README.md Co-authored-by: Dustin Ingram <di@users.noreply.github.com> * Update README.md Co-authored-by: Dustin Ingram <di@users.noreply.github.com> * Update README.md Co-authored-by: Dustin Ingram <di@users.noreply.github.com> * Update README.md Co-authored-by: Dustin Ingram <di@users.noreply.github.com> * Update examples/cloud_run_cloudevents/send_cloudevent.py Co-authored-by: Dustin Ingram <di@users.noreply.github.com> * Update examples/cloud_run_cloudevents/README.md Co-authored-by: Dustin Ingram <di@users.noreply.github.com> * Update examples/cloud_run_cloudevents/README.md Co-authored-by: Dustin Ingram <di@users.noreply.github.com> * Update src/functions_framework/__init__.py Co-authored-by: Dustin Ingram <di@users.noreply.github.com> * Update src/functions_framework/__init__.py Co-authored-by: Dustin Ingram <di@users.noreply.github.com> * cloudevents 1.1.0 update Signed-off-by: Curtis Mason <cumason@google.com> * simplified exceptions debug Signed-off-by: Curtis Mason <cumason@google.com> * simplified cloudevent view test Signed-off-by: Curtis Mason <cumason@google.com> * Update src/functions_framework/__init__.py Co-authored-by: Dustin Ingram <di@users.noreply.github.com> * shebang cloudevent executable Signed-off-by: Curtis Mason <cumason@google.com> * cloudevents version bump Signed-off-by: Curtis Mason <cumason@google.com> * Removed InvalidStructuredJSON exception Signed-off-by: Curtis Mason <cumason@google.com> * Don't bump version in a feature branch * Add back missing CHANGELOG lines * Reformat with latest black Co-authored-by: Dustin Ingram <di@users.noreply.github.com> Co-authored-by: Katie McLaughlin <glasnt@google.com> Co-authored-by: Adam Ross <grayside@gmail.com>
1 parent 033de0f commit fccb88c

File tree

18 files changed

+578
-424
lines changed

18 files changed

+578
-424
lines changed

README.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,52 +179,45 @@ You can configure the Functions Framework using command-line flags or environmen
179179
| `--host` | `HOST` | The host on which the Functions Framework listens for requests. Default: `0.0.0.0` |
180180
| `--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080` |
181181
| `--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function` |
182-
| `--signature-type` | `FUNCTION_SIGNATURE_TYPE` | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: `http`; accepted values: `http` or `event` or `cloudevent` |
182+
| `--signature-type` | `FUNCTION_SIGNATURE_TYPE` | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: `http`; accepted values: `http`, `event` or `cloudevent` |
183183
| `--source` | `FUNCTION_SOURCE` | The path to the file containing your function. Default: `main.py` (in the current working directory) |
184184
| `--debug` | `DEBUG` | A flag that allows to run functions-framework to run in debug mode, including live reloading. Default: `False` |
185185

186-
187186
## Enable Google Cloud Functions Events
188187

189188
The Functions Framework can unmarshall incoming
190189
Google Cloud Functions [event](https://cloud.google.com/functions/docs/concepts/events-triggers#events) payloads to `data` and `context` objects.
191190
These will be passed as arguments to your function when it receives a request.
192191
Note that your function must use the `event`-style function signature:
193192

194-
195193
```python
196194
def hello(data, context):
197195
print(data)
198196
print(context)
199197
```
200198

201199
To enable automatic unmarshalling, set the function signature type to `event`
202-
using a command-line flag or an environment variable. By default, the HTTP
200+
using the `--signature-type` command-line flag or the `FUNCTION_SIGNATURE_TYPE` environment variable. By default, the HTTP
203201
signature will be used and automatic event unmarshalling will be disabled.
204202

205-
For more details on this signature type, check out the Google Cloud Functions
203+
For more details on this signature type, see the Google Cloud Functions
206204
documentation on
207205
[background functions](https://cloud.google.com/functions/docs/writing/background#cloud_pubsub_example).
208206

209207
See the [running example](examples/cloud_run_event).
210208

211209
## Enable CloudEvents
212210

213-
The Functions Framework can unmarshall incoming
214-
[CloudEvent](http://cloudevents.io) payloads to a `cloudevent` object.
215-
It will be passed as an argument to your function when it receives a request.
216-
Note that your function must use the `cloudevent`-style function signature
217-
211+
The Functions framework can also unmarshall incoming [CloudEvents](http://cloudevents.io) payloads to the `cloudevent` 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:
218212

219213
```python
220214
def hello(cloudevent):
221-
print("Received event with ID: %s" % cloudevent.EventID())
222-
return 200
215+
print(f"Received event with ID: {cloudevent['id']}")
223216< A3E2 /code>
```
224217

225218
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.
226219

227-
See the [running example](examples/cloud_run_cloudevents).
220+
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).
228221

229222
## Advanced Examples
230223

examples/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Python Functions Frameworks Examples
22

33
* [`cloud_run_http`](./cloud_run_http/) - Deploying an HTTP function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
4-
* [`cloud_run_event`](./cloud_run_event/) - Deploying a [Google Cloud Functions Event](https://cloud.google.com/functions/docs/concepts/events-triggers#events) function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
5-
* [`cloud_run_cloudevents`](./cloud_run_cloudevents/) - Deploying a [CloudEvent](https://github.com/cloudevents/sdk-python) function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
4+
* [`cloud_run_event`](./cloud_run_event/) - Deploying a CloudEvent function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
5+
* [`cloud_run_cloudevents`](./cloud_run_cloudevents/) - Deploying a [CloudEvent](https://github.com/cloudevents/sdk-python) function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework

examples/cloud_run_cloudevents/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ FROM python:3.7-slim
44

55
# Copy local code to the container image.
66
ENV APP_HOME /app
7+
ENV PYTHONUNBUFFERED TRUE
8+
79
WORKDIR $APP_HOME
810
COPY . .
911

1012
# Install production dependencies.
1113
RUN pip install gunicorn cloudevents functions-framework
1214
RUN pip install -r requirements.txt
15+
RUN chmod +x send_cloudevent.py
1316

1417
# Run the web service on container startup.
15-
CMD exec functions-framework --target=hello --signature-type=cloudevent
18+
CMD ["functions-framework", "--target=hello", "--signature-type=cloudevent"]
Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,23 @@
1-
# Deploying a CloudEvent function to Cloud Run with the Functions Framework
2-
This sample uses the [Cloud Events SDK](https://github.com/cloudevents/sdk-python) to send and receive a CloudEvent on Cloud Run.
1+
# Deploying a CloudEvent Function to Cloud Run with the Functions Framework
2+
3+
This sample uses the [CloudEvents SDK](https://github.com/cloudevents/sdk-python) to send and receive a [CloudEvent](http://cloudevents.io) on Cloud Run.
34

45
## How to run this locally
6+
57
Build the Docker image:
68

79
```commandline
8-
docker build --tag ff_example .
10+
docker build -t cloudevent_example .
911
```
1012

1113
Run the image and bind the correct ports:
1214

1315
```commandline
14-
docker run -p:8080:8080 ff_example
16+
docker run --rm -p 8080:8080 -e PORT=8080 cloudevent_example
1517
```
1618

1719
Send an event to the container:
1820

1921
```python
20-
from cloudevents.sdk import converters
21-
from cloudevents.sdk import marshaller
22-
from cloudevents.sdk.converters import structured
23-
from cloudevents.sdk.event import v1
24-
import requests
25-
import json
26-
27-
def run_structured(event, url):
28-
http_marshaller = marshaller.NewDefaultHTTPMarshaller()
29-
structured_headers, structured_data = http_marshaller.ToRequest(
30-
event, converters.TypeStructured, json.dumps
31-
)
32-
print("structured CloudEvent")
33-
print(structured_data.getvalue())
34-
35-
response = requests.post(url,
36-
headers=structured_headers,
37-
data=structured_data.getvalue())
38-
response.raise_for_status()
39-
40-
event = (
41-
v1.Event()
42-
.SetContentType("application/json")
43-
.SetData('{"name":"john"}')
44-
.SetEventID("my-id")
45-
.SetSource("from-galaxy-far-far-away")
46-
.SetEventTime("tomorrow")
47-
.SetEventType("cloudevent.greet.you")
48-
)
49-
50-
run_structured(event, "http://0.0.0.0:8080/")
51-
22+
docker run -t cloudevent_example send_cloudevent.py
5223
```

examples/cloud_run_cloudevents/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# This sample creates a function that accepts a Cloud Event per
16-
# https://github.com/cloudevents/sdk-python
15+
# This sample creates a function using the CloudEvents SDK
16+
# (https://github.com/cloudevents/sdk-python)
1717
import sys
1818

1919

2020
def hello(cloudevent):
21-
print("Received event with ID: %s" % cloudevent.EventID(), file=sys.stdout, flush=True)
21+
print(f"Received event with ID: {cloudevent['id']} and data {cloudevent.data}")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# Optionally include additional dependencies here
2+
cloudevents>=1.2.0
3+
requests
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/local/bin/python
2+
3+
# Copyright 2020 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
from cloudevents.http import CloudEvent, to_structured
17+
import requests
18+
import json
19+
20+
21+
# 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.
25+
attributes = {
26+
"Content-Type": "application/json",
27+
"source": "from-galaxy-far-far-away",
28+
"type": "cloudevent.greet.you"
29+
}
30+
data = {"name":"john"}
31+
32+
event = CloudEvent(attributes, data)
33+
34+
# Send the event to our local docker container listening on port 8080
35+
headers, data = to_structured(event)
36+
requests.post("http://localhost:8080/", headers=headers, data=data)

examples/cloud_run_event/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ FROM python:3.7-slim
44

55
# Copy local code to the container image.
66
ENV APP_HOME /app
7+
ENV PYTHONUNBUFFERED TRUE
8+
79
WORKDIR $APP_HOME
810
COPY . .
911

@@ -12,4 +14,4 @@ RUN pip install gunicorn functions-framework
1214
RUN pip install -r requirements.txt
1315

1416
# Run the web service on container startup.
15-
CMD exec functions-framework --target=hello --signature_type=event
17+
CMD exec functions-framework --target=hello --signature-type=event

examples/cloud_run_event/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/cloud_run_http/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ FROM python:3.7-slim
44

55
# Copy local code to the container image.
66
ENV APP_HOME /app
7+
ENV PYTHONUNBUFFERED TRUE
8+
79
WORKDIR $APP_HOME
810
COPY . .
911

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"click>=7.0,<8.0",
5353
"watchdog>=0.10.0",
5454
"gunicorn>=19.2.0,<21.0; platform_system!='Windows'",
55-
"cloudevents<1.0",
55+
"cloudevents>=1.2.0,<2.0.0",
5656
],
5757
entry_points={
5858
"console_scripts": [

0 commit comments

Comments
 (0)
0