-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
483 additions
and
119 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Get single item | ||
--- | ||
tags: | ||
- item | ||
parameters: | ||
- name: id | ||
type: integer | ||
in: path | ||
required: True | ||
minimum: 1 | ||
responses: | ||
200: | ||
description: OK | ||
schema: | ||
$ref: '#/definitions/item' |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Update single item | ||
--- | ||
tags: | ||
- item | ||
parameters: | ||
- name: id | ||
type: integer | ||
in: path | ||
required: True | ||
minimum: 1 | ||
- name: Item | ||
in: body | ||
schema: | ||
type: object | ||
required: | ||
- name | ||
properties: | ||
name: | ||
type: string | ||
maxLength: 31 | ||
description: global unique | ||
responses: | ||
200: | ||
description: OK | ||
schema: | ||
$ref: '#/definitions/Item' |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Get all items | ||
--- | ||
tags: | ||
- item | ||
parameters: | ||
- name: type | ||
type: string | ||
in: query | ||
required: True | ||
maxLength: 10 | ||
responses: | ||
200: | ||
description: OK | ||
schema: | ||
$ref: '#/definitions/Items' | ||
401: | ||
description: Auth required | ||
definitions: | ||
Items: | ||
type: object | ||
properties: | ||
objects: | ||
type: array | ||
items: | ||
$ref: "#/definitions/Item" |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Create item | ||
--- | ||
tags: | ||
- item | ||
parameters: | ||
- name: Item | ||
in: body | ||
schema: | ||
type: object | ||
required: | ||
- name | ||
- type | ||
properties: | ||
name: | ||
type: string | ||
maxLength: 31 | ||
description: global unique | ||
type: | ||
type: string | ||
maxLength: 10 | ||
responses: | ||
201: | ||
description: OK | ||
schema: | ||
$ref: '#/definitions/Item' |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
swagger: 2.0 | ||
info: | ||
title: 'ParsedMethodView Example' | ||
description: 'ParsedMethodView Example' | ||
version: 0.1 | ||
|
||
definitions: | ||
Item: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
name: | ||
type: string | ||
type: | ||
type: string |
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 |
---|---|---|
@@ -0,0 +1,132 @@ | ||
""" | ||
Example of auto load doc file, parse and validate | ||
""" | ||
import os | ||
|
||
from flask import Flask, request, jsonify, Response | ||
from flask.views import MethodView | ||
from flasgger import Swagger | ||
|
||
|
||
_TEST_META_SKIP_FULL_VALIDATION = True | ||
|
||
app = Flask(__name__) | ||
app.config['SWAGGER'] = { | ||
'title': 'Flasgger ParsedMethodView Example', | ||
'doc_dir': './examples/docs/' | ||
} | ||
swag = Swagger( | ||
app, | ||
template_file=os.path.join( | ||
os.getcwd(), 'examples', 'docs', 'template.yml'), | ||
parse=True) | ||
|
||
|
||
class ItemsView(MethodView): | ||
def get(self): | ||
return jsonify( | ||
[{'name': 'test', 'id': 1, 'type': request.parsed_data['args']['type']}, | ||
{'name': 'test2', 'id': 2, 'type': request.parsed_data['args']['type']}]) | ||
|
||
def post(self): | ||
return jsonify( | ||
{'name': request.parsed_data['json']['name'], 'id': 3, | ||
'type': request.parsed_data['json']['type']}) | ||
|
||
|
||
class ItemMethodView(MethodView): | ||
def get(self, id): | ||
return jsonify({'name': 'test', 'id': id, 'type': 'NORMAL'}) | ||
|
||
def put(self, id): | ||
return jsonify( | ||
{'name': request.parsed_data['json']['name'], 'id': 3, 'type': 'NORMAL'}) | ||
|
||
|
||
class EmptyView(MethodView): | ||
"""In this view, we do not provide api doc""" | ||
def get(self): | ||
return Response(status=200) | ||
|
||
|
||
@app.route('/api/auth/', methods=['POST']) | ||
def auth(): | ||
"""Auth | ||
--- | ||
parameters: | ||
- name: Item | ||
in: body | ||
schema: | ||
type: object | ||
required: | ||
- name | ||
- code | ||
properties: | ||
name: | ||
type: string | ||
maxLength: 10 | ||
code: | ||
type: string | ||
definitions: | ||
User: | ||
type: object | ||
properties: | ||
id: | ||
type: int | ||
name: | ||
type: string | ||
responses: | ||
200: | ||
schema: | ||
$ref: '#/definitions/User' | ||
examples: | ||
{'id': 1, 'name': 'test'} | ||
404: | ||
description: Auth failed | ||
400: | ||
description: Miss name or code | ||
""" | ||
return jsonify({'id': 1, 'name': request.parsed_data['json']['name']}) | ||
|
||
|
||
app.add_url_rule( | ||
'/api/items/', view_func=ItemsView.as_view(name='items'), | ||
methods=['GET', 'POST']) | ||
app.add_url_rule( | ||
'/api/items/<int:id>/', view_func=ItemMethodView.as_view(name='item'), | ||
methods=['GET', 'PUT']) | ||
app.add_url_rule( | ||
'/api/empty/', view_func=EmptyView.as_view(name='empty'), | ||
methods=['GET']) | ||
|
||
|
||
def test_swag(client, specs_data): | ||
""" | ||
This test is runs automatically in Travis CI | ||
:param client: Flask app test client | ||
:param specs_data: {'url': {swag_specs}} for every spec in app | ||
""" | ||
res = client.get('/api/items/') | ||
assert res.status_code == 400 | ||
res = client.get('/api/items/?type=myLengthIsTooLong') | ||
assert res.status_code == 400 | ||
res = client.get('/api/items/?type=NORMAL') | ||
assert res.status_code == 200 | ||
res = client.get('/api/items/1/') | ||
assert res.status_code == 200 | ||
res = client.put('/api/items/1/') | ||
assert res.status_code == 400 | ||
res = client.put('/api/items/1/', json={'name': 'test'}) | ||
assert res.status_code == 200 | ||
res = client.post('/api/auth/', json={'name': 'miss code'}) | ||
assert res.status_code == 400 | ||
res = client.post( | ||
'/api/auth/', json={'name': 'myLengthIsTooLong', 'code': 'test'}) | ||
assert res.status_code == 400 | ||
res = client.post('/api/auth/', json={'name': 'test', 'code': 'test'}) | ||
assert res.status_code == 200 | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |
Oops, something went wrong.