Skip to content

Cloud Run events samples should use sdk-python cloudevents #4531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cumason123 opened this issue Aug 20, 2020 · 0 comments · Fixed by #4537
Closed

Cloud Run events samples should use sdk-python cloudevents #4531

cumason123 opened this issue Aug 20, 2020 · 0 comments · Fixed by #4537
Assignees
Labels
api: run Issues related to the Cloud Run API. 🚨 This issue needs some love. samples Issues that are directly related to samples. triage me I really want to be triaged.

Comments

@cumason123
Copy link
Contributor

cumason123 commented Aug 20, 2020

In which file did you encounter the issue?

events-pubsub and events-storage

Describe the issue

These repo's are showing how to handle cloudevent wrapped HTTP requests in python. There exists a python library: sdk-python cloudevents which recently reached version 1.2.0 that handles this exact purpose.

e.g.:

Instead of

from flask import Flask, request


required_fields = ['Ce-Id', 'Ce-Source', 'Ce-Type', 'Ce-Specversion']
app = Flask(__name__)


@app.route('/', methods=['POST'])
def index():
    for field in required_fields:
        if field not in request.headers:
            errmsg = f'Bad Request: missing required header {field}'
            print(errmsg)
            return errmsg, 400

we could do this:

import cloudevents.exceptions as cloud_exceptions
from cloudevents.http import from_http

from flask import Flask, request


app = Flask(__name__)

@app.route('/', methods=['POST'])
def index():
    # Create CloudEvent from HTTP headers and body
    try:
        event = from_http(request.headers, request.get_data())

    except cloud_exceptions.MissingRequiredFields as e:
        print(f"cloudevents.exceptions.MissingRequiredFields: {e}")
        return "Failed to find all required cloudevent fields. ", 400
@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Aug 21, 2020
@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Aug 21, 2020
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Aug 25, 2020
@JustinBeckwith JustinBeckwith added the api: run Issues related to the Cloud Run API. label Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: run Issues related to the Cloud Run API. 🚨 This issue needs some love. samples Issues that are directly related to samples. triage me I really want to be triaged.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants