diff --git a/src/onc/modules/_OncService.py b/src/onc/modules/_OncService.py index d278b02..a1d3c78 100644 --- a/src/onc/modules/_OncService.py +++ b/src/onc/modules/_OncService.py @@ -1,5 +1,7 @@ from __future__ import annotations +import logging +import pprint import weakref from time import time from urllib import parse @@ -8,6 +10,8 @@ from ._util import _createErrorMessage, _formatDuration +logging.basicConfig(format="%(levelname)s: %(message)s") + class _OncService: """ @@ -60,6 +64,26 @@ def _doRequest(self, url: str, filters: dict | None = None, getTime: bool = Fals response.raise_for_status() self._log(f"Web Service response time: {_formatDuration(responseTime)}") + # Log warning messages only when showWarning is True + # and jsonResult["messages"] is not an empty list + if ( + self._config("showWarning") + and "messages" in jsonResult + and jsonResult["messages"] + ): + long_message = "\n".join( + [f"* {message}" for message in jsonResult["messages"]] + ) + + filters_without_token = filters.copy() + del filters_without_token["token"] + filters_str = pprint.pformat(filters_without_token) + + logging.warning( + f"When calling {url} with filters\n{filters_str},\n" + f"there are several warning messages:\n{long_message}\n" + ) + if getTime: return jsonResult, responseTime else: diff --git a/src/onc/onc.py b/src/onc/onc.py index bcf1a8e..c0569df 100644 --- a/src/onc/onc.py +++ b/src/onc/onc.py @@ -32,6 +32,9 @@ class ONC: - True: Print all information and debug messages (intended for debugging). - False: Only print information messages. + showWarning : boolean, default True + Whether warning messages are displayed. Some web services have "messages" key in the response JSON + to indicate that something might need attention, like using a default value for a missing parameter. outPath : str | Path, default "output" The directory that files are saved to (relative to the current directory) when downloading files. The directory will be created if it does not exist during the download. @@ -50,11 +53,13 @@ def __init__( token, production: bool = True, showInfo: bool = False, + showWarning: bool = True, outPath: str | Path = "output", timeout: int = 60, ): self.token = re.sub(r"[^a-zA-Z0-9\-]+", "", token) self.showInfo = showInfo + self.showWarning = showWarning self.timeout = timeout self.production = production self.outPath = outPath