Added API example to the method view docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 32c7e43 · Browse files · 1 file·+69 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+69 added−0 removed
- other:+69 added−0 removed
Provenance
- Commit
e68b34e- Tree
474f380- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
32c7e43
1 file changed+69 added−0 removed
modified:docs/views.rst+69 added
154 unmodified lines155 156 Due to the implicit self from the caller's perspective you cannot use157 regular view decorators on the individual methods of the view however,158 keep this in mind.159+160+Method Views for APIs161+---------------------162+163+Web APIs are often working very closely with HTTP verbs so it makes a lot164+of sense to implement such an API based on the165+:class:`~flask.views.MethodView`. That said, you will notice that the API166+will require different URL rules that go to the same method view most of167+the time. For instance consider that you are exposing a user object on168+the web:169+170+=============== =============== ======================================171+URL Method Description172+--------------- --------------- --------------------------------------173+``/users/`` ``GET`` Gives a list of all users174+``/users/`` ``POST`` Creates a new user175+``/users/<id>`` ``GET`` Shows a single user176+``/users/<id>`` ``PUT`` Updates a single user177+``/users/<id>`` ``DELETE`` Deletes a single user178+=============== =============== ======================================179+180+So how would you go about doing that with the181+:class:`~flask.views.MethodView`? The trick is to take advantage of the182+fact that you can provide multiple rules to the same view.183+184+Let's assume for the moment the view would look like this::185+186+ class UserAPI(MethodView):187+188+ def get(self, user_id):189+ if user_id is None:190+ # return a list of users191+ pass192+ else:193+ # expose a single user194+ pass195+196+ def post(self):197+ # create a new user198+ pass199+200+ def delete(self, user_id):201+ # delete a single user202+ pass203+204+ def put(self, user_id):205+ # update a single user206+ pass207+208+So how do we hook this up with the routing system? By adding two rules209+and explicitly mentioning the methods for each::210+211+ user_view = UserAPI.as_view('user_api')212+ app.add_url_rule('/users/', defaults={'user_id': None},213+ view_func=user_view, methods=['GET', 'POST'])214+ app.add_url_rule('/users/<int:user_id>', view_func=user_view,215+ methods=['GET', 'PUT', 'DELETE'])216+217+If you have a lot of APIs that look similar you can refactor that218+registration code::219+220+ def register_api(view, endpoint, url, pk='id', pk_type='int'):221+ view_func = view.as_view(endpoint)222+ app.add_url_rule(url, defaults={pk: None},223+ view_func=view_func, methods=['GET', 'POST'])224+ app.add_url_rule('%s<%s:%s>' % (url, pk), view_func=view_func,225+ methods=['GET', 'PUT', 'DELETE'])226+227+ register_api(UserAPI, 'user_api', '/users/', pk='user_id')