Skip to content

Commit

Permalink
Docs: Remove usage from README, Add documentation and contribution se…
Browse files Browse the repository at this point in the history
…ction in README
  • Loading branch information
chenjr0719 committed Aug 2, 2019
1 parent 6eb533d commit baf76ce
Showing 1 changed file with 17 additions and 139 deletions.
156 changes: 17 additions & 139 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,156 +29,34 @@ Your routes will be automatically categorized by their blueprints.

## Example

For an example Swagger UI, see the [Pet Store](http://petstore.swagger.io/)

## Usage

### Use simple decorators to document routes:

```python
from sanic_openapi import doc

@app.get("/user/<user_id:int>")
@doc.summary("Fetches a user by ID")
@doc.produces({ "user": { "name": str, "id": int } })
async def get_user(request, user_id):
...

@app.post("/user")
@doc.summary("Creates a user")
@doc.consumes(doc.JsonBody({"user": { "name": str }}), location="body")
async def create_user(request):
...
```

### Model your input/output

```python
class Car:
make = str
model = str
year = int

class Garage:
spaces = int
cars = [Car]

@app.get("/garage")
@doc.summary("Gets the whole garage")
@doc.produces(Garage)
async def get_garage(request):
return json({
"spaces": 2,
"cars": [{"make": "Nissan", "model": "370Z"}]
})

```

### Get more descriptive
Here is an example to use Sanic-OpenAPI:

```python
class Car:
make = doc.String("Who made the car")
model = doc.String("Type of car. This will vary by make")
year = doc.Integer("4-digit year of the car", required=False)

class Garage:
spaces = doc.Integer("How many cars can fit in the garage")
cars = doc.List(Car, description="All cars in the garage")
```
from sanic import Sanic
from sanic.response import json
from sanic_openapi import swagger_blueprint

### Specify a JSON body without extensive modelling
app = Sanic()
app.blueprint(swagger_blueprint)

```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
@app.route("/")
async def test(request):
return json({"hello": "world"})

```python
app.config.API_VERSION = '1.0.0'
app.config.API_TITLE = 'Car API'
app.config.API_DESCRIPTION = 'Car API'
app.config.API_TERMS_OF_SERVICE = 'Use with caution!'
app.config.API_PRODUCES_CONTENT_TYPES = ['application/json']
app.config.API_CONTACT_EMAIL = 'channelcat@gmail.com'
```

By default, Sanic registers URIs both with and without a trailing `/`. You may specify the type of the shown URIs by setting `app.config.API_URI_FILTER` to one of the following values:

- `all`: Include both types of URIs.
- `slash`: Only include URIs with a trailing `/`.
- All other values (and default): Only include URIs without a trailing `/`.

#### 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'
}
}
}
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)

```

### Set Swagger-UI configuration parameters
And you can get your Swagger document at <http://localhost:8000/swagger> like this:
![](docs/_static/images/hello_world_example.png)

Here you can set any configuration described in the swagger-ui documentation
## Documentation

```python
app.config.SWAGGER_UI_CONFIGURATION = {
'validatorUrl': None, # Disable Swagger validator
'displayRequestDuration': True,
'docExpansion': 'full'
}
```
Please check the documentation on [Readthedocs](https://sanic-openapi.readthedocs.io)

### Set responses for different HTTP status codes
## Contribution

```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)
```
Any contribution is welcome. If you don't know how to getting started, please check issues first and check our [Contributing Guide](CONTRIBUTING.md) to start you contribution.

0 comments on commit baf76ce

Please sign in to comment.