cloud-py-api / nc_py_api

Nextcloud Python Framework
https://cloud-py-api.github.io/nc_py_api/
Other
85 stars 4 forks source link

JSONDecodeError when trying to use ocs with groupfolders #277

Closed Luux closed 1 month ago

Luux commented 1 month ago
nc.ocs(method="GET", path="/apps/groupfolders/folders")

results in a JSONDecodeError:

Exception has occurred: JSONDecodeError
Expecting value: line 1 column 1 (char 0)
StopIteration: 0

During handling of the above exception, another exception occurred:

  File ".../nc_py_api/nc_py_api/_session.py", line 216, in ocs
    response_data = loads(response.text)
                    ^^^^^^^^^^^^^^^^^^^^
  File ".../nc_py_api/nc_py_api/nextcloud.py", line 144, in ocs
    return self._session.ocs(method, path, content=content, json=json, params=params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../customer-data-nextcloud/nextcloud_api_test/nc-py-api.py", line 51, in <module>
    folders = nc.ocs(method="GET", path="/apps/groupfolders/folders")
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Checking the response and also testing with curl, it seems that the server sends xml back per default. With curl, this can be fixed by adding -H "Accept: application/json" - this should be done per default in the ocs function as well.

bigcat88 commented 1 month ago

it is done in 'ocs' function, but for only for ocs urls

_session.py:

    self._ocs_regexp = re.compile(r"/ocs/v[12]\.php/")

    def _request_event_ocs(self, request: Request) -> None:
        str_url = str(request.url)
        if re.search(self._ocs_regexp, str_url) is not None:  # this is OCS call
            request.url = request.url.copy_merge_params({"format": "json"})
Luux commented 1 month ago

Ah! Could you extend this to support the groupfolders app as well? (Idk if also other apps deviate from the API like this)

Luux commented 1 month ago

I can confirm that extending the regex to self._ocs_regexp = re.compile(r"/ocs/v[12]\.php/|/apps/groupfolders/") seems to solve the issue already

bigcat88 commented 1 month ago

Reference:

We can temporarily make a workaround for this..

bigcat88 commented 1 month ago

I can confirm that extending the regex to self._ocs_regexp = re.compile(r"/ocs/v[12]\.php/|/apps/groupfolders/") seems to solve the issue already

Are you sure that this works?

AFAIK, setting "format=json" works only for native OCS endpoints in Nextcloud(maybe I am wrong).

Only setting Accept: application/json header works for my setup for groupfolder endpoints

Luux commented 1 month ago

At least group_folders = nc.ocs(method="GET", path="/apps/groupfolders/folders") worked without issues. I did not try all available commands though.

bigcat88 commented 1 month ago

@Luux can you please check if the linked PR works for you?

I do not know what is with my dev-setup, but I can not get ?format=json work in it for GroupFolders app.