Coverage for src / sentry_tool / client.py: 100.00%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.2, created at 2026-02-15 10:53 -0500

1"""Sentry API client with automatic retry for transient failures.""" 

2 

3from typing import Any 

4 

5import requests 

6from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential 

7 

8from sentry_tool.monitoring import get_logger 

9 

10log = get_logger("client") 

11 

12HTTP_NOT_FOUND = 404 

13 

14 

15class NotFoundError(Exception): 

16 """Raised when a Sentry API resource is not found (404).""" 

17 

18 

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) 

28 

29 if response.status_code == HTTP_NOT_FOUND: 

30 raise NotFoundError(endpoint) 

31 

32 response.raise_for_status() 

33 return response.json()