config_api.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from __future__ import annotations
  2. from typing import Any, TypedDict
  3. from .auth import find_project_config, resolve_project_token, _request_json
  4. CONFIG_OPERATOR = "Dataturing"
  5. class SetPointValueItem(TypedDict):
  6. point_id: str
  7. value: str
  8. def _post_config(project_key: str, path: str, payload: dict[str, Any]) -> Any:
  9. return _post_project_json(
  10. project_key,
  11. path,
  12. payload,
  13. success_key="state",
  14. message_keys=("state_info",),
  15. )
  16. def _post_ai(project_key: str, path: str, payload: dict[str, Any]) -> Any:
  17. return _post_project_json(
  18. project_key,
  19. path,
  20. payload,
  21. success_key="errcode",
  22. message_keys=("msg", "message"),
  23. )
  24. def _post_project_json(
  25. project_key: str,
  26. path: str,
  27. payload: dict[str, Any],
  28. *,
  29. success_key: str,
  30. message_keys: tuple[str, ...],
  31. ) -> Any:
  32. project = find_project_config(project_key)
  33. authorization = resolve_project_token(project)
  34. response_payload = _request_json(
  35. "POST",
  36. f"{project['base_url']}{path}",
  37. authorization,
  38. json_payload=payload,
  39. )
  40. if not isinstance(response_payload, dict):
  41. raise ValueError(f"API returned invalid payload for {path}: {response_payload}")
  42. status = response_payload.get(success_key)
  43. if str(status) not in {"0", "0.0"}:
  44. message = ""
  45. for key in message_keys:
  46. message = str(response_payload.get(key) or "").strip()
  47. if message:
  48. break
  49. raise ValueError(f"API failed for {path}: {message or response_payload}")
  50. return response_payload
  51. def search_ai_systems(
  52. project_key: str,
  53. keyword: str | None = None,
  54. page_size: int = 20,
  55. page_num: int = 1,
  56. order_by: list[str] | None = None,
  57. ) -> Any:
  58. return _post_ai(
  59. project_key,
  60. "/api/ai/system/search",
  61. {
  62. "page": page_num,
  63. "page_size": page_size,
  64. "keyword": keyword or "",
  65. "order_by": order_by or ["-id"],
  66. },
  67. )
  68. def search_ai_rcmd_operations(
  69. project_key: str,
  70. codes: list[str],
  71. end: str,
  72. page_size: int = 10,
  73. page_num: int = 1,
  74. order: str = "-create_time",
  75. ) -> Any:
  76. return _post_ai(
  77. project_key,
  78. "/api/ai/ai_rcmd_operation/search_ai_rcmd_operation",
  79. {
  80. "end": end,
  81. "codes": codes,
  82. "order": order,
  83. "page_size": page_size,
  84. "page_num": page_num,
  85. },
  86. )
  87. def get_ai_online_v2(project_key: str, codes: list[str]) -> Any:
  88. return _post_ai(
  89. project_key,
  90. "/api/ai/ai_rcmd_operation/ai_online_v2",
  91. {
  92. "codes": codes,
  93. },
  94. )
  95. def get_command_log(
  96. project_key: str,
  97. point_id: str,
  98. begin: int,
  99. end: int,
  100. page_size: int = 20,
  101. page_num: int = 1,
  102. export: bool = False,
  103. ) -> Any:
  104. return _post_config(
  105. project_key,
  106. "/api/configapi/public/get_command_log",
  107. {
  108. "page_num": page_num,
  109. "page_size": page_size,
  110. "point_id": point_id,
  111. "begin": begin,
  112. "end": end,
  113. "export": export,
  114. },
  115. )
  116. def set_multi_values(project_key: str, points: list[SetPointValueItem], from_: str = "M2_BACKEND") -> Any:
  117. return _post_config(
  118. project_key,
  119. "/basedataportal/value/set_multi_values",
  120. {
  121. "points": points,
  122. "from": from_,
  123. },
  124. )