-
Notifications
You must be signed in to change notification settings - Fork 0
/
newdemo.py
55 lines (45 loc) · 1.34 KB
/
newdemo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
import falcon
from falcon_apispec import FalconPlugin
from marshmallow import Schema, fields
# Optional marshmallow support
class CategorySchema(Schema):
id = fields.Int()
name = fields.Str(required=True)
class PetSchema(Schema):
category = fields.Nested(CategorySchema, many=True)
name = fields.Str()
# Create Falcon web app
app = falcon.API()
class RandomPetResource:
def on_get(self, req, resp):
"""A cute furry animal endpoint.
---
description: Get a random pet
responses:
200:
description: A pet to be returned
schema: PetSchema
"""
pet = get_random_pet() # returns JSON
resp.media = pet
# create instance of resource
random_pet_resource = RandomPetResource()
# pass into `add_route` for Falcon
app.add_route("/random", random_pet_resource)
# Create an APISpec
spec = APISpec(
title='Swagger Petstore',
version='1.0.0',
openapi_version='2.0',
plugins=[
FalconPlugin(app),
MarshmallowPlugin(),
],
)
# Register entities and paths
spec.components.schema('Category', schema=CategorySchema)
spec.components.schema('Pet', schema=PetSchema)
# pass created resource into `path` for APISpec
spec.path(resource=random_pet_resource)