pihole6api 0.1.2__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pihole6api/conn.py CHANGED
@@ -2,6 +2,7 @@ import requests
2
2
  import urllib3
3
3
  from urllib.parse import urljoin
4
4
  import warnings
5
+ import time
5
6
 
6
7
  # Suppress InsecureRequestWarning
7
8
  urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
@@ -25,22 +26,43 @@ class PiHole6Connection:
25
26
  self._authenticate()
26
27
 
27
28
  def _authenticate(self):
28
- """Authenticate with the Pi-hole API and store session ID and CSRF token."""
29
+ """Authenticate with the Pi-hole API and store session ID and CSRF token.
30
+
31
+ Retries up to three times (with a one-second pause between attempts)
32
+ before raising an exception.
33
+ """
29
34
  auth_url = urljoin(self.base_url, "auth")
30
35
  payload = {"password": self.password}
36
+ max_attempts = 3
37
+ last_exception = None
31
38
 
32
- response = requests.post(auth_url, json=payload, verify=False)
33
-
34
- if response.status_code == 200:
35
- data = response.json()
36
- if "session" in data and data["session"]["valid"]:
37
- self.session_id = data["session"]["sid"]
38
- self.csrf_token = data["session"]["csrf"]
39
- self.validity = data["session"]["validity"]
40
- else:
41
- raise Exception("Authentication failed: Invalid session response")
42
- else:
43
- raise Exception(f"Authentication failed: {response.json().get('error', {}).get('message', 'Unknown error')}")
39
+ for attempt in range(1, max_attempts + 1):
40
+ response = requests.post(auth_url, json=payload, verify=False)
41
+ try:
42
+ if response.status_code == 200:
43
+ data = response.json()
44
+ if "session" in data and data["session"]["valid"]:
45
+ self.session_id = data["session"]["sid"]
46
+ self.csrf_token = data["session"]["csrf"]
47
+ self.validity = data["session"]["validity"]
48
+ return # Successful authentication
49
+ else:
50
+ last_exception = Exception("Authentication failed: Invalid session response")
51
+ else:
52
+ # Try to extract an error message from the response
53
+ try:
54
+ error_msg = response.json().get("error", {}).get("message", "Unknown error")
55
+ except (json.decoder.JSONDecodeError, ValueError):
56
+ error_msg = f"HTTP {response.status_code}: {response.reason}"
57
+ last_exception = Exception(f"Authentication failed: {error_msg}")
58
+ except Exception as e:
59
+ last_exception = e
60
+
61
+ if attempt < max_attempts:
62
+ time.sleep(1) # Pause before retrying
63
+
64
+ # All attempts failed; raise the last captured exception.
65
+ raise last_exception
44
66
 
45
67
  def _get_headers(self):
46
68
  """Return headers including the authentication SID and CSRF token."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: pihole6api
3
- Version: 0.1.2
3
+ Version: 0.1.3
4
4
  Summary: Python API Client for Pi-hole 6
5
5
  Author-email: Shane Barbetta <shane@barbetta.me>
6
6
  License: MIT
@@ -3,7 +3,7 @@ pihole6api/actions.py,sha256=8CBkr8nYfT8yfdCO6F9M9nompaYcFdsaYGiEa1eVDCw,693
3
3
  pihole6api/client.py,sha256=HYdRh3CSZJ0srbkpjIVnLo-iy1avqKDUne5ji2Aq394,2013
4
4
  pihole6api/client_management.py,sha256=opPYGrjuW6SiwuxuvoOxnqjpmflU2znKHsqFbGSS3Gg,2439
5
5
  pihole6api/config.py,sha256=NdBHOudz147oIs5YVR3U4WLvqk3hU3HlZHnshy1NK4g,4680
6
- pihole6api/conn.py,sha256=ArXgFtb3QG2HeBeDvhEuZ7jcBDR6fW8QorlCF46Y3MM,4840
6
+ pihole6api/conn.py,sha256=qqC-PO6a2RR_dluhSeu_RMeo6TpA8_Xlg5CA2FTwG7Y,5825
7
7
  pihole6api/dhcp.py,sha256=1A3z-3q9x51-6MOC3JMl7yR_5pHmRxZtMWtPqzWxYm0,629
8
8
  pihole6api/dns_control.py,sha256=mxV3AIuGCsx0-1ibpMXor9QUGd_fDFfeaUENPhIK_TY,853
9
9
  pihole6api/domain_management.py,sha256=vxhQSG5F8EFDGqtiNkF0H_KOWFMerXaAuJZT0nMa8ec,3492
@@ -12,8 +12,8 @@ pihole6api/group_management.py,sha256=MGHwegw-b9U9PIA-IBzqT-a1kYkpXyjfSXJJJjkyTx
12
12
  pihole6api/list_management.py,sha256=B98FdgsIZhfRJHRxtRmwWsRPJ4rGlhvN-9gFEGdbb-c,3396
13
13
  pihole6api/metrics.py,sha256=czNyx9tUf2yZi_HnUpqykrtW51c042Rxq3zFl2_GjLY,7379
14
14
  pihole6api/network_info.py,sha256=u5NIteFoI-yav05eE6v81-9gs3MeZVBlNCSBS5gzCT8,1006
15
- pihole6api-0.1.2.dist-info/LICENSE,sha256=hpO6J6J9O1VZxZeHQTxKMTmuobaHbApiZxp279I4xNU,1062
16
- pihole6api-0.1.2.dist-info/METADATA,sha256=0kZSJq4RYKkx6fHMae0Tohtbwm7QVORbYnZ15GJdgPw,3837
17
- pihole6api-0.1.2.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
18
- pihole6api-0.1.2.dist-info/top_level.txt,sha256=Qrh46lxEC54rBR8T53em-tuZLWbmi1SDwL1rOhsgrME,11
19
- pihole6api-0.1.2.dist-info/RECORD,,
15
+ pihole6api-0.1.3.dist-info/LICENSE,sha256=hpO6J6J9O1VZxZeHQTxKMTmuobaHbApiZxp279I4xNU,1062
16
+ pihole6api-0.1.3.dist-info/METADATA,sha256=JMgYTLowrNI52DPCEaVpBIqiD4NHBX9av86qJLQ8-wg,3837
17
+ pihole6api-0.1.3.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
18
+ pihole6api-0.1.3.dist-info/top_level.txt,sha256=Qrh46lxEC54rBR8T53em-tuZLWbmi1SDwL1rOhsgrME,11
19
+ pihole6api-0.1.3.dist-info/RECORD,,