Skip to content
Myr

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

1 file+69 added−0 removed

modified:docs/views.rst+69 added
154 unmodified lines
155156Due to the implicit self from the caller's perspective you cannot use157regular view decorators on the individual methods of the view however,158keep this in mind.159160Method Views for APIs161---------------------162163Web APIs are often working very closely with HTTP verbs so it makes a lot164of sense to implement such an API based on the165:class:`~flask.views.MethodView`. That said, you will notice that the API166will require different URL rules that go to the same method view most of167the time. For instance consider that you are exposing a user object on168the web:169170=============== =============== ======================================171URL 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=============== =============== ======================================179180So how would you go about doing that with the181:class:`~flask.views.MethodView`? The trick is to take advantage of the182fact that you can provide multiple rules to the same view.183184Let's assume for the moment the view would look like this::185186 class UserAPI(MethodView):187188 def get(self, user_id):189 if user_id is None:190 # return a list of users191 pass192 else:193 # expose a single user194 pass195196 def post(self):197 # create a new user198 pass199200 def delete(self, user_id):201 # delete a single user202 pass203204 def put(self, user_id):205 # update a single user206 pass207208So how do we hook this up with the routing system? By adding two rules209and explicitly mentioning the methods for each::210211 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'])216217If you have a lot of APIs that look similar you can refactor that218registration code::219220 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'])226227 register_api(UserAPI, 'user_api', '/users/', pk='user_id')