def query(bucket_name): if request.method == 'OPTIONS': # OPTIONS requests are made by XHR as part of the CORS spec # if the client uses custom headers response = app.make_default_options_response() response.headers['Access-Control-Max-Age'] = '86400' response.headers['Access-Control-Allow-Headers'] = 'cache-control' else: result = validate_request_args(request.args, raw_queries_allowed(bucket_name)) if not result.is_valid: return log_error_and_respond(result.message, 400) bucket = Bucket(db, bucket_name) try: result_data = bucket.query(Query.parse(request.args)).data() except InvalidOperationError: return log_error_and_respond('invalid collect for that data', 400) # Taken from flask.helpers.jsonify to add JSONEncoder # NB. this can be removed once fix #471 works it's way into a release # https://github.com/mitsuhiko/flask/pull/471 json_data = json.dumps({"data": result_data}, cls=JsonEncoder, indent=None if request.is_xhr else 2) response = app.response_class(json_data, mimetype='application/json') # allow requests from any origin response.headers['Access-Control-Allow-Origin'] = '*' return response
def fetch(bucket_config): if bucket_config is None or not bucket_config.queryable: bname = "" if bucket_config is None else bucket_config.name + " " return log_error_and_respond( bname, 'bucket not found', 404) if request.method == 'OPTIONS': # OPTIONS requests are made by XHR as part of the CORS spec # if the client uses custom headers response = app.make_default_options_response() response.headers['Access-Control-Max-Age'] = '86400' response.headers['Access-Control-Allow-Headers'] = 'cache-control' else: result = validate_request_args(request.args, bucket_config.raw_queries_allowed) if not result.is_valid: return log_error_and_respond( bucket_config.name, result.message, 400) bucket = Bucket(db, bucket_config) try: query = Query.parse(request.args) data = bucket.query(query).data() except InvalidOperationError: return log_error_and_respond( bucket.name, 'invalid collect function', 400) response = jsonify(data=data) # allow requests from any origin response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Cache-Control'] = "max-age=%d, must-revalidate" % \ bucket_config.max_age return response
def query(bucket_name): result = validate_request_args(request.args, raw_queries_allowed(bucket_name)) if not result.is_valid: app.logger.error(result.message) return jsonify(status='error', message=result.message), 400 bucket = Bucket(db, bucket_name) result_data = bucket.query(Query.parse(request.args)).data() # Taken from flask.helpers.jsonify to add JSONEncoder # NB. this can be removed once fix #471 works it's way into a release # https://github.com/mitsuhiko/flask/pull/471 json_data = json.dumps({"data": result_data}, cls=JsonEncoder, indent=None if request.is_xhr else 2) response = app.response_class(json_data, mimetype='application/json') # allow requests from any origin response.headers['Access-Control-Allow-Origin'] = '*' return response