client.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. """ODPS client factory."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from app.core.config import settings
  5. from app.hot_content.exceptions import HotContentFlowError
  6. def get_odps_client() -> Any:
  7. """Create a PyODPS client from environment-backed settings."""
  8. if not settings.odps_access_id.strip():
  9. raise HotContentFlowError("ODPS_ACCESS_ID is not configured")
  10. if not settings.odps_access_key.strip():
  11. raise HotContentFlowError("ODPS_ACCESS_KEY is not configured")
  12. if not settings.odps_project.strip():
  13. raise HotContentFlowError("ODPS_PROJECT is not configured")
  14. if not settings.odps_endpoint.strip():
  15. raise HotContentFlowError("ODPS_ENDPOINT is not configured")
  16. from odps import ODPS
  17. kwargs: dict[str, Any] = {
  18. "access_id": settings.odps_access_id.strip(),
  19. "secret_access_key": settings.odps_access_key.strip(),
  20. "project": settings.odps_project.strip(),
  21. "endpoint": settings.odps_endpoint.strip(),
  22. }
  23. if settings.odps_tunnel_endpoint.strip():
  24. kwargs["tunnel_endpoint"] = settings.odps_tunnel_endpoint.strip()
  25. return ODPS(**kwargs)