Skip to content

Commit 1a6d136

Browse files
committed
Take care of some CORS issues to make the example more slick. Add Access-Control-Allow-Origin headers etc to the example
1 parent 3dce0ce commit 1a6d136

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

example.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,31 @@ def get(self, todo_id):
6868
# This goes into the summary
6969
"Get a todo task"
7070
abort_if_todo_doesnt_exist(todo_id)
71-
return TODOS[todo_id]
71+
return TODOS[todo_id], 200, {'Access-Control-Allow-Origin': '*'}
7272

7373
def delete(self, todo_id):
7474
abort_if_todo_doesnt_exist(todo_id)
7575
del TODOS[todo_id]
76-
return '', 204
76+
return '', 204, {'Access-Control-Allow-Origin': '*'}
7777

7878
def put(self, todo_id):
7979
args = parser.parse_args()
8080
task = {'task': args['task']}
8181
TODOS[todo_id] = task
82-
return task, 201
82+
return task, 201, {'Access-Control-Allow-Origin': '*'}
8383

84+
def options (self, **args):
85+
return {'Allow' : 'GET,PUT,POST,DELETE' }, 200, \
86+
{ 'Access-Control-Allow-Origin': '*', \
87+
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE', \
88+
'Access-Control-Allow-Headers': 'Content-Type' }
8489

8590
# TodoList
8691
# shows a list of all todos, and lets you POST to add new tasks
8792
class TodoList(Resource):
8893

8994
def get(self):
90-
return TODOS
95+
return TODOS, 200, {'Access-Control-Allow-Origin': '*'}
9196

9297
@swagger.operation(
9398
notes='Creates a new TODO item',
@@ -118,7 +123,7 @@ def post(self):
118123
args = parser.parse_args()
119124
todo_id = 'todo%d' % (len(TODOS) + 1)
120125
TODOS[todo_id] = {'task': args['task']}
121-
return TODOS[todo_id], 201
126+
return TODOS[todo_id], 201, {'Access-Control-Allow-Origin': '*'}
122127

123128
@swagger.model
124129
class ModelWithResourceFields:
@@ -158,8 +163,9 @@ class MarshalWithExample(Resource):
158163
responseClass=TodoItemWithResourceFields,
159164
nickname='get')
160165
@marshal_with(TodoItemWithResourceFields.resource_fields)
161-
def get(self):
162-
return {}
166+
def get(self, **kwargs):
167+
return {}, 200, {'Access-Control-Allow-Origin': '*'}
168+
163169

164170
##
165171
## Actually setup the Api resource routing here

0 commit comments

Comments
 (0)