def formatted_response(response, fields, labels, iterable=None):
 output_format = validated_parameter(request.args, 'format', values=['json', 'json_lines', 'csv'], default='json')
 header_type = validated_parameter(request.args, 'header', values=['names', 'labels', 'none'], default='labels')
 if (header_type == 'names'):
  header = fields
 elif (header_type == 'labels'):
  header = labels
 else:
  header = None
 iterable = (iterable or response)
 if (output_format == 'json'):
  return jsonify(response)
 elif (output_format == 'json_lines'):
  return Response(JSONLinesGenerator(iterable), mimetype='application/x-json-lines')
 elif (output_format == 'csv'):
  generator = csv_generator(iterable, fields, include_header=bool(header), header=header)
  headers = {'Content-Disposition': 'attachment; filename="facts.csv"'}
  return Response(generator, mimetype='text/csv', headers=headers)

def formatted_response ( response , fields , labels , iterable = None ) : output_format = validated_parameter ( request . args , 'format' , values = [ 'json' , 'json_lines' , 'csv' ] , default = 'json' ) header_type = validated_parameter ( request . args , 'header' , values = [ 'names' , 'labels' , 'none' ] , default = 'labels' ) if ( header_type == 'names' ) : header = fields elif ( header_type == 'labels' ) : header = labels else : header = None iterable = ( iterable or response ) if ( output_format == 'json' ) : return jsonify ( response ) elif ( output_format == 'json_lines' ) : return Response ( JSONLinesGenerator ( iterable ) , mimetype = 'application/x-json-lines' ) elif ( output_format == 'csv' ) : generator = csv_generator ( iterable , fields , include_header = bool ( header ) , header = header ) headers = { 'Content-Disposition' : 'attachment; filename="facts.csv"' } return Response ( generator , mimetype = 'text/csv' , headers = headers )
wraps request which returns response that can be formatted .