Skip to content

Commit

Permalink
fixed LDAP user_id bug
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Piskun <[email protected]>
  • Loading branch information
bigcat88 committed Oct 15, 2023
1 parent e411fca commit 881af3d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]

Expand Down
36 changes: 29 additions & 7 deletions nc_py_api/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -168,22 +168,33 @@ 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,
params: Optional[dict] = None,
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(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 881af3d

Please sign in to comment.