| 1 |
""" |
| 2 |
In this example `openapi` version is used instead of `swagger` version. |
| 3 |
""" |
| 4 |
from flask import Flask, jsonify |
| 5 |
from flasgger import Swagger, swag_from |
| 6 |
|
| 7 |
app = Flask(__name__) |
| 8 |
|
| 9 |
swagger_config = { |
| 10 |
"headers": [], |
| 11 |
"specs": [ |
| 12 |
{ |
| 13 |
"endpoint": "swagger", |
| 14 |
"route": "/characteristics/swagger.json", |
| 15 |
"rule_filter": lambda rule: True, |
| 16 |
"model_filter": lambda tag: True, |
| 17 |
} |
| 18 |
], |
| 19 |
"title": "Product Characteristics APIs", |
| 20 |
"version": '', |
| 21 |
"termsOfService": "", |
| 22 |
"static_url_path": "/characteristics/static", |
| 23 |
"swagger_ui": True, |
| 24 |
"specs_route": "/characteristics/swagger/", |
| 25 |
"description": "", |
| 26 |
"securityDefinitions": { |
| 27 |
"oAuthSample": { |
| 28 |
"type": "oauth2", |
| 29 |
"flow": "application", |
| 30 |
"tokenUrl": "https://api.pgsmartshopassistant.com/o/token/", |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
colors_spec = { |
| 37 |
"tags": [ |
| 38 |
"colors" |
| 39 |
], |
| 40 |
"parameters": [ |
| 41 |
{ |
| 42 |
"name": "palette", |
| 43 |
"in": "path", |
| 44 |
"type": "string", |
| 45 |
"enum": [ |
| 46 |
"all", |
| 47 |
"rgb", |
| 48 |
"cmyk" |
| 49 |
], |
| 50 |
"required": True, |
| 51 |
"default": "all", |
| 52 |
"description": "Which palette to filter?" |
| 53 |
} |
| 54 |
], |
| 55 |
"operationId": "get_colors", |
| 56 |
"consumes": [ |
| 57 |
"application/json" |
| 58 |
], |
| 59 |
"produces": [ |
| 60 |
"application/json" |
| 61 |
], |
| 62 |
"security": { |
| 63 |
"colors_oauth": { |
| 64 |
"$ref": "#/securityDefinitions/oAuthSample" |
| 65 |
} |
| 66 |
}, |
| 67 |
"schemes": [ |
| 68 |
"http", |
| 69 |
"https" |
| 70 |
], |
| 71 |
"externalDocs": { |
| 72 |
"description": "Project repository", |
| 73 |
"url": "http://github.com/rochacbruno/flasgger" |
| 74 |
}, |
| 75 |
"definitions": { |
| 76 |
"Palette": { |
| 77 |
"type": "object", |
| 78 |
"properties": { |
| 79 |
"palette_name": { |
| 80 |
"type": "array", |
| 81 |
"items": { |
| 82 |
"$ref": "#/definitions/Color" |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
}, |
| 87 |
"Color": { |
| 88 |
"type": "string" |
| 89 |
} |
| 90 |
}, |
| 91 |
"responses": { |
| 92 |
"200": { |
| 93 |
"description": "A list of colors (may be filtered by palette)", |
| 94 |
"schema": { |
| 95 |
"$ref": "#/definitions/Palette" |
| 96 |
}, |
| 97 |
"examples": { |
| 98 |
"rgb": [ |
| 99 |
"red", |
| 100 |
"green", |
| 101 |
"blue" |
| 102 |
] |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
@app.route('/colors/<palette>/') |
| 110 |
@swag_from(colors_spec) |
| 111 |
def colors(palette): |
| 112 |
""" |
| 113 |
Example using a dictionary as specification |
| 114 |
This is the description |
| 115 |
You can also set 'summary' and 'description' in |
| 116 |
specs_dict |
| 117 |
--- |
| 118 |
# values here overrides the specs dict |
| 119 |
""" |
| 120 |
all_colors = { |
| 121 |
'cmyk': ['cyan', 'magenta', 'yellow', 'black'], |
| 122 |
'rgb': ['red', 'green', 'blue'] |
| 123 |
} |
| 124 |
if palette == 'all': |
| 125 |
result = all_colors |
| 126 |
else: |
| 127 |
result = {palette: all_colors.get(palette)} |
| 128 |
|
| 129 |
return jsonify(result) |
| 130 |
|
| 131 |
swag = Swagger(app, config=swagger_config) |
| 132 |
|
| 133 |
|
| 134 |
def test_swag(client, specs_data): |
| 135 |
""" |
| 136 |
This test is runs automatically in Travis CI |
| 137 |
|
| 138 |
:param client: Flask app test client |
| 139 |
:param specs_data: {'url': {swag_specs}} for every spec in app |
| 140 |
""" |
| 141 |
for spec in specs_data.values(): |
| 142 |
assert 'securityDefinitions' in spec |
| 143 |
assert 'oAuthSample' in spec['securityDefinitions'] |
| 144 |
|
| 145 |
|
| 146 |
if __name__ == '__main__': |
| 147 |
app.run(debug=True) |