Coverage for src / sentry_tool / client.py: 100.00%
16 statements
« prev ^ index » next coverage.py v7.13.2, created at 2026-02-17 21:46 -0500
« prev ^ index » next coverage.py v7.13.2, created at 2026-02-17 21:46 -0500
1"""Sentry API client with automatic retry for transient failures."""
3from typing import Any
5import requests
6from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential
8from sentry_tool.monitoring import get_logger
10log = get_logger("client")
12HTTP_NOT_FOUND = 404
15class NotFoundError(Exception):
16 """Raised when a Sentry API resource is not found (404)."""
19@retry(
20 retry=retry_if_exception_type(requests.exceptions.RequestException),
21 stop=stop_after_attempt(3),
22 wait=wait_exponential(multiplier=1, min=2, max=10),
23)
24def api_call(endpoint: str, token: str, base_url: str) -> Any:
25 full_url = f"{base_url}/api/0{endpoint}"
26 headers = {"Authorization": f"Bearer {token}"}
27 response = requests.get(full_url, headers=headers, timeout=30)
29 if response.status_code == HTTP_NOT_FOUND:
30 raise NotFoundError(endpoint)
32 response.raise_for_status()
33 return response.json()