aws / chalice

Python Serverless Microframework for AWS
Apache License 2.0
10.53k stars 1.01k forks source link

Feature request: Pluggable resourceful routing definition #1108

Open studio3104 opened 5 years ago

studio3104 commented 5 years ago

In current Chalice, we usually need to define some conditions if we make an endpoint handle various HTTP methods just like following,

@app.route('/', methods=['GET', 'POST'])
def index():
    method = app.current_request.method

    if method == 'GET':
        pass

    if method == 'POST':
        pass

However I guess it can lose readability in some cases And it would be worse if it needs to handle all CRUD with a single endpoint

Thus, althogh I would be happy if I can define an endpoint like below, what do you think about it? Do you have any plan to support pluggable or classified view definition?

@app.route('/')
class Index(View):
    def get(self):
        pass

    def post(self):
        pass

or

class Index(View):
    def get(self):
        pass

    def post(self):
        pass

app.register_route(Index, '/')

or both :) I prefer the latter one

This looks similar to https://github.com/aws/chalice/issues/487, but I guess slightly difference As of today, Chalice supports blueprints (as experimental API though), I think development efficiency should be improved if classified view definition and devided routing definition are supported as in the latter example

joguSD commented 5 years ago

This makes sense to me, marking as a feature request for class based views. Next steps would be to get a more formal proposal together as mentioned here.

studio3104 commented 5 years ago

@joguSD You mean that I just need to update this issue as following the guide, right? Or newly open an issue?

dmulter commented 5 years ago

If going down this route, I would suggest getting a feel for which of the existing Flask-RestXXX styles people like. I prefer the Flask-RESTPlus style personally. I think a resourceful style proposal would emulate one of these existing approaches.

dankingston commented 10 months ago

In case you're still interested, I've recently created an extension for chalice to add support for class-based views, amongst other things:

https://github.com/kingstonlabs/chalice-plus

from chalice_plus.views import RetrieveUpdateDeleteView, CreateListView

class BookDetailView(RetrieveUpdateDeleteView):
    model = Book
    schema_class = BookSchema

class BookListView(CreateListView):
    model = Book
    schema_class = BookSchema
urlpatterns = [
    ("/books", BookListView.as_view()),
    ("/books/{uuid:id}", BookDetailView.as_view()),
]

The views are customisable, so you can override methods to get the behaviour you want, or build from scratch using APIView.

There's quite a few other features, including field masking and permissions. It's available on pypi.