Skip to content

Commit

Permalink
feat(examples): class based routing example
Browse files Browse the repository at this point in the history
  • Loading branch information
fMeow committed May 10, 2018
1 parent de21965 commit 9137de1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/class_based_view/blueprint.py
@@ -0,0 +1,20 @@
from sanic.blueprints import Blueprint
from sanic.views import HTTPMethodView
from sanic_openapi import doc
from sanic.response import json

blueprint = Blueprint('Class-based View', url_prefix='/class-based-view')


class MyView(HTTPMethodView):
@doc.summary("Get my view")
def get(self, request):
return json({"method": "GET"})

@doc.summary("Post my view")
@doc.consumes({"view": {"name": str}}, location="body")
def post(self, request):
return json({"method": "POST"})


blueprint.add_route(MyView.as_view(), '/view', strict_slashes=True)
27 changes: 27 additions & 0 deletions examples/class_based_view/main.py
@@ -0,0 +1,27 @@
from sanic_openapi import doc
from sanic_openapi import openapi_blueprint, swagger_blueprint
from sanic import Sanic
from sanic.response import json

from blueprint import blueprint

app = Sanic()

app.blueprint(openapi_blueprint)
app.blueprint(swagger_blueprint)
app.blueprint(blueprint)


@app.post("/plain", strict_slashes=True)
@doc.summary("Creates a user")
@doc.consumes({"user": {"name": str}}, location="body")
async def create_user(request):
return json({})


app.config.API_VERSION = 'pre-alpha'
app.config.API_TITLE = 'Class Based View Demonstration API'
app.config.API_TERMS_OF_SERVICE = 'Use with caution!'
app.config.API_CONTACT_EMAIL = 'guoli-lyu@hotmail.com'

app.run(host="0.0.0.0", debug=True)

0 comments on commit 9137de1

Please sign in to comment.