-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Ignore IDE files. * Use the test file directory as a basis instead of cwd. Allows tests to be run from anywhere and enables IDE debugger * Add support for Cloud Events. Rough draft. I will squash a bunch of these interim commits before submitting the PR. DO NOT SUBMIT * Return the functions return value. Test Cloud Events SDK 0.3. Add some error handling. Please see all the TODO questions before I finish off this PR. DO NOT SUBMIT * Minor cleanup. Split test code. * Clean up unused paths, split large test files into two, ensure functions DO NOT return a custom value. General tidy-up. Support binary functions. * Fix lint errors with black. * Fix lint errors with black. * Update setup.py Co-authored-by: Dustin Ingram <[email protected]> * Update tests/test_cloudevent_functions.py Co-authored-by: Dustin Ingram <[email protected]> * Update tests/test_cloudevent_functions.py Co-authored-by: Dustin Ingram <[email protected]> * Update src/functions_framework/__init__.py Co-authored-by: Dustin Ingram <[email protected]> * Update tests/test_functions/cloudevents/main.py Co-authored-by: Dustin Ingram <[email protected]> * Clearer imports. * don't factor out routes. * Add a TODO for testing the different combinations of events and signature types. * Add cloudevent as a signature type in the argument list. * Clarify import. * Clarify import. * A sample that shows how to use a CloudEvent. * In the case of a sig type / event type mismatch throw a 400 * Update the docs to use CloudEvent sig type instead of Event sig type. Note that I wrote the "Event" type is deprecated. Not sure if this is accurate. * Lint fixes. * Tests for checking correct event type corresponds to correct function sig. Fixed abort import error. * Sort imports. * Remove old example. * Readme to explain how to run the sample locally. * Rename cloud_event to cloudevent * For legacy docs, add a notice to the new docs. * There is no 1.1 event type. * use the term cloudevent rather than event everywhere where we are talking about a CloudEvent to disambiguate these signature types. * Update examples/cloudevents/README.md Co-authored-by: Dustin Ingram <[email protected]> * Update examples/cloudevents/README.md Co-authored-by: Dustin Ingram <[email protected]> * Update examples/cloudevents/README.md Co-authored-by: Dustin Ingram <[email protected]> * Update examples/cloudevents/main.py Co-authored-by: Dustin Ingram <[email protected]> * Update tests/test_view_functions.py Co-authored-by: Dustin Ingram <[email protected]> * Add legacy event back to docs. * Add legacy event back to docs. * Use abort from flask for consistency and fix return in event test. * update docs and error messages to better mirror the other runtimes. * Minor fixes to docs w.r.t. naming. * Update src/functions_framework/__init__.py Co-authored-by: Dustin Ingram <[email protected]> * Fix enum per reviewer suggestion. * Rename text event => strucuture event. Co-authored-by: Joel Gerard <[email protected]> Co-authored-by: Dustin Ingram <[email protected]>
- Loading branch information
1 parent
32bba7d
commit eddbc7e
Showing
16 changed files
with
599 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Python Functions Frameworks Examples | ||
|
||
* [`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 | ||
* [`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 | ||
* [`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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 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 cloudevents functions-framework | ||
RUN pip install -r requirements.txt | ||
|
||
# Run the web service on container startup. | ||
CMD exec functions-framework --target=hello --signature-type=cloudevent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Deploying a CloudEvent function to Cloud Run with the Functions Framework | ||
This sample uses the [Cloud Events SDK](https://github.com/cloudevents/sdk-python) to send and receive a CloudEvent on Cloud Run. | ||
|
||
## How to run this locally | ||
Build the Docker image: | ||
|
||
```commandline | ||
docker build --tag ff_example . | ||
``` | ||
|
||
Run the image and bind the correct ports: | ||
|
||
```commandline | ||
docker run -p:8080:8080 ff_example | ||
``` | ||
|
||
Send an event to the container: | ||
|
||
```python | ||
from cloudevents.sdk import converters | ||
from cloudevents.sdk import marshaller | ||
from cloudevents.sdk.converters import structured | ||
from cloudevents.sdk.event import v1 | ||
import requests | ||
import json | ||
|
||
def run_structured(event, url): | ||
http_marshaller = marshaller.NewDefaultHTTPMarshaller() | ||
structured_headers, structured_data = http_marshaller.ToRequest( | ||
event, converters.TypeStructured, json.dumps | ||
) | ||
print("structured CloudEvent") | ||
print(structured_data.getvalue()) | ||
|
||
response = requests.post(url, | ||
headers=structured_headers, | ||
data=structured_data.getvalue()) | ||
response.raise_for_status() | ||
|
||
event = ( | ||
v1.Event() | ||
.SetContentType("application/json") | ||
.SetData('{"name":"john"}') | ||
.SetEventID("my-id") | ||
.SetSource("from-galaxy-far-far-away") | ||
.SetEventTime("tomorrow") | ||
.SetEventType("cloudevent.greet.you") | ||
) | ||
|
||
run_structured(event, "http://0.0.0.0:8080/") | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 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. | ||
|
||
# This sample creates a function that accepts a Cloud Event per | ||
# https://github.com/cloudevents/sdk-python | ||
import sys | ||
|
||
|
||
def hello(cloudevent): | ||
print("Received event with ID: %s" % cloudevent.EventID(), file=sys.stdout, flush=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Optionally include additional dependencies here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Google Cloud Functions Events Example | ||
This example demonstrates how to write an event function. Note that you can also use [CloudEvents](https://github.com/cloudevents/sdk-python) | ||
([example](../cloud_run_cloudevents)), which is a different construct. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.