30
30
import os
31
31
import sys
32
32
import logging
33
+ from functools import wraps
33
34
from flask import Flask , jsonify , request , url_for , make_response , abort
34
35
from models import Pet , DataValidationError
35
36
@@ -93,6 +94,25 @@ def internal_server_error(error):
93
94
app .logger .info (message )
94
95
return jsonify (status = 500 , error = 'Internal Server Error' , message = message ), 500
95
96
97
+ ######################################################################
98
+ # DECORATORS
99
+ ######################################################################
100
+ def requires_content_type (* content_types ):
101
+ """ Use this decorator to check content type """
102
+ def decorator (func ):
103
+ """ Inner decorator """
104
+ @wraps (func )
105
+ def wrapper (* args , ** kwargs ):
106
+ """ Checks that the content type is correct """
107
+ for content_type in content_types :
108
+ if request .headers ['Content-Type' ] == content_type :
109
+ return func (* args , ** kwargs )
110
+
111
+ app .logger .error ('Invalid Content-Type: %s' , request .headers ['Content-Type' ])
112
+ abort (HTTP_415_UNSUPPORTED_MEDIA_TYPE ,
113
+ 'Content-Type must be {}' .format (content_types ))
114
+ return wrapper
115
+ return decorator
96
116
97
117
######################################################################
98
118
# GET INDEX
@@ -143,14 +163,14 @@ def get_pets(pet_id):
143
163
# ADD A NEW PET
144
164
######################################################################
145
165
@app .route ('/pets' , methods = ['POST' ])
166
+ @requires_content_type ('application/json' , 'application/x-www-form-urlencoded' )
146
167
def create_pets ():
147
168
"""
148
169
Creates a Pet
149
170
150
171
This endpoint will create a Pet based the data in the body that is posted
151
172
or data that is sent via an html form post.
152
173
"""
153
- check_content_type ('application/json' )
154
174
data = {}
155
175
# Check for form submission data
156
176
if request .headers .get ('Content-Type' ) == 'application/x-www-form-urlencoded' :
@@ -174,13 +194,13 @@ def create_pets():
174
194
# UPDATE AN EXISTING PET
175
195
######################################################################
176
196
@app .route ('/pets/<int:pet_id>' , methods = ['PUT' ])
197
+ @requires_content_type ('application/json' )
177
198
def update_pets (pet_id ):
178
199
"""
179
200
Update a Pet
180
201
181
202
This endpoint will update a Pet based the body that is posted
182
203
"""
183
- check_content_type ('application/json' )
184
204
pet = Pet .find (pet_id )
185
205
if not pet :
186
206
abort (HTTP_404_NOT_FOUND , "Pet with id '{}' was not found." .format (pet_id ))
@@ -240,12 +260,12 @@ def data_reset():
240
260
""" Removes all Pets from the database """
241
261
Pet .remove_all ()
242
262
243
- def check_content_type (content_type ):
244
- """ Checks that the media type is correct """
245
- if request .headers ['Content-Type' ] == content_type :
246
- return
247
- app .logger .error ('Invalid Content-Type: %s' , request .headers ['Content-Type' ])
248
- abort (HTTP_415_UNSUPPORTED_MEDIA_TYPE , 'Content-Type must be {}' .format (content_type ))
263
+ # def check_content_type(content_type):
264
+ # """ Checks that the media type is correct """
265
+ # if request.headers['Content-Type'] == content_type:
266
+ # return
267
+ # app.logger.error('Invalid Content-Type: %s', request.headers['Content-Type'])
268
+ # abort(HTTP_415_UNSUPPORTED_MEDIA_TYPE, 'Content-Type must be {}'.format(content_type))
249
269
250
270
# @app.before_first_request
251
271
def initialize_logging (log_level = logging .INFO ):
0 commit comments