Skip to content

Commit

Permalink
Updates for CloudEvents (#17)
Browse files Browse the repository at this point in the history
* Move HTTP example

* Add example of CloudEvent function

* Error if data is empty

* Add a dummy endpoint for GET healthchecks on /

* Update changelog
  • Loading branch information
di authored Feb 7, 2020
1 parent 2a30643 commit 6b3ae16
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Make `--debug` a flag instead of a boolean option

### Fixed
- Better support for CloudEvent functions and error handling

## [1.0.1] - 2020-01-30
### Added
- Add Cloud Run Button ([#1])
Expand Down
3 changes: 2 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Python Functions Frameworks Examples

* [`cloud_run`](./cloud_run/) - Deploying a function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
* [`cloud_run_http`](./cloud_run_http/) - Deploying an HTTP function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
* [`cloud_run_event`](./cloud_run_event/) - Deploying a CloudEvent function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
18 changes: 18 additions & 0 deletions examples/cloud_run_event/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Use the official Python image.
# https://hub.docker.com/_/python
FROM python:3.7-slim

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .

# Install production dependencies.
RUN pip install gunicorn functions-framework
RUN pip install -r requirements.txt

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 -e FUNCTION_TARGET=hello -e FUNCTION_SIGNATURE_TYPE=event functions_framework:app
17 changes: 17 additions & 0 deletions examples/cloud_run_event/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def hello(data, context):
pass
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions examples/cloud_run_http/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Optionally include additional dependencies here
5 changes: 5 additions & 0 deletions src/functions_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def view_func(path):
else:
# This is a regular CloudEvent
event_data = request.get_json()
if not event_data:
flask.abort(400)
event_object = _Event(**event_data)
data = event_object.data
context = Context(**event_object.context)
Expand Down Expand Up @@ -184,6 +186,9 @@ def create_app(target=None, source=None, signature_type=None):
werkzeug.routing.Rule("/<path:path>", endpoint="run", methods=["POST"])
)
app.view_functions["run"] = _event_view_func_wrapper(function, flask.request)
# Add a dummy endpoint for GET /
app.url_map.add(werkzeug.routing.Rule("/", endpoint="get", methods=["GET"]))
app.view_functions["get"] = lambda: ""
else:
raise FunctionsFrameworkException(
"Invalid signature type: {signature_type}".format(
Expand Down
20 changes: 20 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ def test_background_function_executes(background_json):
assert resp.status_code == 200


def test_background_function_supports_get(background_json):
source = TEST_FUNCTIONS_DIR / "background_trigger" / "main.py"
target = "function"

client = create_app(target, source, "event").test_client()

resp = client.get("/")
assert resp.status_code == 200


def test_background_function_executes_entry_point_one(background_json):
source = TEST_FUNCTIONS_DIR / "background_multiple_entry_points" / "main.py"
target = "myFunctionFoo"
Expand Down Expand Up @@ -227,6 +237,16 @@ def test_pubsub_payload(background_json):
)


def test_background_function_no_data(background_json):
source = TEST_FUNCTIONS_DIR / "background_trigger" / "main.py"
target = "function"

client = create_app(target, source, "event").test_client()

resp = client.post("/")
assert resp.status_code == 400


def test_invalid_function_definition_missing_function_file():
source = TEST_FUNCTIONS_DIR / "missing_function_file" / "main.py"
target = "functions"
Expand Down

0 comments on commit 6b3ae16

Please sign in to comment.