51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
VWED.api 模块 - API接口注册
|
||
"""
|
||
|
||
from typing import Dict, Any, Callable
|
||
from ..script_registry_service import get_global_registry
|
||
|
||
|
||
class VWEDApiModule:
|
||
"""VWED.api 模块 - API接口注册"""
|
||
|
||
def __init__(self, script_id: str):
|
||
self.script_id = script_id
|
||
self.registry = get_global_registry()
|
||
|
||
def register_route(self, path: str, method: str = "GET", handler: Callable = None,
|
||
description: str = "", params: Dict = None,
|
||
parameters: Dict = None, response_schema: Dict = None):
|
||
"""分离式API接口注册(用户要求的新格式)
|
||
|
||
使用方式:
|
||
VWED.api.register_route(
|
||
path="/calculate",
|
||
method="POST",
|
||
handler=calculate_handler,
|
||
description="数学计算接口",
|
||
params={"a": 0, "b": 0, "operation": "add"} # 简化的参数定义
|
||
)
|
||
"""
|
||
# 验证HTTP方法
|
||
valid_methods = ["GET", "POST", "PUT", "DELETE"]
|
||
if method.upper() not in valid_methods:
|
||
raise ValueError(f"method参数错误:'{method}' 不是有效的HTTP请求方式。支持的方式有:{', '.join(valid_methods)}")
|
||
|
||
if handler is None:
|
||
raise ValueError("handler参数不能为空")
|
||
|
||
self.registry.register_api_route(
|
||
path=path,
|
||
method=method.upper(),
|
||
handler=handler,
|
||
script_id=self.script_id,
|
||
description=description,
|
||
params=params, # 传递简化的参数格式
|
||
parameters=parameters,
|
||
response_schema=response_schema
|
||
)
|
||
return handler |