Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

enterprise_service.py 3.3KB

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