Skip to content

Add class-based view (MethodView) support#32

Merged
greyli merged 9 commits intomasterfrom
cbv
Apr 27, 2021
Merged

Add class-based view (MethodView) support#32
greyli merged 9 commits intomasterfrom
cbv

Conversation

@greyli
Copy link
Copy Markdown
Member

@greyli greyli commented Apr 24, 2021

This PR adds class-based view support. In particular, it will support to use @input, @output, @doc, @auth_required decorator on view methods of the MethodView class. Here is an example:

from apiflask import APIFlask, Schema, input, output
from flask.views import MethodView

app = APIFlask(__name__)


class PetInSchema(Schema):
    pass


class PetOutSchema(Schema):
    pass


@app.route('/pets/<int:pet_id>')
class Pet(MethodView):

    decorators = [auth_required(auth), doc(responses=[404])]

    @output(PetOutSchema)
    def get(self, pet_id):
        pass

    @output({}, 204)
    def delete(self, pet_id):
        pass

    @input(PetInSchema)
    @output(PetOutSchema)
    def put(self, pet_id, data):
        pass

    @input(PetInSchema(partial=True))
    @output(PetOutSchema)
    def patch(self, pet_id, data):
        pass

To collect the OpenAPI doc info stored on view methods, I rewritten Flask's app.route() decorator. Thus, the MethodView class should be decorated with @app.route() instead of using app.add_url_rule().

Checklist:

  • Add example application.
  • Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
  • Add or update relevant docs, in the docs folder and in code docstring.
  • Add an entry in CHANGES.md summarizing the change and linking to the issue and your username.
  • Add **Version Changed** or **Version Added** note in any relevant docs and docstring.
  • Run pytest and tox, no tests failed.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 24, 2021

Codecov Report

Merging #32 (c10a9b4) into master (d8c71cf) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #32   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           13        14    +1     
  Lines          702       787   +85     
  Branches       115       141   +26     
=========================================
+ Hits           702       787   +85     
Impacted Files Coverage Δ
apiflask/decorators.py 100.00% <ø> (ø)
apiflask/types.py 100.00% <ø> (ø)
apiflask/app.py 100.00% <100.00%> (ø)
apiflask/blueprint.py 100.00% <100.00%> (ø)
apiflask/openapi.py 100.00% <100.00%> (ø)
apiflask/route.py 100.00% <100.00%> (ø)
apiflask/utils.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d8c71cf...c10a9b4. Read the comment docs.

@greyli greyli merged commit 4fd27c7 into master Apr 27, 2021
@greyli greyli deleted the cbv branch April 27, 2021 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is there any plan for Class-Based-View?

2 participants