暫無描述

iris.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from typing import Any
  2. import httpx
  3. class IrisAdapter:
  4. def __init__(self, base_url: str, api_key: str) -> None:
  5. self.base_url = base_url.rstrip("/")
  6. self.api_key = api_key
  7. def _headers(self) -> dict[str, str]:
  8. return {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
  9. async def create_case(self, payload: dict[str, Any]) -> dict[str, Any]:
  10. headers = self._headers()
  11. async with httpx.AsyncClient(verify=False, timeout=20.0) as client:
  12. v2_url = f"{self.base_url}/api/v2/cases"
  13. response = await client.post(v2_url, json=payload, headers=headers)
  14. if response.status_code == 404:
  15. legacy_url = f"{self.base_url}/manage/cases/add"
  16. response = await client.post(legacy_url, json=payload, headers=headers)
  17. active_url = legacy_url
  18. else:
  19. active_url = v2_url
  20. try:
  21. response.raise_for_status()
  22. except httpx.HTTPStatusError as exc:
  23. detail = response.text.strip()
  24. raise RuntimeError(
  25. f"IRIS returned {response.status_code} for {active_url}. Response: {detail}"
  26. ) from exc
  27. return response.json() if response.content else {"status_code": response.status_code}
  28. async def update_case(self, case_id: str, payload: dict[str, Any]) -> dict[str, Any]:
  29. headers = self._headers()
  30. async with httpx.AsyncClient(verify=False, timeout=20.0) as client:
  31. v2_url = f"{self.base_url}/api/v2/cases/{case_id}"
  32. response = await client.put(v2_url, json=payload, headers=headers)
  33. if response.status_code == 404:
  34. legacy_url = f"{self.base_url}/manage/cases/update/{case_id}"
  35. response = await client.post(legacy_url, json=payload, headers=headers)
  36. active_url = legacy_url
  37. else:
  38. active_url = v2_url
  39. try:
  40. response.raise_for_status()
  41. except httpx.HTTPStatusError as exc:
  42. detail = response.text.strip()
  43. raise RuntimeError(
  44. f"IRIS returned {response.status_code} for {active_url}. Response: {detail}"
  45. ) from exc
  46. return response.json() if response.content else {"status_code": response.status_code}
  47. async def whoami(self) -> dict[str, Any]:
  48. headers = self._headers()
  49. url = f"{self.base_url}/user/whoami"
  50. async with httpx.AsyncClient(verify=False, timeout=20.0) as client:
  51. response = await client.get(url, headers=headers)
  52. response.raise_for_status()
  53. return response.json() if response.content else {"status_code": response.status_code}