Skip to content

Commit 5d49f34

Browse files
authored
Redact API Key in logs (#212)
* redact in logs * refactor
1 parent 8ef64a9 commit 5d49f34

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "peopledatalabs"
3-
version = "6.1.1"
3+
version = "6.2.0"
44
description = "Official Python client for the People Data Labs API"
55
homepage = "https://www.peopledatalabs.com"
66
repository = "https://github.com/peopledatalabs/peopledatalabs-python"

src/peopledatalabs/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
from .main import PDLPY
66

77

8-
__version__ = "6.1.1"
8+
__version__ = "6.2.0"
99

1010
__all__ = ["PDLPY"]

src/peopledatalabs/requests.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ def __post_init__(self):
4747
self.params = self.validator(**self.params).dict(exclude_none=True)
4848
logger.debug("Request object params after validation: %s", self.params)
4949

50+
def _sanitize_params(self) -> dict:
51+
"""
52+
Creates a copy of params with sensitive data redacted for logging.
53+
54+
Returns:
55+
A sanitized copy of the parameters dictionary.
56+
"""
57+
logger_params = self.params.copy()
58+
if "api_key" in logger_params:
59+
logger_params["api_key"] = "***"
60+
return logger_params
61+
5062
def get(self):
5163
"""
5264
Executes a GET request from the specified API.
@@ -60,7 +72,7 @@ def get(self):
6072
logger.info(
6173
"Calling %s with params: %s",
6274
self.url,
63-
json.dumps(self.params, indent=2),
75+
json.dumps(self._sanitize_params(), indent=2),
6476
)
6577
return requests.get(
6678
self.url, params=self.params, headers=self.headers, timeout=None
@@ -79,7 +91,7 @@ def post(self):
7991
logger.info(
8092
"Calling %s with params: %s",
8193
self.url,
82-
json.dumps(self.params, indent=2),
94+
json.dumps(self._sanitize_params(), indent=2),
8395
)
8496
return requests.post(
8597
self.url, json=self.params, headers=self.headers, timeout=None

tests/client/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_version():
1919
"""
2020
Version check.
2121
"""
22-
assert __version__ == "6.1.1"
22+
assert __version__ == "6.2.0"
2323

2424

2525
@pytest.mark.usefixtures("fake_api_key")

0 commit comments

Comments
 (0)