Skip to content

Commit a1c4690

Browse files
committed
Revert "Add Cloud Events support for #55 (#56) (#64)"
This reverts commit 8f3fe35.
1 parent 9e4aa46 commit a1c4690

File tree

16 files changed

+219
-599
lines changed

16 files changed

+219
-599
lines changed

README.md

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -129,57 +129,27 @@ You can configure the Functions Framework using command-line flags or environmen
129129
| `--host` | `HOST` | The host on which the Functions Framework listens for requests. Default: `0.0.0.0` |
130130
| `--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080` |
131131
| `--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function` |
132-
| `--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` |
132+
| `--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` |
133133
| `--source` | `FUNCTION_SOURCE` | The path to the file containing your function. Default: `main.py` (in the current working directory) |
134134
| `--debug` | `DEBUG` | A flag that allows to run functions-framework to run in debug mode, including live reloading. Default: `False` |
135135

136+
# Enable CloudEvents
136137

137-
# Enable Google Cloud Functions Events
138-
139-
The Functions Framework can unmarshall incoming
140-
Google Cloud Functions [event](https://cloud.google.com/functions/docs/concepts/events-triggers#events) payloads to `data` and `context` objects.
141-
These will be passed as arguments to your function when it receives a request.
142-
Note that your function must use the `event`-style function signature:
143-
138+
The Functions Framework can unmarshall incoming [CloudEvents](http://cloudevents.io) payloads to `data` and `context` objects. These will be passed as arguments to your function when it receives a request. Note that your function must use the event-style function signature:
144139

145140
```python
146141
def hello(data, context):
147142
print(data)
148143
print(context)
149144
```
150145

151-
To enable automatic unmarshalling, set the function signature type to `event`
152-
using a command-line flag or an environment variable. By default, the HTTP
153-
signature will be used and automatic event unmarshalling will be disabled.
154-
155-
For more details on this signature type, check out the Google Cloud Functions
156-
documentation on
157-
[background functions](https://cloud.google.com/functions/docs/writing/background#cloud_pubsub_example).
158-
159-
See the [running example](examples/cloud_run_event).
160-
161-
# Enable CloudEvents
162-
163-
The Functions Framework can unmarshall incoming
164-
[CloudEvent](http://cloudevents.io) payloads to a `cloudevent` object.
165-
It will be passed as an argument to your function when it receives a request.
166-
Note that your function must use the `cloudevent`-style function signature
167-
168-
169-
```python
170-
def hello(cloudevent):
171-
print("Received event with ID: %s" % cloudevent.EventID())
172-
return 200
173-
```
174-
175-
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.
146+
To enable automatic unmarshalling, set the function signature type to `event` 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.
176147

177-
See the [running example](examples/cloud_run_cloudevents).
148+
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).
178149

179150
# Advanced Examples
180151

181-
More advanced guides can be found in the [`examples/`](./examples/) directory. You can also find examples
182-
on using the CloudEvent Python SDK [here](https://github.com/cloudevents/sdk-python).
152+
More advanced guides can be found in the [`examples/`](./examples/) directory.
183153

184154
# Contributing
185155

examples/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
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

examples/cloud_run_cloudevents/Dockerfile

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

examples/cloud_run_cloudevents/README.md

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

examples/cloud_run_cloudevents/main.py

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

examples/cloud_run_cloudevents/requirements.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/cloud_run_event/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ RUN pip install gunicorn functions-framework
1212
RUN pip install -r requirements.txt
1313

1414
# Run the web service on container startup.
15-
CMD exec functions-framework --target=hello --signature_type=event
15+
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.

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
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",
5655
],
5756
entry_points={
5857
"console_scripts": [

src/functions_framework/__init__.py

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

15-
import enum
15+
import functools
1616
import importlib.util
17-
import io
18-
import json
1917
import os.path
2018
import pathlib
2119
import sys
2220
import types
2321

24-
import cloudevents.sdk
25-
import cloudevents.sdk.event
26-
import cloudevents.sdk.event.v1
27-
import cloudevents.sdk.marshaller
2822
import flask
2923
import werkzeug
3024

@@ -42,12 +36,6 @@
4236
MAX_CONTENT_LENGTH = 10 * 1024 * 1024
4337

4438

45-
class _EventType(enum.Enum):
46-
LEGACY = 1
47-
CLOUDEVENT_BINARY = 2
48-
CLOUDEVENT_STRUCTURED = 3
49-
50-
5139
class _Event(object):
5240
"""Event passed to background functions."""
5341

@@ -80,83 +68,38 @@ def view_func(path):
8068
return view_func
8169

8270

83-
def _get_cloudevent_version():
84-
return cloudevents.sdk.event.v1.Event()
85-
86-
87-
def _run_legacy_event(function, request):
88-
event_data = request.get_json()
89-
if not event_data:
90-
flask.abort(400)
91-
event_object = _Event(**event_data)
92-
data = event_object.data
93-
context = Context(**event_object.context)
94-
function(data, context)
95-
96-
97-
def _run_binary_cloudevent(function, request, cloudevent_def):
98-
data = io.BytesIO(request.get_data())
99-
http_marshaller = cloudevents.sdk.marshaller.NewDefaultHTTPMarshaller()
100-
event = http_marshaller.FromRequest(
101-
cloudevent_def, request.headers, data, json.load
102-
)
103-
104-
function(event)
105-
106-
107-
def _run_structured_cloudevent(function, request, cloudevent_def):
108-
data = io.StringIO(request.get_data(as_text=True))
109-
m = cloudevents.sdk.marshaller.NewDefaultHTTPMarshaller()
110-
event = m.FromRequest(cloudevent_def, request.headers, data, json.loads)
111-
function(event)
112-
113-
114-
def _get_event_type(request):
115-
if (
71+
def _is_binary_cloud_event(request):
72+
return (
11673
request.headers.get("ce-type")
11774
and request.headers.get("ce-specversion")
11875
and request.headers.get("ce-source")
11976
and request.headers.get("ce-id")
120-
):
121-
return _EventType.CLOUDEVENT_BINARY
122-
elif request.headers.get("Content-Type") == "application/cloudevents+json":
123-
return _EventType.CLOUDEVENT_STRUCTURED
124-
else:
125-
return _EventType.LEGACY
77+
)
12678

12779

12880
def _event_view_func_wrapper(function, request):
12981
def view_func(path):
130-
if _get_event_type(request) == _EventType.LEGACY:
131-
_run_legacy_event(function, request)
132-
else:
133-
# here for defensive backwards compatibility in case we make a mistake in rollout.
134-
flask.abort(
135-
400,
136-
description="The FUNCTION_SIGNATURE_TYPE for this function is set to event "
137-
"but no Google Cloud Functions Event was given. If you are using CloudEvents set "
138-
"FUNCTION_SIGNATURE_TYPE=cloudevent",
82+
if _is_binary_cloud_event(request):
83+
# Support CloudEvents in binary content mode, with data being the
84+
# whole request body and context attributes retrieved from request
85+
# headers.
86+
data = request.get_data()
87+
context = Context(
88+
eventId=request.headers.get("ce-eventId"),
89+
timestamp=request.headers.get("ce-timestamp"),
90+
eventType=request.headers.get("ce-eventType"),
91+
resource=request.headers.get("ce-resource"),
13992
)
140-
141-
return "OK"
142-
143-
return view_func
144-
145-
146-
def _cloudevent_view_func_wrapper(function, request):
147-
def view_func(path):
148-
cloudevent_def = _get_cloudevent_version()
149-
event_type = _get_event_type(request)
150-
if event_type == _EventType.CLOUDEVENT_STRUCTURED:
151-
_run_structured_cloudevent(function, request, cloudevent_def)
152-
elif event_type == _EventType.CLOUDEVENT_BINARY:
153-
_run_binary_cloudevent(function, request, cloudevent_def)
93+
function(data, context)
15494
else:
155-
flask.abort(
156-
400,
157-
description="Function was defined with FUNCTION_SIGNATURE_TYPE=cloudevent "
158-
" but it did not receive a cloudevent as a request.",
159-
)
95+
# This is a regular CloudEvent
96+
event_data = request.get_json()
97+
if not event_data:
98+
flask.abort(400)
99+
event_object = _Event(**event_data)
100+
data = event_object.data
101+
context = Context(**event_object.context)
102+
function(data, context)
160103

161104
return "OK"
162105

@@ -263,27 +206,19 @@ def create_app(target=None, source=None, signature_type=None):
263206
app.view_functions["run"] = _http_view_func_wrapper(function, flask.request)
264207
app.view_functions["error"] = lambda: flask.abort(404, description="Not Found")
265208
app.after_request(read_request)
266-
elif signature_type == "event" or signature_type == "cloudevent":
209+
elif signature_type == "event":
267210
app.url_map.add(
268211
werkzeug.routing.Rule(
269-
"/", defaults={"path": ""}, endpoint=signature_type, methods=["POST"]
212+
"/", defaults={"path": ""}, endpoint="run", methods=["POST"]
270213
)
271214
)
272215
app.url_map.add(
273-
werkzeug.routing.Rule(
274-
"/<path:path>", endpoint=signature_type, methods=["POST"]
275-
)
216+
werkzeug.routing.Rule("/<path:path>", endpoint="run", methods=["POST"])
276217
)
277-
218+
app.view_functions["run"] = _event_view_func_wrapper(function, flask.request)
278219
# Add a dummy endpoint for GET /
279220
app.url_map.add(werkzeug.routing.Rule("/", endpoint="get", methods=["GET"]))
280221
app.view_functions["get"] = lambda: ""
281-
282-
# Add the view functions
283-
app.view_functions["event"] = _event_view_func_wrapper(function, flask.request)
284-
app.view_functions["cloudevent"] = _cloudevent_view_func_wrapper(
285-
function, flask.request
286-
)
287222
else:
288223
raise FunctionsFrameworkException(
289224
"Invalid signature type: {signature_type}".format(

src/functions_framework/_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
@click.option(
2727
"--signature-type",
2828
envvar="FUNCTION_SIGNATURE_TYPE",
29-
type=click.Choice(["http", "event", "cloudevent"]),
29+
type=click.Choice(["http", "event"]),
3030
default="http",
3131
)
3232
@click.option("--host", envvar="HOST", type=click.STRING, default="0.0.0.0")

0 commit comments

Comments
 (0)