Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

enterprise_service.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from datetime import datetime
  2. from pydantic import BaseModel, Field
  3. from services.enterprise.base import EnterpriseRequest
  4. class WebAppSettings(BaseModel):
  5. access_mode: str = Field(
  6. description="Access mode for the web app. Can be 'public', 'private', 'private_all', 'sso_verified'",
  7. default="private",
  8. alias="accessMode",
  9. )
  10. class EnterpriseService:
  11. @classmethod
  12. def get_info(cls):
  13. return EnterpriseRequest.send_request("GET", "/info")
  14. @classmethod
  15. def get_workspace_info(cls, tenant_id: str):
  16. return EnterpriseRequest.send_request("GET", f"/workspace/{tenant_id}/info")
  17. @classmethod
  18. def get_app_sso_settings_last_update_time(cls) -> datetime:
  19. data = EnterpriseRequest.send_request("GET", "/sso/app/last-update-time")
  20. if not data:
  21. raise ValueError("No data found.")
  22. try:
  23. # parse the UTC timestamp from the response
  24. return datetime.fromisoformat(data)
  25. except ValueError as e:
  26. raise ValueError(f"Invalid date format: {data}") from e
  27. @classmethod
  28. def get_workspace_sso_settings_last_update_time(cls) -> datetime:
  29. data = EnterpriseRequest.send_request("GET", "/sso/workspace/last-update-time")
  30. if not data:
  31. raise ValueError("No data found.")
  32. try:
  33. # parse the UTC timestamp from the response
  34. return datetime.fromisoformat(data)
  35. except ValueError as e:
  36. raise ValueError(f"Invalid date format: {data}") from e
  37. class WebAppAuth:
  38. @classmethod
  39. def is_user_allowed_to_access_webapp(cls, user_id: str, app_code: str):
  40. params = {"userId": user_id, "appCode": app_code}
  41. data = EnterpriseRequest.send_request("GET", "/webapp/permission", params=params)
  42. return data.get("result", False)
  43. @classmethod
  44. def get_app_access_mode_by_id(cls, app_id: str) -> WebAppSettings:
  45. if not app_id:
  46. raise ValueError("app_id must be provided.")
  47. params = {"appId": app_id}
  48. data = EnterpriseRequest.send_request("GET", "/webapp/access-mode/id", params=params)
  49. if not data:
  50. raise ValueError("No data found.")
  51. return WebAppSettings(**data)
  52. @classmethod
  53. def batch_get_app_access_mode_by_id(cls, app_ids: list[str]) -> dict[str, WebAppSettings]:
  54. if not app_ids:
  55. return {}
  56. body = {"appIds": app_ids}
  57. data: dict[str, str] = EnterpriseRequest.send_request("POST", "/webapp/access-mode/batch/id", json=body)
  58. if not data:
  59. raise ValueError("No data found.")
  60. if not isinstance(data["accessModes"], dict):
  61. raise ValueError("Invalid data format.")
  62. ret = {}
  63. for key, value in data["accessModes"].items():
  64. curr = WebAppSettings()
  65. curr.access_mode = value
  66. ret[key] = curr
  67. return ret
  68. @classmethod
  69. def get_app_access_mode_by_code(cls, app_code: str) -> WebAppSettings:
  70. if not app_code:
  71. raise ValueError("app_code must be provided.")
  72. params = {"appCode": app_code}
  73. data = EnterpriseRequest.send_request("GET", "/webapp/access-mode/code", params=params)
  74. if not data:
  75. raise ValueError("No data found.")
  76. return WebAppSettings(**data)
  77. @classmethod
  78. def update_app_access_mode(cls, app_id: str, access_mode: str):
  79. if not app_id:
  80. raise ValueError("app_id must be provided.")
  81. if access_mode not in ["public", "private", "private_all"]:
  82. raise ValueError("access_mode must be either 'public', 'private', or 'private_all'")
  83. data = {"appId": app_id, "accessMode": access_mode}
  84. response = EnterpriseRequest.send_request("POST", "/webapp/access-mode", json=data)
  85. return response.get("result", False)
  86. @classmethod
  87. def cleanup_webapp(cls, app_id: str):
  88. if not app_id:
  89. raise ValueError("app_id must be provided.")
  90. body = {"appId": app_id}
  91. EnterpriseRequest.send_request("DELETE", "/webapp/clean", json=body)