Skip to content

Commit 597f466

Browse files
committed
feat: add warning messages if the response has a "messages" key with a non-empty list value
1 parent abd6232 commit 597f466

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/onc/modules/_OncService.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import logging
4+
import pprint
35
import weakref
46
from time import time
57
from urllib import parse
@@ -8,6 +10,8 @@
810

911
from ._util import _createErrorMessage, _formatDuration
1012

13+
logging.basicConfig(format="%(levelname)s: %(message)s")
14+
1115

1216
class _OncService:
1317
"""
@@ -60,6 +64,26 @@ def _doRequest(self, url: str, filters: dict | None = None, getTime: bool = Fals
6064
response.raise_for_status()
6165
self._log(f"Web Service response time: {_formatDuration(responseTime)}")
6266

67+
# Log warning messages only when showWarning is True
68+
# and jsonResult["messages"] is not an empty list
69+
if (
70+
self._config("showWarning")
71+
and "messages" in jsonResult
72+
and jsonResult["messages"]
73+
):
74+
long_message = "\n".join(
75+
[f"* {message}" for message in jsonResult["messages"]]
76+
)
77+
78+
filters_without_token = filters.copy()
79+
del filters_without_token["token"]
80+
filters_str = pprint.pformat(filters_without_token)
81+
82+
logging.warning(
83+
f"When calling {url} with filters\n{filters_str},\n"
84+
f"there are several warning messages:\n{long_message}\n"
85+
)
86+
6387
if getTime:
6488
return jsonResult, responseTime
6589
else:

src/onc/onc.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class ONC:
3232
3333
- True: Print all information and debug messages (intended for debugging).
3434
- False: Only print information messages.
35+
showWarning : boolean, default True
36+
Whether warning messages are displayed. Some web services have "messages" key in the response JSON
37+
to indicate that something might need attention, like using a default value for a missing parameter.
3538
outPath : str | Path, default "output"
3639
The directory that files are saved to (relative to the current directory) when downloading files.
3740
The directory will be created if it does not exist during the download.
@@ -50,11 +53,13 @@ def __init__(
5053
token,
5154
production: bool = True,
5255
showInfo: bool = False,
56+
showWarning: bool = True,
5357
outPath: str | Path = "output",
5458
timeout: int = 60,
5559
):
5660
self.token = re.sub(r"[^a-zA-Z0-9\-]+", "", token)
5761
self.showInfo = showInfo
62+
self.showWarning = showWarning
5863
self.timeout = timeout
5964
self.production = production
6065
self.outPath = outPath

0 commit comments

Comments
 (0)