forked from sanic-org/sanic-openapi
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): class based routing example
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |