Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support and examples for Class-Based Views (HTTPMethodView) #64

Merged
merged 5 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/cars/blueprints/repair.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from sanic.views import HTTPMethodView
from sanic.blueprints import Blueprint
from sanic.response import json
from sanic_openapi import doc

blueprint = Blueprint('Repair', '/repair')
from models import Station
from data import test_station


class RepairStation(HTTPMethodView):
@doc.summary("Fetches all repair stations")
@doc.produces([Station])
def get(self, request):
return json([test_station])

@doc.summary("make an appointment")
@doc.description("submit necessary information for appointment")
def post(self, request):
return json(request.json)


blueprint.add_route(RepairStation.as_view(), "/station", strict_slashes=True)
8 changes: 7 additions & 1 deletion examples/cars/data.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from models import Car, Driver, Garage, Manufacturer, Status
from models import Car, Driver, Garage, Manufacturer, Status, Station
import datetime

test_manufacturer = Manufacturer()
test_driver = Driver()
test_car = Car()
test_garage = Garage()
test_status = Status()
test_station = Station()

test_manufacturer = {
'id': 1,
Expand Down Expand Up @@ -36,3 +37,8 @@
test_success = {
'success': True
}

test_station = {
'contact': 00000000,
'location': 'Seattle',
}
2 changes: 2 additions & 0 deletions examples/cars/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from blueprints.driver import blueprint as driver_blueprint
from blueprints.garage import blueprint as garage_blueprint
from blueprints.manufacturer import blueprint as manufacturer_blueprint
from blueprints.repair import blueprint as repair_blueprint

app = Sanic()

Expand All @@ -12,6 +13,7 @@
app.blueprint(driver_blueprint)
app.blueprint(garage_blueprint)
app.blueprint(manufacturer_blueprint)
app.blueprint(repair_blueprint)

app.config.API_VERSION = '1.0.0'
app.config.API_TITLE = 'Car API'
Expand Down
6 changes: 6 additions & 0 deletions examples/cars/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import date
from sanic_openapi import doc


class Manufacturer:
Expand Down Expand Up @@ -27,3 +28,8 @@ class Garage:

class Status:
success = bool


class Station:
location = doc.String("location")
contact = doc.Integer("phone number")
20 changes: 20 additions & 0 deletions examples/class_based_view/blueprint.py
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)
27 changes: 27 additions & 0 deletions examples/class_based_view/main.py
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)
40 changes: 30 additions & 10 deletions sanic_openapi/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def build_spec(app, loop):
# Authorization
# --------------------------------------------------------------- #

_spec['securityDefinitions'] = getattr(app.config, 'API_SECURITY_DEFINITIONS', None)
_spec['securityDefinitions'] = getattr(
app.config, 'API_SECURITY_DEFINITIONS', None)
_spec['security'] = getattr(app.config, 'API_SECURITY', None)

# --------------------------------------------------------------- #
Expand All @@ -78,10 +79,21 @@ def build_spec(app, loop):
for blueprint in app.blueprints.values():
if hasattr(blueprint, 'routes'):
for route in blueprint.routes:
route_spec = route_specs[route.handler]
route_spec.blueprint = blueprint
if not route_spec.tags:
route_spec.tags.append(blueprint.name)
if hasattr(route.handler, 'view_class'):
# class based view
view = route.handler.view_class
for http_method in route.methods:
_handler = getattr(view, http_method.lower(), None)
if _handler:
route_spec = route_specs[_handler]
route_spec.blueprint = blueprint
if not route_spec.tags:
route_spec.tags.append(blueprint.name)
else:
route_spec = route_specs[route.handler]
route_spec.blueprint = blueprint
if not route_spec.tags:
route_spec.tags.append(blueprint.name)

paths = {}
for uri, route in app.router.routes_all.items():
Expand All @@ -103,15 +115,21 @@ def build_spec(app, loop):

methods = {}
for _method, _handler in method_handlers:
route_spec = route_specs.get(_handler) or RouteSpec()
if hasattr(_handler, 'view_class'):
view_handler = getattr(_handler.view_class, _method.lower())
route_spec = route_specs.get(view_handler) or RouteSpec()
else:
route_spec = route_specs.get(_handler) or RouteSpec()

if _method == 'OPTIONS' or route_spec.exclude:
continue

api_consumes_content_types = getattr(app.config, 'API_CONSUMES_CONTENT_TYPES', ['application/json'])
api_consumes_content_types = getattr(
app.config, 'API_CONSUMES_CONTENT_TYPES', ['application/json'])
consumes_content_types = route_spec.consumes_content_type or api_consumes_content_types

api_produces_content_types = getattr(app.config, 'API_PRODUCES_CONTENT_TYPES', ['application/json'])
api_produces_content_types = getattr(
app.config, 'API_PRODUCES_CONTENT_TYPES', ['application/json'])
produces_content_types = route_spec.produces_content_type or api_produces_content_types

# Parameters - Path & Query String
Expand Down Expand Up @@ -176,15 +194,17 @@ def build_spec(app, loop):

uri_parsed = uri
for parameter in route.parameters:
uri_parsed = re.sub('<'+parameter.name+'.*?>', '{'+parameter.name+'}', uri_parsed)
uri_parsed = re.sub('<'+parameter.name+'.*?>',
'{'+parameter.name+'}', uri_parsed)

paths[uri_parsed] = methods

# --------------------------------------------------------------- #
# Definitions
# --------------------------------------------------------------- #

_spec['definitions'] = {obj.object_name: definition for cls, (obj, definition) in definitions.items()}
_spec['definitions'] = {
obj.object_name: definition for cls, (obj, definition) in definitions.items()}

# --------------------------------------------------------------- #
# Tags
Expand Down