Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-129926 / 25.04 / private_methods #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 13 additions & 6 deletions truenas_api_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@
class Client:
"""Implicit wrapper of either a `JSONRPCClient` or a `LegacyClient`."""

def __init__(self, uri: str | None=None, reserved_ports=False, py_exceptions=False, log_py_exceptions=False,
call_timeout: float | UndefinedType=undefined, verify_ssl=True):
def __init__(self, uri: str | None=None, reserved_ports=False, private_methods=False, py_exceptions=False,
log_py_exceptions=False, call_timeout: float | UndefinedType=undefined, verify_ssl=True):
"""Initialize either a `JSONRPCClient` or a `LegacyClient`.

Use `JSONRPCClient` unless `uri` ends with '/websocket'.

Args:
uri: The address to connect to. Defaults to the local middlewared socket.
reserved_ports: `True` if the local socket should use a reserved port.
private_methods: `True` if calling private methods should be allowed
py_exceptions: `True` if the server should include exception objects in
`message['error']['data']['py_exception']`.
log_py_exceptions: `True` if exception tracebacks from API calls should be logged.
Expand All @@ -90,7 +91,8 @@ def __init__(self, uri: str | None=None, reserved_ports=False, py_exceptions=Fal
else:
client_class = JSONRPCClient

self.__client = client_class(uri, reserved_ports, py_exceptions, log_py_exceptions, call_timeout, verify_ssl)
self.__client = client_class(uri, reserved_ports, private_methods, py_exceptions, log_py_exceptions,
call_timeout, verify_ssl)

def __getattr__(self, item):
return getattr(self.__client, item)
Expand Down Expand Up @@ -397,13 +399,14 @@ class JSONRPCClient:
Keeps track of the calls made, jobs submitted, and callbacks. Maintains a websocket connection using a `WSClient`.

"""
def __init__(self, uri: str | None=None, reserved_ports=False, py_exceptions=False, log_py_exceptions=False,
call_timeout: float | UndefinedType=undefined, verify_ssl=True):
def __init__(self, uri: str | None = None, reserved_ports=False, private_methods=False, py_exceptions=False,
log_py_exceptions=False, call_timeout: float | UndefinedType = undefined, verify_ssl=True):
"""Initialize a `JSONRPCClient`.

Args:
uri: The address to connect to. Defaults to the local middlewared socket.
reserved_ports: `True` if the local socket should use a reserved port.
private_methods: `True` if calling private methods should be allowed
py_exceptions: `True` if the server should include exception objects in
`message['error']['data']['py_exception']`.
log_py_exceptions: `True` if exception tracebacks from API calls should be logged.
Expand All @@ -425,6 +428,7 @@ def __init__(self, uri: str | None=None, reserved_ports=False, py_exceptions=Fal
self._jobs: defaultdict[str, _JobDict] = defaultdict(dict) # type: ignore
self._jobs_lock = Lock()
self._jobs_watching = False
self._private_methods = private_methods
self._py_exceptions = py_exceptions
self._log_py_exceptions = log_py_exceptions
self._call_timeout = call_timeout
Expand Down Expand Up @@ -583,7 +587,10 @@ def _run_callback(self, event: _Payload, args: Iterable[str], kwargs: Collection

def on_open(self):
"""Make an API call to `core.set_options` to configure how middlewared sends its responses."""
self._set_options_call = self.call("core.set_options", {"py_exceptions": self._py_exceptions}, background=True)
self._set_options_call = self.call("core.set_options", {
"private_methods": self._private_methods,
"py_exceptions": self._py_exceptions,
}, background=True)

def on_close(self, code: int, reason: str | None=None):
"""Close this `JSONRPCClient` in response to the `WebSocketApp` closing.
Expand Down
4 changes: 2 additions & 2 deletions truenas_api_client/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ def result(self):


class LegacyClient:
def __init__(self, uri=None, reserved_ports=False, py_exceptions=False, log_py_exceptions=False,
call_timeout: float | UndefinedType=undefined, verify_ssl=True):
def __init__(self, uri=None, reserved_ports=False, private_methods=False, py_exceptions=False,
log_py_exceptions=False, call_timeout: float | UndefinedType=undefined, verify_ssl=True):
"""
Arguments:
:reserved_ports(bool): should the local socket used a reserved port
Expand Down
Loading