forked from nyu-devops/lab-travis-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
300 lines (263 loc) · 10.6 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright 2016, 2017 John J. Rofrano. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Pet API Controller
This modules provides a REST API for the Pet Model
Paths:
------
GET /pets - Lists all of the Pets
GET /pets/{id} - Retrieves a single Pet with the specified id
POST /pets - Creates a new Pet
PUT /pets/{id} - Updates a single Pet with the specified id
DELETE /pets/{id} - Deletes a single Pet with the specified id
POST /pets/{id}/purchase - Action to purchase a Pet
"""
import os
import sys
import logging
from functools import wraps
from flask import Flask, jsonify, request, url_for, make_response, abort
from models import Pet, DataValidationError
# Create Flask application
app = Flask(__name__)
app.config['LOGGING_LEVEL'] = logging.INFO
# Pull options from environment
DEBUG = (os.getenv('DEBUG', 'False') == 'True')
PORT = os.getenv('PORT', '5000')
# Status Codes
HTTP_200_OK = 200
HTTP_201_CREATED = 201
HTTP_204_NO_CONTENT = 204
HTTP_400_BAD_REQUEST = 400
HTTP_404_NOT_FOUND = 404
HTTP_409_CONFLICT = 409
HTTP_415_UNSUPPORTED_MEDIA_TYPE = 415
######################################################################
# Error Handlers
######################################################################
@app.errorhandler(DataValidationError)
def request_validation_error(error):
""" Handles Value Errors from bad data """
return bad_request(error)
@app.errorhandler(400)
def bad_request(error):
""" Handles bad reuests with 400_BAD_REQUEST """
message = error.message or str(error)
app.logger.info(message)
return jsonify(status=400, error='Bad Request', message=message), 400
@app.errorhandler(404)
def not_found(error):
""" Handles resources not found with 404_NOT_FOUND """
message = error.message or str(error)
app.logger.info(message)
return jsonify(status=404, error='Not Found', message=message), 404
@app.errorhandler(405)
def method_not_supported(error):
""" Handles unsuppoted HTTP methods with 405_METHOD_NOT_SUPPORTED """
message = error.message or str(error)
app.logger.info(message)
return jsonify(status=405, error='Method not Allowed', message=message), 405
@app.errorhandler(415)
def mediatype_not_supported(error):
""" Handles unsuppoted media requests with 415_UNSUPPORTED_MEDIA_TYPE """
message = error.message or str(error)
app.logger.info(message)
return jsonify(status=415, error='Unsupported media type', message=message), 415
@app.errorhandler(500)
def internal_server_error(error):
""" Handles unexpected server error with 500_SERVER_ERROR """
message = error.message or str(error)
app.logger.info(message)
return jsonify(status=500, error='Internal Server Error', message=message), 500
######################################################################
# DECORATORS
######################################################################
def requires_content_type(*content_types):
""" Use this decorator to check content type """
def decorator(func):
""" Inner decorator """
@wraps(func)
def wrapper(*args, **kwargs):
""" Checks that the content type is correct """
for content_type in content_types:
if request.headers['Content-Type'] == content_type:
return func(*args, **kwargs)
app.logger.error('Invalid Content-Type: %s', request.headers['Content-Type'])
abort(HTTP_415_UNSUPPORTED_MEDIA_TYPE,
'Content-Type must be {}'.format(content_types))
return wrapper
return decorator
######################################################################
# GET INDEX
######################################################################
@app.route('/')
def index():
""" Send back the home page """
return app.send_static_file('index.html')
######################################################################
# LIST ALL PETS
######################################################################
@app.route('/pets', methods=['GET'])
def list_pets():
""" Returns all of the Pets """
pets = []
category = request.args.get('category')
name = request.args.get('name')
available = request.args.get('available')
if category:
pets = Pet.find_by_category(category)
elif name:
pets = Pet.find_by_name(name)
elif available:
pets = Pet.find_by_availability(available)
else:
pets = Pet.all()
results = [pet.serialize() for pet in pets]
return make_response(jsonify(results), HTTP_200_OK)
######################################################################
# RETRIEVE A PET
######################################################################
@app.route('/pets/<int:pet_id>', methods=['GET'])
def get_pets(pet_id):
"""
Retrieve a single Pet
This endpoint will return a Pet based on it's id
"""
pet = Pet.find(pet_id)
if not pet:
abort(HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id))
return make_response(jsonify(pet.serialize()), HTTP_200_OK)
######################################################################
# ADD A NEW PET
######################################################################
@app.route('/pets', methods=['POST'])
@requires_content_type('application/json', 'application/x-www-form-urlencoded')
def create_pets():
"""
Creates a Pet
This endpoint will create a Pet based the data in the body that is posted
or data that is sent via an html form post.
"""
data = {}
# Check for form submission data
if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded':
app.logger.info('Processing FORM data')
data = {
'name': request.form['name'],
'category': request.form['category'],
'available': request.form['available'].lower() in ['true', '1', 't']
}
else:
app.logger.info('Processing JSON data')
data = request.get_json()
pet = Pet()
pet.deserialize(data)
pet.save()
message = pet.serialize()
return make_response(jsonify(message), HTTP_201_CREATED,
{'Location': url_for('get_pets', pet_id=pet.id, _external=True)})
######################################################################
# UPDATE AN EXISTING PET
######################################################################
@app.route('/pets/<int:pet_id>', methods=['PUT'])
@requires_content_type('application/json')
def update_pets(pet_id):
"""
Update a Pet
This endpoint will update a Pet based the body that is posted
"""
pet = Pet.find(pet_id)
if not pet:
abort(HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id))
pet.deserialize(request.get_json())
pet.id = pet_id
pet.save()
return make_response(jsonify(pet.serialize()), HTTP_200_OK)
######################################################################
# DELETE A PET
######################################################################
@app.route('/pets/<int:pet_id>', methods=['DELETE'])
def delete_pets(pet_id):
"""
Delete a Pet
This endpoint will delete a Pet based the id specified in the path
"""
pet = Pet.find(pet_id)
if pet:
pet.delete()
return make_response('', HTTP_204_NO_CONTENT)
######################################################################
# PURCHASE A PET
######################################################################
@app.route('/pets/<int:pet_id>/purchase', methods=['PUT'])
def purchase_pets(pet_id):
""" Purchase a Pet """
pet = Pet.find(pet_id)
if not pet:
abort(HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id))
if not pet.available:
abort(HTTP_400_BAD_REQUEST, "Pet with id '{}' is not available.".format(pet_id))
pet.available = False
pet.save()
return make_response(jsonify(pet.serialize()), HTTP_200_OK)
######################################################################
# U T I L I T Y F U N C T I O N S
######################################################################
@app.before_first_request
def init_db(redis=None):
""" Initlaize the model """
Pet.init_db(redis)
# load sample data
def data_load(payload):
""" Loads a Pet into the database """
pet = Pet(0, payload['name'], payload['category'])
pet.save()
def data_reset():
""" Removes all Pets from the database """
Pet.remove_all()
# def check_content_type(content_type):
# """ Checks that the media type is correct """
# if request.headers['Content-Type'] == content_type:
# return
# app.logger.error('Invalid Content-Type: %s', request.headers['Content-Type'])
# abort(HTTP_415_UNSUPPORTED_MEDIA_TYPE, 'Content-Type must be {}'.format(content_type))
# @app.before_first_request
def initialize_logging(log_level=logging.INFO):
""" Initialized the default logging to STDOUT """
if not app.debug:
print 'Setting up logging...'
# Set up default logging for submodules to use STDOUT
# datefmt='%m/%d/%Y %I:%M:%S %p'
fmt = '[%(asctime)s] %(levelname)s in %(module)s: %(message)s'
logging.basicConfig(stream=sys.stdout, level=log_level, format=fmt)
# Make a new log handler that uses STDOUT
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter(fmt))
handler.setLevel(log_level)
# Remove the Flask default handlers and use our own
handler_list = list(app.logger.handlers)
for log_handler in handler_list:
app.logger.removeHandler(log_handler)
app.logger.addHandler(handler)
app.logger.setLevel(log_level)
app.logger.info('Logging handler established')
######################################################################
# M A I N
######################################################################
if __name__ == "__main__":
print "************************************************************"
print " P E T R E S T A P I S E R V I C E "
print "************************************************************"
initialize_logging(app.config['LOGGING_LEVEL'])
app.run(host='0.0.0.0', port=int(PORT), debug=DEBUG)