Geen omschrijving

iris_api_examples.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. import json
  3. import os
  4. from typing import Any
  5. import httpx
  6. def env_bool(name: str, default: bool) -> bool:
  7. value = os.getenv(name)
  8. if value is None:
  9. return default
  10. return value.lower() in {"1", "true", "yes", "on"}
  11. IRIS_BASE_URL = os.getenv("IRIS_BASE_URL", "https://localhost:8443").rstrip("/")
  12. IRIS_API_KEY = os.getenv("IRIS_API_KEY", "")
  13. IRIS_VERIFY_SSL = env_bool("IRIS_VERIFY_SSL", False)
  14. INTEGRATOR_URL = os.getenv("INTEGRATOR_URL", "http://localhost:8088").rstrip("/")
  15. def pretty(title: str, payload: dict[str, Any]) -> None:
  16. print(f"\n=== {title} ===")
  17. print(json.dumps(payload, indent=2, default=str))
  18. def direct_iris_create_case() -> None:
  19. url = f"{IRIS_BASE_URL}/api/v2/cases"
  20. headers = {"Authorization": f"Bearer {IRIS_API_KEY}"} if IRIS_API_KEY else {}
  21. payload = {
  22. "title": "Sample SOC case from direct IRIS API call",
  23. "description": "Created by soc-integrator/examples/iris_api_examples.py",
  24. "tags": ["soc", "sample", "direct-api"],
  25. }
  26. with httpx.Client(verify=IRIS_VERIFY_SSL, timeout=20.0) as client:
  27. response = client.post(url, json=payload, headers=headers)
  28. response.raise_for_status()
  29. data = response.json() if response.content else {"status_code": response.status_code}
  30. pretty("Direct IRIS API response", data)
  31. def via_integrator_create_case() -> None:
  32. url = f"{INTEGRATOR_URL}/action/create-iris-case"
  33. payload = {
  34. "title": "Sample SOC case via integrator",
  35. "severity": "medium",
  36. "source": "soc-integrator-example",
  37. "payload": {
  38. "description": "Created through /action/create-iris-case",
  39. "tags": ["soc", "sample", "integrator-api"],
  40. },
  41. }
  42. with httpx.Client(timeout=20.0) as client:
  43. response = client.post(url, json=payload)
  44. response.raise_for_status()
  45. data = response.json() if response.content else {"status_code": response.status_code}
  46. pretty("Via integrator response", data)
  47. if __name__ == "__main__":
  48. try:
  49. direct_iris_create_case()
  50. except Exception as exc:
  51. print(f"Direct IRIS call failed: {exc}")
  52. try:
  53. via_integrator_create_case()
  54. except Exception as exc:
  55. print(f"Integrator IRIS call failed: {exc}")