From 881af3d3318881467bd621e569d40c0f099106a6 Mon Sep 17 00:00:00 2001 From: Alexander Piskun Date: Sun, 15 Oct 2023 21:03:33 +0300 Subject: [PATCH] fixed LDAP user_id bug Signed-off-by: Alexander Piskun --- CHANGELOG.md | 3 ++- nc_py_api/_session.py | 36 +++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3db302d5..0eebdfe9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ This release contains some breaking changes in `users`, `notifications` API. ### Added -- `__repr__` method added for most objects(previously it was only present for `FsNode`). +- `__repr__` method added for most objects(previously it was only present for `FsNode`). #147 ### Changed @@ -21,6 +21,7 @@ This release contains some breaking changes in `users`, `notifications` API. ### Fixed - `users.get_details` with empty parameter in some cases was raised exception. +- ClientMode: in case when LDAP was used as user backend, user login differs from user_id and most API failed with 404. #148 ## [0.3.1 - 2023-10-07] diff --git a/nc_py_api/_session.py b/nc_py_api/_session.py index 420ffb50..88c6c376 100644 --- a/nc_py_api/_session.py +++ b/nc_py_api/_session.py @@ -134,15 +134,15 @@ class NcSessionBasic(ABC): adapter: Client adapter_dav: Client cfg: BasicConfig - user: str custom_headers: dict - _capabilities: dict response_headers: HttpxHeaders + _user: str + _capabilities: dict @abstractmethod def __init__(self, **kwargs): self._capabilities = {} - self.user = kwargs.get("user", "") + self._user = kwargs.get("user", "") self.custom_headers = kwargs.get("headers", {}) self.limits = Limits(max_keepalive_connections=20, max_connections=20, keepalive_expiry=60.0) self.init_adapter() @@ -168,7 +168,7 @@ def _get_stream(self, path_params: str, headers: dict, **kwargs) -> Iterator[Res "GET", f"{self.cfg.endpoint}{path_params}", headers=headers, timeout=timeout, **kwargs ) - def request_json( + def request( self, method: str, path: str, @@ -176,14 +176,25 @@ def request_json( data: Optional[Union[bytes, str]] = None, json: Optional[Union[dict, list]] = None, **kwargs, - ) -> dict: + ): method = method.upper() if params is None: params = {} params.update({"format": "json"}) headers = kwargs.pop("headers", {}) data_bytes = self.__data_to_bytes(headers, data, json) - r = self._ocs(method, f"{quote(path)}?{urlencode(params, True)}", headers, data_bytes, not_parse=True) + return self._ocs(method, f"{quote(path)}?{urlencode(params, True)}", headers, data_bytes, not_parse=True) + + def request_json( + self, + method: str, + path: str, + params: Optional[dict] = None, + data: Optional[Union[bytes, str]] = None, + json: Optional[Union[dict, list]] = None, + **kwargs, + ) -> dict: + r = self.request(method, path, params, data, json, **kwargs) return loads(r.text) if r.status_code != 304 else {} def ocs( @@ -319,6 +330,17 @@ def _create_adapter(self) -> Client: def update_server_info(self) -> None: self._capabilities = self.ocs(method="GET", path="/ocs/v1.php/cloud/capabilities") + @property + def user(self) -> str: + """Current user ID. Can be different from login name.""" + if not self._user: + self._user = self.ocs(method="GET", path="/ocs/v1.php/cloud/user")["id"] + return self._user + + @user.setter + def user(self, value: str): + self._user = value + @property def capabilities(self) -> dict: if not self._capabilities: @@ -360,7 +382,7 @@ class NcSession(NcSessionBasic): def __init__(self, **kwargs): self.cfg = Config(**kwargs) - super().__init__(user=self.cfg.auth[0]) + super().__init__() def _create_adapter(self) -> Client: return Client(auth=self.cfg.auth, follow_redirects=True, limits=self.limits, verify=self.cfg.options.nc_cert)