Skip to content

Commit 187f85c

Browse files
Refactored to emphasise detection of the public IP address.
1 parent 7c57312 commit 187f85c

3 files changed

Lines changed: 11 additions & 25 deletions

File tree

src/wiley_tdm/ip_utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Standard library imports
88
import logging
9-
from typing import Final, List
9+
from typing import Final, List, Optional
1010

1111
# Third-party imports
1212
import requests
@@ -19,7 +19,6 @@ class IPUtils:
1919
_logger = logging.getLogger(__name__)
2020

2121
# IP Configuration
22-
DEFAULT_IP: Final[str] = "127.0.0.1"
2322
IP_APIS: Final[List[str]] = [
2423
"https://api.ipify.org",
2524
"https://api.my-ip.io/ip",
@@ -31,11 +30,11 @@ class IPUtils:
3130
API_READ_TIMEOUT: Final[int] = 3 # seconds
3231

3332
@staticmethod
34-
def get_ip_address() -> str:
33+
def get_ip_address() -> Optional[str]:
3534
"""Get the public IP address by trying multiple API endpoints.
3635
3736
Returns:
38-
str: Public IP address or DEFAULT_IP if all attempts fail
37+
Optional[str]: Public IP address if successful, or None if all attempts fail.
3938
"""
4039
for api_url in IPUtils.IP_APIS:
4140
try:
@@ -52,4 +51,4 @@ def get_ip_address() -> str:
5251
continue
5352

5453
IPUtils._logger.error("All IP detection attempts failed")
55-
return IPUtils.DEFAULT_IP
54+
return None

src/wiley_tdm/tdm_client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@ def __init__(
118118

119119
self._validate_api_token(api_token)
120120
self._create_api_session()
121-
self._my_ip_address: str = IPUtils.get_ip_address()
121+
self._client_public_ip: Optional[str] = IPUtils.get_ip_address()
122122

123-
self.logger.info(
124-
"Your IP address, used to check entitlements: %s", self._my_ip_address
125-
)
123+
if self._client_public_ip:
124+
self.logger.info(
125+
"Your public IP address, (only) used to check entitlements: %s",
126+
self._client_public_ip,
127+
)
126128

127129
def _validate_api_token(self, api_token: Optional[str]) -> None:
128130
"""Initialize the TDM API token.
@@ -368,7 +370,7 @@ def _handle_api_error(
368370
return DownloadResult(
369371
doi,
370372
DownloadStatus.ACCESS_DENIED,
371-
f"TDM access denied from IP {self._my_ip_address}. Try manually: {pdf_url}",
373+
f"TDM access denied from IP {self._client_public_ip}. Try manually: {pdf_url}",
372374
api_status=status,
373375
)
374376
if status == HTTPStatus.NOT_FOUND:

tests/test_ip_utils.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33
Tests public IP detection and fallback behavior with mocked responses.
44
"""
55

6-
# Standard library imports
7-
import socket
8-
from unittest.mock import patch
9-
10-
# Third-party imports
11-
import requests
12-
136
# Local imports
147
from wiley_tdm.ip_utils import IPUtils
158

@@ -22,11 +15,3 @@ def test_get_ip_address_success():
2215
parts = ip.split(".")
2316
assert len(parts) == 4
2417
assert all(0 <= int(p) <= 255 for p in parts)
25-
26-
27-
def test_get_ip_address_fallback():
28-
"""Test fallback to DEFAULT_IP when detection fails."""
29-
with patch("requests.get", side_effect=requests.RequestException):
30-
with patch("socket.gethostbyname", side_effect=socket.error):
31-
ip = IPUtils.get_ip_address()
32-
assert ip == IPUtils.DEFAULT_IP

0 commit comments

Comments
 (0)