chalice icon indicating copy to clipboard operation
chalice copied to clipboard

Feature request: Pluggable resourceful routing definition

Open studio3104 opened this issue 5 years ago • 4 comments

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

studio3104 avatar Apr 16 '19 04:04 studio3104

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.

joguSD avatar Apr 17 '19 20:04 joguSD

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

studio3104 avatar Apr 20 '19 01:04 studio3104

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.

dmulter avatar Jul 10 '19 18:07 dmulter

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.

dankingston avatar Aug 21 '23 14:08 dankingston