Diomedes Git commited on
Commit
b901e6f
·
1 Parent(s): 2eec855

adding a tenacity based retry handler, etc

Browse files
src/cluas_mcp/common/api_clients.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import requests
2
  import feedparser
3
  import xml.etree.ElementTree as ET
 
1
+ from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
2
  import requests
3
  import feedparser
4
  import xml.etree.ElementTree as ET
src/cluas_mcp/common/http.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tenacity import (
2
+ retry,
3
+ stop_after_attempt,
4
+ wait_exponential,
5
+ retry_if_exception_type,
6
+ )
7
+ import requests
8
+
9
+
10
+ def _raise_for_status(response: requests.Response) -> requests.Response:
11
+ """
12
+ Raise a retryable exception for transient HTTP issues.
13
+ We consider 429 and 5xx status codes retryable.
14
+ """
15
+ if response.status_code == 429 or response.status_code >= 500:
16
+ raise requests.exceptions.RequestException(
17
+ f"Transient HTTP error {response.status_code}"
18
+ )
19
+ return response
20
+
21
+
22
+ @retry(
23
+ retry=retry_if_exception_type(requests.exceptions.RequestException),
24
+ stop=stop_after_attempt(3),
25
+ wait=wait_exponential(multiplier=1, min=4, max=10),
26
+ )
27
+ def fetch_with_retry(url: str) -> requests.Response:
28
+ """
29
+ Fetches a URL with retry logic suitable for academic APIs.
30
+
31
+ Retries on:
32
+ - Connection errors
33
+ - Timeouts
34
+ - 5xx responses
35
+ - 429 responses
36
+ """
37
+ response = requests.get(url, timeout=10)
38
+ return _raise_for_status(response)