| 1 |
""" |
| 2 |
Example of Flask RESTFul integration. |
| 3 |
requires: `pip install flask-restful` |
| 4 |
""" |
| 5 |
from flask import Flask |
| 6 |
from flask_restful import Api, Resource, abort, reqparse |
| 7 |
|
| 8 |
from flasgger import Swagger, swag_from |
| 9 |
|
| 10 |
app = Flask(__name__) |
| 11 |
api = Api(app) |
| 12 |
app.config['SWAGGER'] = { |
| 13 |
'title': 'Flasgger RESTful', |
| 14 |
'uiversion': 2 |
| 15 |
} |
| 16 |
swag = Swagger(app) |
| 17 |
|
| 18 |
|
| 19 |
TODOS = { |
| 20 |
'todo1': {'task': 'build an API'}, |
| 21 |
'todo2': {'task': '?????'}, |
| 22 |
'todo3': {'task': 'profit!'}, |
| 23 |
'42': {'task': 'Use Flasgger'} |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
def abort_if_todo_doesnt_exist(todo_id): |
| 28 |
if todo_id not in TODOS: |
| 29 |
abort(404, message="Todo {} doesn't exist".format(todo_id)) |
| 30 |
|
| 31 |
|
| 32 |
parser = reqparse.RequestParser() |
| 33 |
parser.add_argument('task') |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class Todo(Resource): |
| 39 |
def get(self, todo_id): |
| 40 |
""" |
| 41 |
This is an example |
| 42 |
--- |
| 43 |
tags: |
| 44 |
- restful |
| 45 |
parameters: |
| 46 |
- in: path |
| 47 |
name: todo_id |
| 48 |
required: true |
| 49 |
description: The ID of the task, try 42! |
| 50 |
type: string |
| 51 |
responses: |
| 52 |
200: |
| 53 |
description: The task data |
| 54 |
schema: |
| 55 |
id: Task |
| 56 |
properties: |
| 57 |
task: |
| 58 |
type: string |
| 59 |
default: My Task |
| 60 |
""" |
| 61 |
abort_if_todo_doesnt_exist(todo_id) |
| 62 |
return TODOS[todo_id] |
| 63 |
|
| 64 |
def delete(self, todo_id): |
| 65 |
""" |
| 66 |
This is an example |
| 67 |
--- |
| 68 |
tags: |
| 69 |
- restful |
| 70 |
parameters: |
| 71 |
- in: path |
| 72 |
name: todo_id |
| 73 |
required: true |
| 74 |
description: The ID of the task, try 42! |
| 75 |
type: string |
| 76 |
responses: |
| 77 |
204: |
| 78 |
description: Task deleted |
| 79 |
""" |
| 80 |
abort_if_todo_doesnt_exist(todo_id) |
| 81 |
del TODOS[todo_id] |
| 82 |
return '', 204 |
| 83 |
|
| 84 |
def put(self, todo_id): |
| 85 |
""" |
| 86 |
This is an example |
| 87 |
--- |
| 88 |
tags: |
| 89 |
- restful |
| 90 |
parameters: |
| 91 |
- in: body |
| 92 |
name: body |
| 93 |
schema: |
| 94 |
$ref: '#/definitions/Task' |
| 95 |
- in: path |
| 96 |
name: todo_id |
| 97 |
required: true |
| 98 |
description: The ID of the task, try 42! |
| 99 |
type: string |
| 100 |
responses: |
| 101 |
201: |
| 102 |
description: The task has been updated |
| 103 |
schema: |
| 104 |
$ref: '#/definitions/Task' |
| 105 |
""" |
| 106 |
args = parser.parse_args() |
| 107 |
task = {'task': args['task']} |
| 108 |
TODOS[todo_id] = task |
| 109 |
return task, 201 |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
class TodoList(Resource): |
| 115 |
def get(self): |
| 116 |
""" |
| 117 |
This is an example |
| 118 |
--- |
| 119 |
tags: |
| 120 |
- restful |
| 121 |
responses: |
| 122 |
200: |
| 123 |
description: The task data |
| 124 |
schema: |
| 125 |
id: Tasks |
| 126 |
properties: |
| 127 |
task_id: |
| 128 |
type: object |
| 129 |
schema: |
| 130 |
$ref: '#/definitions/Task' |
| 131 |
""" |
| 132 |
return TODOS |
| 133 |
|
| 134 |
def post(self): |
| 135 |
""" |
| 136 |
This is an example |
| 137 |
--- |
| 138 |
tags: |
| 139 |
- restful |
| 140 |
parameters: |
| 141 |
- in: body |
| 142 |
name: body |
| 143 |
schema: |
| 144 |
$ref: '#/definitions/Task' |
| 145 |
responses: |
| 146 |
201: |
| 147 |
description: The task has been created |
| 148 |
schema: |
| 149 |
$ref: '#/definitions/Task' |
| 150 |
""" |
| 151 |
args = parser.parse_args() |
| 152 |
todo_id = int(max(TODOS.keys()).lstrip('todo')) + 1 |
| 153 |
todo_id = 'todo%i' % todo_id |
| 154 |
TODOS[todo_id] = {'task': args['task']} |
| 155 |
return TODOS[todo_id], 201 |
| 156 |
|
| 157 |
|
| 158 |
class Username(Resource): |
| 159 |
@swag_from('username_specs.yml', methods=['GET']) |
| 160 |
def get(self, username): |
| 161 |
return {'username': username}, 200 |
| 162 |
|
| 163 |
|
| 164 |
api.add_resource(TodoList, '/todos') |
| 165 |
api.add_resource(Todo, '/todos/<todo_id>') |
| 166 |
api.add_resource(Username, '/username/<username>') |
| 167 |
|
| 168 |
|
| 169 |
if __name__ == '__main__': |
| 170 |
app.run(debug=True) |