Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/onc/modules/_OncService.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import logging
import pprint
import weakref
from time import time
from urllib import parse
Expand All @@ -8,6 +10,8 @@

from ._util import _createErrorMessage, _formatDuration

logging.basicConfig(format="%(levelname)s: %(message)s")


class _OncService:
"""
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions src/onc/onc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down