116 lines
2.9 KiB
Python
116 lines
2.9 KiB
Python
"""
|
|
客户端管理 API 蓝图
|
|
"""
|
|
from flask import Blueprint, request, jsonify
|
|
from services.client_service import ClientService
|
|
|
|
clients_bp = Blueprint('clients', __name__, url_prefix='/api/clients')
|
|
client_service = ClientService()
|
|
|
|
|
|
@clients_bp.route('', methods=['GET'])
|
|
def get_clients():
|
|
"""获取所有客户端列表"""
|
|
try:
|
|
clients = client_service.get_clients_with_connection()
|
|
return jsonify({
|
|
'success': True,
|
|
'data': clients
|
|
})
|
|
except Exception as e:
|
|
return jsonify({
|
|
'success': False,
|
|
'error': str(e)
|
|
}), 500
|
|
|
|
|
|
@clients_bp.route('', methods=['POST'])
|
|
def add_client():
|
|
"""添加新客户端"""
|
|
try:
|
|
data = request.get_json()
|
|
|
|
# 验证必需字段
|
|
required_fields = ['name', 'host', 'port']
|
|
for field in required_fields:
|
|
if field not in data:
|
|
return jsonify({
|
|
'success': False,
|
|
'error': f'缺少必需字段: {field}'
|
|
}), 400
|
|
|
|
client = client_service.add_client(data)
|
|
return jsonify({
|
|
'success': True,
|
|
'data': client
|
|
}), 201
|
|
|
|
except Exception as e:
|
|
return jsonify({
|
|
'success': False,
|
|
'error': str(e)
|
|
}), 500
|
|
|
|
|
|
@clients_bp.route('/<client_id>', methods=['PUT'])
|
|
def update_client(client_id):
|
|
"""更新客户端配置"""
|
|
try:
|
|
data = request.get_json()
|
|
client = client_service.update_client(client_id, data)
|
|
|
|
if client:
|
|
return jsonify({
|
|
'success': True,
|
|
'data': client
|
|
})
|
|
else:
|
|
return jsonify({
|
|
'success': False,
|
|
'error': '客户端不存在'
|
|
}), 404
|
|
|
|
except Exception as e:
|
|
return jsonify({
|
|
'success': False,
|
|
'error': str(e)
|
|
}), 500
|
|
|
|
|
|
@clients_bp.route('/<client_id>', methods=['DELETE'])
|
|
def delete_client(client_id):
|
|
"""删除客户端"""
|
|
try:
|
|
success = client_service.delete_client(client_id)
|
|
|
|
if success:
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '客户端删除成功'
|
|
})
|
|
else:
|
|
return jsonify({
|
|
'success': False,
|
|
'error': '客户端不存在'
|
|
}), 404
|
|
|
|
except Exception as e:
|
|
return jsonify({
|
|
'success': False,
|
|
'error': str(e)
|
|
}), 500
|
|
|
|
|
|
@clients_bp.route('/<client_id>/test', methods=['POST'])
|
|
def test_client_connection(client_id):
|
|
"""测试客户端连接"""
|
|
try:
|
|
result = client_service.test_client_connection(client_id)
|
|
return jsonify(result)
|
|
|
|
except Exception as e:
|
|
return jsonify({
|
|
'success': False,
|
|
'error': str(e)
|
|
}), 500
|