-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from DirkGuijt/master
sanic-openapi 0.5.0; many fixes, see description
- Loading branch information
Showing
20 changed files
with
463 additions
and
381 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 |
---|---|---|
|
@@ -14,16 +14,15 @@ Give your Sanic API a UI and OpenAPI documentation, all for the price of free! | |
pip install sanic-openapi | ||
``` | ||
|
||
Add OpenAPI and Swagger UI: | ||
Add Swagger UI with the OpenAPI spec: | ||
|
||
```python | ||
from sanic_openapi import swagger_blueprint, openapi_blueprint | ||
from sanic_openapi import swagger_blueprint | ||
|
||
app.blueprint(openapi_blueprint) | ||
app.blueprint(swagger_blueprint) | ||
``` | ||
|
||
You'll now have a Swagger UI at the URL `/swagger`. | ||
You'll now have a Swagger UI at the URL `/swagger` and an OpenAPI 2.0 spec at `/swagger/swagger.json`. | ||
Your routes will be automatically categorized by their blueprints. | ||
|
||
## Example | ||
|
@@ -45,7 +44,7 @@ async def get_user(request, user_id): | |
|
||
@app.post("/user") | ||
@doc.summary("Creates a user") | ||
@doc.consumes({"user": { "name": str }}, location="body") | ||
@doc.consumes(doc.JsonBody({"user": { "name": str }}), location="body") | ||
async def create_user(request): | ||
... | ||
``` | ||
|
@@ -86,6 +85,29 @@ class Garage: | |
cars = doc.List(Car, description="All cars in the garage") | ||
``` | ||
|
||
### Specify a JSON body without extensive modelling | ||
|
||
```python | ||
garage = doc.JsonBody({ | ||
"spaces": doc.Integer, | ||
"cars": [ | ||
{ | ||
"make": doc.String, | ||
"model": doc.String, | ||
"year": doc.Integer | ||
} | ||
] | ||
}) | ||
|
||
@app.post("/store/garage") | ||
@doc.summary("Stores a garage object") | ||
@doc.consumes(garage, content_type="application/json", location="body") | ||
async def store_garage(request): | ||
store_garage(request.json) | ||
return json(request.json) | ||
``` | ||
|
||
|
||
### Configure all the things | ||
|
||
```python | ||
|
@@ -96,3 +118,48 @@ app.config.API_TERMS_OF_SERVICE = 'Use with caution!' | |
app.config.API_PRODUCES_CONTENT_TYPES = ['application/json'] | ||
app.config.API_CONTACT_EMAIL = '[email protected]' | ||
``` | ||
|
||
#### Including OpenAPI's host, basePath and security parameters | ||
|
||
Just follow the OpenAPI 2.0 specification on this | ||
|
||
``` python | ||
app.config.API_HOST = 'subdomain.host.ext' | ||
app.config.API_BASEPATH = '/v2/api/' | ||
|
||
app.config.API_SECURITY = [ | ||
{ | ||
'authToken': [] | ||
} | ||
] | ||
|
||
app.config.API_SECURITY_DEFINITIONS = { | ||
'authToken': { | ||
'type': 'apiKey', | ||
'in': 'header', | ||
'name': 'Authorization', | ||
'description': 'Paste your auth token and do not forget to add "Bearer " in front of it' | ||
}, | ||
'OAuth2': { | ||
'type': 'oauth2', | ||
'flow': 'application', | ||
'tokenUrl': 'https://your.authserver.ext/v1/token', | ||
'scopes': { | ||
'some_scope': 'Grants access to this API' | ||
} | ||
} | ||
} | ||
|
||
``` | ||
|
||
### Set responses for different HTTP status codes | ||
|
||
```python | ||
@app.get("/garage/<id>") | ||
@doc.summary("Gets the whole garage") | ||
@doc.produces(Garage) | ||
@doc.response(404, {"message": str}, description="When the garage cannot be found") | ||
async def get_garage(request, id): | ||
garage = some_fetch_function(id) | ||
return json(garage) | ||
``` |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
sanic==0.6.0 | ||
sanic==0.7.0 | ||
tox==2.7.0 |
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
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from .openapi import blueprint as openapi_blueprint | ||
from .swagger import blueprint as swagger_blueprint | ||
|
||
__version__ = '0.4.0' | ||
__all__ = ['openapi_blueprint', 'swagger_blueprint'] | ||
__version__ = '0.5.0' | ||
__all__ = ['swagger_blueprint'] |
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
Oops, something went wrong.