feat(api): standardize REST contracts and auth

This commit is contained in:
2026-07-30 20:38:51 +08:00
parent ae1a657554
commit ba947b616b
86 changed files with 54193 additions and 990 deletions
+20 -30
View File
@@ -13,7 +13,7 @@ from .apps import (
data_timeseries_scheme_app,
)
from .common import emit_api, runtime_context
from .core import CLIError, parse_time_with_timezone, require_network, resolve_scheme
from .core import CLIError, parse_time_with_timezone, resolve_scheme
from .option_types import (
CompositeKind,
ElementType,
@@ -73,7 +73,7 @@ def data_realtime_links(
ctx,
summary="读取实时管道数据成功",
method="GET",
path="/realtime/links",
path="/timeseries/realtime/links",
params={
"start_time": parse_time_with_timezone(start_time, option_name="--start-time").isoformat(),
"end_time": parse_time_with_timezone(end_time, option_name="--end-time").isoformat(),
@@ -93,7 +93,7 @@ def data_realtime_nodes(
ctx,
summary="读取实时节点数据成功",
method="GET",
path="/realtime/nodes",
path="/timeseries/realtime/nodes",
params={
"start_time": parse_time_with_timezone(start_time, option_name="--start-time").isoformat(),
"end_time": parse_time_with_timezone(end_time, option_name="--end-time").isoformat(),
@@ -114,7 +114,7 @@ def data_realtime_simulation_by_id_time(
ctx,
summary="读取实时模拟数据成功",
method="GET",
path="/realtime/query/by-id-time",
path="/timeseries/realtime/simulation-results",
params={
"id": id,
"type": type.value,
@@ -137,7 +137,7 @@ def data_realtime_simulation_by_time_property(
ctx,
summary="读取实时属性聚合数据成功",
method="GET",
path="/realtime/query/by-time-property",
path="/timeseries/realtime/records",
params={
"type": type.value,
"query_time": parse_time_with_timezone(time, option_name="--time").isoformat(),
@@ -161,7 +161,7 @@ def data_scheme_links(
ctx,
summary="读取方案管道数据成功",
method="GET",
path="/scheme/links",
path="/timeseries/schemes/links",
params={
"scheme_name": resolve_scheme(runtime, scheme, required=True),
"scheme_type": _scheme_type_option(scheme_type),
@@ -189,7 +189,7 @@ def data_scheme_node_field(
ctx,
summary="读取方案节点字段成功",
method="GET",
path=f"/scheme/nodes/{node}/field",
path=f"/timeseries/schemes/nodes/{node}/field",
params={
"field": field,
"scheme_name": resolve_scheme(runtime, scheme, required=True),
@@ -233,7 +233,7 @@ def data_scheme_simulation(
ctx,
summary="读取方案单点模拟数据成功",
method="GET",
path="/scheme/query/by-id-time",
path="/timeseries/schemes/simulation-results",
params=params,
require_auth=True,
require_project=True,
@@ -253,7 +253,7 @@ def data_scheme_simulation(
ctx,
summary="读取方案属性聚合数据成功",
method="GET",
path="/scheme/query/by-scheme-time-property",
path="/timeseries/schemes/records",
params=params,
require_auth=True,
require_project=True,
@@ -270,7 +270,7 @@ def data_scada_query(
end_time: Annotated[str, typer.Option("--end-time", help="结束时间")],
field: Annotated[str | None, typer.Option("--field", help="字段名,仅支持 monitored_value|cleaned_value")] = None,
) -> None:
path = "/scada/by-ids-field-time-range" if field else "/scada/by-ids-time-range"
path = "/timeseries/scada-readings/fields" if field else "/timeseries/scada-readings"
params = {
"device_ids": ",".join(device_id),
"start_time": parse_time_with_timezone(start_time, option_name="--start-time").isoformat(),
@@ -334,7 +334,7 @@ def data_timeseries_composite(
ctx,
summary="读取复合 SCADA-模拟数据成功",
method="GET",
path="/composite/scada-simulation",
path="/timeseries/views/scada-simulations",
params=params,
require_auth=True,
require_project=True,
@@ -357,7 +357,7 @@ def data_timeseries_composite(
ctx,
summary="读取复合元素模拟数据成功",
method="GET",
path="/composite/element-simulation",
path="/timeseries/views/element-simulations",
params=params,
require_auth=True,
require_project=True,
@@ -377,7 +377,7 @@ def data_timeseries_composite(
ctx,
summary="读取元素关联 SCADA 数据成功",
method="GET",
path="/composite/element-scada",
path="/timeseries/views/element-scada-readings",
params=params,
require_auth=True,
require_project=True,
@@ -398,21 +398,19 @@ def data_composite_pipeline_health(
ctx,
summary="读取管道健康预测成功",
method="GET",
path="/composite/pipeline-health-prediction",
path="/pipeline-health-predictions",
params={
"network_name": require_network(runtime_context(ctx)),
"query_time": parse_time_with_timezone(end_time, option_name="--end-time").isoformat(),
},
require_auth=True,
require_project=True,
require_network_ctx=True,
)
def _scada_mapping(kind: str, action: str) -> tuple[str, dict[str, str]]:
mapping = {
("info", "get"): ("/getscadainfo/", {"id_param": "id"}),
("info", "list"): ("/getallscadainfo/", {}),
("info", "get"): ("/scada-info/detail", {"id_param": "id"}),
("info", "list"): ("/scada-info", {}),
}
result = mapping.get((kind, action))
if result is None:
@@ -433,7 +431,7 @@ def data_scada_get(
) -> None:
runtime = runtime_context(ctx)
path, meta = _scada_mapping(kind.value, "get")
params = {"network": require_network(runtime), meta["id_param"]: id}
params = {meta["id_param"]: id}
emit_api(
ctx,
summary="读取 SCADA 数据成功",
@@ -441,7 +439,6 @@ def data_scada_get(
path=path,
params=params,
require_auth=True,
require_network_ctx=True,
)
@@ -457,9 +454,7 @@ def data_scada_list(
summary="读取 SCADA 列表成功",
method="GET",
path=path,
params={"network": require_network(runtime)},
require_auth=True,
require_network_ctx=True,
)
@@ -470,10 +465,8 @@ def data_scheme_schema(ctx: typer.Context) -> None:
ctx,
summary="读取方案 schema 成功",
method="GET",
path="/getschemeschema/",
params={"network": require_network(runtime)},
path="/network-schemas/scheme",
require_auth=True,
require_network_ctx=True,
)
@@ -487,10 +480,9 @@ def data_scheme_get(
ctx,
summary="读取方案成功",
method="GET",
path="/getscheme/",
params={"network": require_network(runtime), "schema_name": name},
path="/schemes/detail",
params={"schema_name": name},
require_auth=True,
require_network_ctx=True,
)
@@ -502,7 +494,5 @@ def data_scheme_list(ctx: typer.Context) -> None:
summary="读取方案列表成功",
method="GET",
path="/schemes",
params={"network": require_network(runtime)},
require_auth=True,
require_network_ctx=True,
)