chalice icon indicating copy to clipboard operation
chalice copied to clipboard

Allow two functions app routes to differ only by HTTP method

Open BenWal opened this issue 10 years ago • 8 comments

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.

BenWal avatar Aug 01 '16 21:08 BenWal

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.

jamesls avatar Aug 01 '16 22:08 jamesls

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:.

siennathesane avatar Aug 15 '16 20:08 siennathesane

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?

corespace avatar Nov 16 '16 22:11 corespace

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!'

hadrien avatar Dec 02 '16 17:12 hadrien

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.

mrburrito avatar Jan 03 '17 16:01 mrburrito

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!

shenril avatar Jan 30 '17 07:01 shenril

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.

kyleknap avatar Jun 08 '17 23:06 kyleknap

I do not know if this is the best place to mention @kyleknap, but "Allow view functions share url with different method" seems not work when I set "host_matching=True" at Flask app init

s3341458 avatar Oct 26 '22 23:10 s3341458