2025-05-27 15:46:31 +08:00

72 lines
2.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from flask import Blueprint, jsonify, request
from VortXDB.client import VTXClient
# 创建Blueprint
vtxdb_bp = Blueprint('vtxdb', __name__)
# 实例化数据库客户端
vtxdb = VTXClient(use_logger=False)
@vtxdb_bp.route("/get_vtxdb_data", methods=["POST"])
def get_vtxdb_data():
try:
# 获取请求体中的数据
data = request.json
table = data.get('table') # 从JSON中获取table
key = data.get('key') # 从JSON中获取key
if not table:
return jsonify({"status": "error", "message": "缺少表名参数 (table)"}), 400
if key:
data = vtxdb.get(table, key) # 获取指定table和key的数据
else:
data = vtxdb.get(table) # 如果没有key获取整个表的数据
# 允许data为None的情况,直接返回空数据
return jsonify({"status": "success", "data": data if data else None})
except Exception as e:
return jsonify({"status": "error", "message": f"获取数据失败: {str(e)}"}), 500
@vtxdb_bp.route("/set_vtxdb_data", methods=["POST"])
def set_vtxdb_data():
try:
# 获取请求体中的数据
data = request.json
table = data.get('table') # 从JSON中获取table
key = data.get('key') # 从JSON中获取key
item_data = data.get('item_data')
# 基本验证
if not table or not key or not item_data: # 如果缺少必要参数,返回错误
return jsonify({"status": "error", "message": "缺少必要参数"}), 400
# 保存数据到 vtxdb
vtxdb.set(table, key, item_data) # 将数据保存到指定的table和key
return jsonify({"status": "success", "message": "保存成功"})
except Exception as e:
return jsonify({"status": "error", "message": f"保存数据失败: {str(e)}"}), 500
@vtxdb_bp.route("/delete_vtxdb_data", methods=["POST"])
def delete_vtxdb_data():
try:
# 获取请求体中的数据
data = request.json
table = data.get('table') # 从JSON中获取table
key = data.get('key') # 从JSON中获取key
# 基本验证
if not table or not key: # 如果缺少必要参数,返回错误
return jsonify({"status": "error", "message": "缺少必要参数"}), 400
# 删除资源
vtxdb.delete(table, key) # 删除指定的table和key的记录
return jsonify({"status": "success", "message": "删除成功"})
except Exception as e:
return jsonify({"status": "error", "message": f"删除数据失败: {str(e)}"}), 500