Skip to content

fix: Handle JSON responses #341

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

Merged
merged 2 commits into from
Mar 17, 2025
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
6 changes: 6 additions & 0 deletions nc_py_api/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def ocs(
*,
content: bytes | str | typing.Iterable[bytes] | typing.AsyncIterable[bytes] | None = None,
json: dict | list | None = None,
response_type: str | None = None,
params: dict | None = None,
files: dict | None = None,
**kwargs,
Expand All @@ -215,6 +216,8 @@ def ocs(
if response.status_code == 204: # NO_CONTENT
return []
response_data = loads(response.text)
if response_type == "json":
return response_data
ocs_meta = response_data["ocs"]["meta"]
if ocs_meta["status"] != "ok":
if (
Expand Down Expand Up @@ -319,6 +322,7 @@ async def ocs(
*,
content: bytes | str | typing.Iterable[bytes] | typing.AsyncIterable[bytes] | None = None,
json: dict | list | None = None,
response_type: str | None = None,
params: dict | None = None,
files: dict | None = None,
**kwargs,
Expand All @@ -337,6 +341,8 @@ async def ocs(
if response.status_code == 204: # NO_CONTENT
return []
response_data = loads(response.text)
if response_type == "json":
return response_data
ocs_meta = response_data["ocs"]["meta"]
if ocs_meta["status"] != "ok":
if (
Expand Down
10 changes: 8 additions & 2 deletions nc_py_api/nextcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,14 @@ def ocs(
*,
content: bytes | str | typing.Iterable[bytes] | typing.AsyncIterable[bytes] | None = None,
json: dict | list | None = None,
response_type: str | None = None,
params: dict | None = None,
**kwargs,
):
"""Performs OCS call and returns OCS response payload data."""
return self._session.ocs(method, path, content=content, json=json, params=params, **kwargs)
return self._session.ocs(
method, path, content=content, json=json, response_type=response_type, params=params, **kwargs
)

def download_log(self, fp) -> None:
"""Downloads Nextcloud log file. Requires Admin privileges."""
Expand Down Expand Up @@ -238,11 +241,14 @@ async def ocs(
*,
content: bytes | str | typing.Iterable[bytes] | typing.AsyncIterable[bytes] | None = None,
json: dict | list | None = None,
response_type: str | None = None,
params: dict | None = None,
**kwargs,
):
"""Performs OCS call and returns OCS response payload data."""
return await self._session.ocs(method, path, content=content, json=json, params=params, **kwargs)
return await self._session.ocs(
method, path, content=content, json=json, response_type=response_type, params=params, **kwargs
)

async def download_log(self, fp) -> None:
"""Downloads Nextcloud log file. Requires Admin privileges."""
Expand Down