def _write_to_bucket(bucket_config): if bucket_config is None: return jsonify(status="error", message='Could not find bucket_config'), 404 g.bucket_name = bucket_config.name try: auth_header = request.headers['Authorization'] except KeyError: return jsonify(status='error', message='Authorization header missing.'), 403 if not bearer_token_is_valid(bucket_config, auth_header): statsd.incr("write_api.bad_token", bucket=g.bucket_name) return jsonify(status='error', message='Forbidden'), 403 try: data = listify_json(request.json) bucket = Bucket(db, bucket_config) bucket.parse_and_store(data) return jsonify(status='ok') except (ParseError, ValidationError) as e: return jsonify(status="error", message=str(e)), 400
def _record_write_error(e): app.logger.exception(e) name_or_path = getattr(g, 'data_set_name', request.path) statsd.incr("write.error", data_set=name_or_path) if getattr(e, 'code', None) == 401: statsd.incr("write_api.bad_token", data_set=name_or_path)
def exception_handler(e): app.logger.exception(e) bucket_name = getattr(g, 'bucket_name', request.path) statsd.incr("write.error", bucket=bucket_name) code = getattr(e, 'code', 500) name = getattr(e, 'name', "Internal Error") return jsonify(status='error', message=name), code
def save(self, obj, tries=3): try: self._collection.save(obj) except AutoReconnect: logging.warning("AutoReconnect on save") statsd.incr("db.AutoReconnect", bucket=self._collection.name) if tries > 1: self.save(obj, tries - 1) else: raise
def post_to_bucket(bucket_name): g.bucket_name = bucket_name tokens = app.config['TOKENS'] auth_header = request.headers.get('Authorization', None) if not bearer_token_is_valid(tokens, auth_header, bucket_name): statsd.incr("write_api.bad_token", bucket=g.bucket_name) return jsonify(status='error', message='Forbidden'), 403 try: data = load_json(request.json) bucket = Bucket(db, bucket_name) bucket.parse_and_store(data) return jsonify(status='ok') except (ParseError, ValidationError) as e: return jsonify(status="error", message=str(e)), 400