Chalice: Allow two functions app routes to differ only by HTTP method

Created on 1 Aug 2016  路  7Comments  路  Source: aws/chalice

I want to have my GET and PUT implementation in different functions, but have the URLs have a consistent format for the same resource. When I try to define two functions with the same path in their route but different methods defined, I get API Gateway's 403 error message.

needs-discussion

Most helpful comment

IMHO, this is flask compliant and should be the way to do in chalice:

from flask import Flask
app = Flask(__name__)


@app.route('/', methods=['GET'])
def hello_get():
    return 'Hello, Get!'


@app.route('/', methods=['POST'])
def hello_post():
    return 'Hello, Post!'

All 7 comments

I recently added a fix to give validation errors when you try to deploy an app with duplicate routes (#80).

My hesitation in supporting this is that it deviates from how flask's API supports this, and I think there's a lot of value in trying to stay as close to the Flask API as possible, so people don't have to relearn a new set of APIs when using chalice.

FWIW, this is also similar to how it's done in django if you're not using class-based views: request.method and require_http_methods

I've marked this as "needs-discussion" and I'll leave this open for a while. If others feel strongly about this, feel free to chime in or +1/-1 the issue.

Personally, the closer to Flask, the better, IMHO. There are already a lot of APIs which exist as part of AWS, so learning as little as possible to easily leverage them speeds time to production by lowering developmental overhead.

Just my 2 :dollar:.

The obvious downside to having to combine multiple method handlers into one python method is the "uglyness" of an if block based on request method. This is less-than-ideal, but hardly a show-stopper.

More interesting downsides will increase in number as the @route annotation increases in power/expressiveness. For example, can I have a PUT that only accepts application/json, but a DELETE that accepts no content-type?

IMHO, this is flask compliant and should be the way to do in chalice:

from flask import Flask
app = Flask(__name__)


@app.route('/', methods=['GET'])
def hello_get():
    return 'Hello, Get!'


@app.route('/', methods=['POST'])
def hello_post():
    return 'Hello, Post!'

I would vote for allowing separate handlers based on the unique combination of path+method+content-type. That would lend itself to the cleanest code and minimization of if/else blocks in the handler functions.

That would be greatly appreciated regarding api key handling too !
Read a resource with GET without key
and create new element with POST using an api key!

Closing this out. It was implemented in this PR: https://github.com/awslabs/chalice/pull/375. It will be available in the next chalice release.

Was this page helpful?
0 / 5 - 0 ratings