-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_gateway.py
More file actions
657 lines (572 loc) · 25.3 KB
/
Copy pathapi_gateway.py
File metadata and controls
657 lines (572 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
"""
API网关
提供完整的REST API接口,支持第三方集成
"""
import os
import json
import logging
from datetime import datetime
from typing import Dict, Any, List, Optional
from flask import Flask, request, jsonify, g
from functools import wraps
import traceback
from file_reader import FileReader
from enhanced_api_client import get_enhanced_api_client
from result_processor import ResultProcessor
from enhanced_security import require_auth, require_permission, get_security_manager
from config_manager import get_config_manager
from async_task_manager import get_task_manager, TaskStatus, TaskPriority
# 配置日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class APIGateway:
"""API网关类"""
def __init__(self, app: Flask):
"""
初始化API网关
Args:
app: Flask应用实例
"""
self.app = app
self._register_routes()
logger.info("API网关已初始化")
def _register_routes(self):
"""注册API路由"""
# 健康检查端点(无需认证)
@self.app.route('/health')
def health_check():
"""健康检查"""
return jsonify({'status': 'healthy', 'timestamp': datetime.now().isoformat()}), 200
# API版本信息
@self.app.route('/api/v1/info')
def api_info():
"""获取API信息"""
return jsonify({
'name': 'JudgeAI API',
'version': '1.0.0',
'description': '算法竞赛团队AI评分系统API',
'endpoints': {
'authentication': '/api/v1/auth/*',
'scoring': '/api/v1/scoring/*',
'config': '/api/v1/config/*',
'tasks': '/api/v1/tasks/*',
'files': '/api/v1/files/*'
},
'documentation': '/api/v1/docs'
})
# 认证相关API
@self.app.route('/api/v1/auth/login', methods=['POST'])
def api_login():
"""用户登录API"""
try:
data = request.get_json()
username = data.get('username')
password = data.get('password')
if not username or not password:
return jsonify({
'success': False,
'error': '用户名和密码不能为空',
'error_code': 'MISSING_CREDENTIALS'
}), 400
security_manager = get_security_manager()
user = security_manager.authenticate_user(username, password)
if not user:
security_manager.log_audit(
user_id=0,
action='api_login_failed',
resource='auth',
details={'username': username, 'ip': request.remote_addr},
success=False
)
return jsonify({
'success': False,
'error': '用户名或密码错误',
'error_code': 'INVALID_CREDENTIALS'
}), 401
# 生成Token
token = security_manager.generate_token(user)
# 记录审计日志
security_manager.log_audit(
user_id=user.id,
action='api_login_success',
resource='auth',
details={'ip': request.remote_addr}
)
return jsonify({
'success': True,
'data': {
'token': token,
'user': user.to_dict(),
'expires_in': 24 * 3600 # 24小时
}
})
except Exception as e:
logger.error(f"登录API错误: {e}")
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR'
}), 500
# 评分相关API
@self.app.route('/api/v1/scoring/submit', methods=['POST'])
@require_auth
@require_permission('write')
def api_submit_scoring():
"""提交评分任务API"""
try:
data = request.get_json()
students = data.get('students', [])
template_id = data.get('template_id')
async_mode = data.get('async', True)
if not students:
return jsonify({
'success': False,
'error': '学生数据不能为空',
'error_code': 'MISSING_STUDENTS'
}), 400
# 验证学生数据格式
for i, student in enumerate(students):
if not isinstance(student, dict):
return jsonify({
'success': False,
'error': f'第{i+1}个学生数据格式错误',
'error_code': 'INVALID_STUDENT_FORMAT'
}), 400
if not student.get('name') and not student.get('id'):
return jsonify({
'success': False,
'error': f'第{i+1}个学生缺少姓名或ID',
'error_code': 'MISSING_STUDENT_INFO'
}), 400
# 添加模板ID到学生信息
if template_id:
for student in students:
student['template_id'] = template_id
user = g.current_user
api_key = os.getenv("DASHSCOPE_API_KEY")
if async_mode:
# 异步模式
task_manager = get_task_manager()
# 保存学生数据到临时文件
import tempfile
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(students, f, ensure_ascii=False, indent=2)
temp_file = f.name
# 提交异步任务
task_parameters = {
'filepath': temp_file,
'api_key': api_key,
'batch_size': 10,
'user_id': user.id
}
task_id = task_manager.submit_task(
task_type='batch_scoring',
parameters=task_parameters,
priority=TaskPriority.HIGH if len(students) > 50 else TaskPriority.NORMAL
)
return jsonify({
'success': True,
'data': {
'task_id': task_id,
'status': 'submitted',
'total_students': len(students),
'async': True
}
})
else:
# 同步模式
api_client = get_enhanced_api_client(api_key=api_key)
results = api_client.score_students_batch(students)
return jsonify({
'success': True,
'data': {
'results': results,
'total_students': len(students),
'async': False
}
})
except Exception as e:
logger.error(f"提交评分API错误: {e}")
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR',
'details': str(e)
}), 500
@self.app.route('/api/v1/scoring/student', methods=['POST'])
@require_auth
@require_permission('write')
def api_score_student():
"""评分单个学生API"""
try:
data = request.get_json()
student = data.get('student')
template_id = data.get('template_id')
if not student:
return jsonify({
'success': False,
'error': '学生数据不能为空',
'error_code': 'MISSING_STUDENT'
}), 400
if not student.get('name') and not student.get('id'):
return jsonify({
'success': False,
'error': '学生缺少姓名或ID',
'error_code': 'MISSING_STUDENT_INFO'
}), 400
# 添加模板ID
if template_id:
student['template_id'] = template_id
api_key = os.getenv("DASHSCOPE_API_KEY")
api_client = get_enhanced_api_client(api_key=api_key)
result = api_client.score_student(student)
return jsonify({
'success': True,
'data': {
'result': result
}
})
except Exception as e:
logger.error(f"评分学生API错误: {e}")
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR',
'details': str(e)
}), 500
# 配置相关API
@self.app.route('/api/v1/config/templates', methods=['GET'])
@require_auth
@require_permission('read')
def api_get_templates():
"""获取评分模板列表API"""
try:
config_manager = get_config_manager()
templates = config_manager.list_templates(active_only=True)
return jsonify({
'success': True,
'data': {
'templates': [tpl.__dict__ for tpl in templates]
}
})
except Exception as e:
logger.error(f"获取模板API错误: {e}")
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR'
}), 500
@self.app.route('/api/v1/config/templates/<template_id>', methods=['GET'])
@require_auth
@require_permission('read')
def api_get_template(template_id):
"""获取单个评分模板API"""
try:
config_manager = get_config_manager()
template = config_manager.get_template(template_id)
if not template:
return jsonify({
'success': False,
'error': '模板不存在',
'error_code': 'TEMPLATE_NOT_FOUND'
}), 404
return jsonify({
'success': True,
'data': {
'template': template.__dict__
}
})
except Exception as e:
logger.error(f"获取模板详情API错误: {e}")
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR'
}), 500
@self.app.route('/api/v1/config/dimensions', methods=['GET'])
@require_auth
@require_permission('read')
def api_get_dimensions():
"""获取评分维度列表API"""
try:
config_manager = get_config_manager()
dimensions = config_manager.list_custom_dimensions(active_only=True)
return jsonify({
'success': True,
'data': {
'dimensions': [dim.__dict__ for dim in dimensions]
}
})
except Exception as e:
logger.error(f"获取维度API错误: {e}")
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR'
}), 500
# 任务相关API
@self.app.route('/api/v1/tasks/<task_id>', methods=['GET'])
@require_auth
@require_permission('read')
def api_get_task(task_id):
"""获取任务状态API"""
try:
task_manager = get_task_manager()
task = task_manager.get_task_status(task_id)
if not task:
return jsonify({
'success': False,
'error': '任务不存在',
'error_code': 'TASK_NOT_FOUND'
}), 404
return jsonify({
'success': True,
'data': {
'task': {
'task_id': task.task_id,
'task_type': task.task_type,
'status': task.status.value,
'priority': task.priority.name,
'created_at': task.created_at.isoformat(),
'started_at': task.started_at.isoformat() if task.started_at else None,
'completed_at': task.completed_at.isoformat() if task.completed_at else None,
'progress': {
'current': task.progress.current,
'total': task.progress.total,
'percentage': task.progress.percentage,
'message': task.progress.message,
'estimated_remaining': task.progress.estimated_remaining
},
'result': task.result.__dict__ if task.result else None
}
}
})
except Exception as e:
logger.error(f"获取任务API错误: {e}")
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR'
}), 500
@self.app.route('/api/v1/tasks', methods=['GET'])
@require_auth
@require_permission('read')
def api_list_tasks():
"""获取任务列表API"""
try:
status_filter = request.args.get('status')
limit = int(request.args.get('limit', 20))
task_manager = get_task_manager()
# 转换状态过滤
task_status = None
if status_filter:
try:
task_status = TaskStatus(status_filter)
except ValueError:
return jsonify({
'success': False,
'error': f'无效的状态值: {status_filter}',
'error_code': 'INVALID_STATUS'
}), 400
tasks = task_manager.get_task_list(status=task_status, limit=limit)
return jsonify({
'success': True,
'data': {
'tasks': [
{
'task_id': task.task_id,
'task_type': task.task_type,
'status': task.status.value,
'priority': task.priority.name,
'created_at': task.created_at.isoformat(),
'started_at': task.started_at.isoformat() if task.started_at else None,
'completed_at': task.completed_at.isoformat() if task.completed_at else None,
'progress': {
'current': task.progress.current,
'total': task.progress.total,
'percentage': task.progress.percentage,
'message': task.progress.message
}
}
for task in tasks
]
}
})
except Exception as e:
logger.error(f"获取任务列表API错误: {e}")
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR'
}), 500
# 文件处理API
@self.app.route('/api/v1/files/upload', methods=['POST'])
@require_auth
@require_permission('write')
def api_upload_file():
"""文件上传API"""
try:
if 'file' not in request.files:
return jsonify({
'success': False,
'error': '没有文件',
'error_code': 'NO_FILE'
}), 400
file = request.files['file']
if file.filename == '':
return jsonify({
'success': False,
'error': '没有选择文件',
'error_code': 'NO_FILE_SELECTED'
}), 400
# 检查文件类型
allowed_extensions = {'csv', 'xlsx', 'xls', 'json', 'txt'}
if not ('.' in file.filename and
file.filename.rsplit('.', 1)[1].lower() in allowed_extensions):
return jsonify({
'success': False,
'error': '不支持的文件格式',
'error_code': 'UNSUPPORTED_FILE_TYPE'
}), 400
# 保存文件
import tempfile
import uuid
file_id = str(uuid.uuid4())
temp_dir = tempfile.mkdtemp()
file_path = os.path.join(temp_dir, file.filename)
file.save(file_path)
# 读取文件
file_reader = FileReader()
students = file_reader.read_file(file_path)
validated_students = file_reader.validate_data(students)
if not validated_students:
return jsonify({
'success': False,
'error': '文件中没有找到有效的学生数据',
'error_code': 'NO_VALID_DATA'
}), 400
return jsonify({
'success': True,
'data': {
'file_id': file_id,
'filename': file.filename,
'total_records': len(students),
'valid_records': len(validated_students),
'preview': validated_students[:5] # 前5条预览
}
})
except Exception as e:
logger.error(f"文件上传API错误: {e}")
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR',
'details': str(e)
}), 500
# 统计信息API
@self.app.route('/api/v1/stats', methods=['GET'])
@require_auth
@require_permission('read')
def api_get_stats():
"""获取系统统计信息API"""
try:
# API统计
api_client = get_enhanced_api_client()
api_stats = api_client.get_stats()
# 任务统计
task_manager = get_task_manager()
all_tasks = task_manager.get_task_list(limit=1000)
task_stats = {
'total_tasks': len(all_tasks),
'completed_tasks': len([t for t in all_tasks if t.status == TaskStatus.COMPLETED]),
'running_tasks': len([t for t in all_tasks if t.status == TaskStatus.RUNNING]),
'failed_tasks': len([t for t in all_tasks if t.status == TaskStatus.FAILED])
}
# 配置统计
config_manager = get_config_manager()
config_stats = {
'total_templates': len(config_manager.list_templates()),
'total_dimensions': len(config_manager.list_custom_dimensions())
}
return jsonify({
'success': True,
'data': {
'api_stats': api_stats,
'task_stats': task_stats,
'config_stats': config_stats,
'system_info': {
'version': '1.0.0',
'uptime': 'N/A', # 可以添加实际运行时间
'timestamp': datetime.now().isoformat()
}
}
})
except Exception as e:
logger.error(f"获取统计信息API错误: {e}")
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR'
}), 500
# Webhook支持
@self.app.route('/api/v1/webhook/scoring-complete', methods=['POST'])
def api_webhook_scoring_complete():
"""评分完成Webhook"""
try:
# 验证webhook签名(可选)
signature = request.headers.get('X-Signature')
if signature:
# 这里可以添加签名验证逻辑
pass
data = request.get_json()
task_id = data.get('task_id')
status = data.get('status')
results = data.get('results', [])
if not task_id or not status:
return jsonify({
'success': False,
'error': '缺少必要参数',
'error_code': 'MISSING_PARAMETERS'
}), 400
# 处理webhook逻辑
logger.info(f"收到评分完成Webhook: 任务{task_id}, 状态{status}")
# 这里可以添加:
# 1. 发送通知
# 2. 更新数据库
# 3. 触发其他业务逻辑
return jsonify({
'success': True,
'message': 'Webhook处理成功'
})
except Exception as e:
logger.error(f"Webhook处理错误: {e}")
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR'
}), 500
# 错误处理
@self.app.errorhandler(404)
def api_not_found(error):
return jsonify({
'success': False,
'error': 'API端点不存在',
'error_code': 'ENDPOINT_NOT_FOUND'
}), 404
@self.app.errorhandler(405)
def api_method_not_allowed(error):
return jsonify({
'success': False,
'error': '请求方法不允许',
'error_code': 'METHOD_NOT_ALLOWED'
}), 405
@self.app.errorhandler(500)
def api_internal_error(error):
return jsonify({
'success': False,
'error': '服务器内部错误',
'error_code': 'INTERNAL_ERROR'
}), 500
def init_api_gateway(app: Flask):
"""初始化API网关"""
return APIGateway(app)