def make_response(data): """ Creates a 200-status JSON response with the provided data and returns it to the caller :data the data to include in the response """ response = { "meta": { 'status': 200 }, "data": data } response = respond((json.dumps(response), 200)) response.headers['Content-Type'] = 'application/json' return response
def make_error(errors): """ Creates a JSON response with the provided errors. The status is determined by the highest code in the provided list of errors. The response object is returned to the caller :errors a single error or list of errors """ error_list = [] if type(errors) is not list: errors = [errors] for error in errors: error_list.append(error.to_dict()) status = max(error.status for error in errors) response = { "meta": { 'status': status }, "errors": error_list } response = respond((json.dumps(response), status)) response.headers['Content-Type'] = 'application/json' return response
def created(data): return respond(data, 201)
def ok(data): return respond(data, 200)
def not_allowed(method): return respond({'method': method}, 405)
def no_content(): return respond('', 204)