Skip to content

Commit 5278ce5

Browse files
authored
skip OCS parsing when status == NO CONTENT (#197)
This code were not working: ```python3 nc.ocs("GET", "/ocs/v2.php/core/whatsnew") ``` this PR fixes it, for OCS that return `Http::STATUS_NO_CONTENT` we return empty list. In addition added `ALL` API scope, ref: nextcloud/app_api/pull/190 Previous commit: 8fd175a was added fast to just implement `ALL` API scope tests in AppAPI repo. --------- Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
1 parent 8fd175a commit 5278ce5

File tree

7 files changed

+37
-12
lines changed

7 files changed

+37
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file.
88

99
- API for registering Speech to Text provider(*avalaible from Nextcloud 29*). #196
1010

11+
### Fixed
12+
13+
- OCS: Correctly handling of `HTTP 204 No Content` status. #197
14+
1115
## [0.7.2 - 2023-12-28]
1216

1317
### Fixed

nc_py_api/_session.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def ocs(
199199

200200
check_error(response, info)
201201
if response.status_code == 204: # NO_CONTENT
202-
return ""
202+
return []
203203
response_data = loads(response.text)
204204
ocs_meta = response_data["ocs"]["meta"]
205205
if ocs_meta["status"] != "ok":
@@ -300,6 +300,8 @@ async def ocs(
300300
raise NextcloudException(408, info=info) from None
301301

302302
check_error(response, info)
303+
if response.status_code == 204: # NO_CONTENT
304+
return []
303305
response_data = loads(response.text)
304306
ocs_meta = response_data["ocs"]["meta"]
305307
if ocs_meta["status"] != "ok":

nc_py_api/ex_app/defs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ class ApiScope(enum.IntEnum):
4444
ACTIVITIES = 110
4545
"""Activity App endpoints."""
4646
NOTES = 120
47-
"""Notes App endpoints"""
47+
"""Notes App endpoints."""
48+
ALL = 9999
49+
"""All endpoints allowed."""

scripts/ci_register.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
php occ app_api:daemon:register manual_install "Manual Install" manual-install 0 0 0
77
php occ app_api:app:register "$1" manual_install --json-info \
8-
"{\"appid\":\"$1\",\"name\":\"$1\",\"daemon_config_name\":\"manual_install\",\"version\":\"$2\",\"secret\":\"$3\",\"host\":\"$4\",\"scopes\":{\"required\":[\"SYSTEM\", \"FILES\", \"FILES_SHARING\"],\"optional\":[\"USER_INFO\", \"USER_STATUS\", \"NOTIFICATIONS\", \"WEATHER_STATUS\", \"TALK\", \"TALK_BOT\", \"ACTIVITIES\", \"NOTES\", \"AI_PROVIDERS\"]},\"port\":$5,\"protocol\":\"http\",\"system_app\":1}" \
8+
"{\"appid\":\"$1\",\"name\":\"$1\",\"daemon_config_name\":\"manual_install\",\"version\":\"$2\",\"secret\":\"$3\",\"host\":\"$4\",\"scopes\":{\"required\":[\"ALL\"],\"optional\":[]},\"port\":$5,\"protocol\":\"http\",\"system_app\":1}" \
99
--force-scopes --wait-finish

scripts/dev_register.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ NEXTCLOUD_URL="http://$2" APP_PORT=9009 APP_ID="nc_py_api" APP_SECRET="12345" AP
1313
echo $! > /tmp/_install.pid
1414
python3 tests/_install_wait.py "http://localhost:9009/heartbeat" "\"status\":\"ok\"" 15 0.5
1515
docker exec "$1" sudo -u www-data php occ app_api:app:register nc_py_api manual_install --json-info \
16-
"{\"appid\":\"nc_py_api\",\"name\":\"nc_py_api\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"scopes\":{\"required\":[\"SYSTEM\", \"FILES\", \"FILES_SHARING\"],\"optional\":[\"USER_INFO\", \"USER_STATUS\", \"NOTIFICATIONS\", \"WEATHER_STATUS\", \"TALK\", \"TALK_BOT\", \"ACTIVITIES\", \"NOTES\", \"AI_PROVIDERS\"]},\"port\":9009,\"protocol\":\"http\",\"system_app\":1}" \
16+
"{\"appid\":\"nc_py_api\",\"name\":\"nc_py_api\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"scopes\":{\"required\":[\"ALL\"],\"optional\":[]},\"port\":9009,\"protocol\":\"http\",\"system_app\":1}" \
1717
--force-scopes --wait-finish
1818
cat /tmp/_install.pid
1919
kill -15 "$(cat /tmp/_install.pid)"

tests/actual_tests/misc_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ async def test_public_ocs_async(anc_any):
182182
assert r == await anc_any._session.ocs("GET", "ocs/v1.php/cloud/capabilities") # noqa
183183

184184

185+
def test_all_scope(nc_any):
186+
r = nc_any.ocs("GET", "/ocs/v2.php/core/whatsnew")
187+
assert isinstance(r, list)
188+
189+
190+
@pytest.mark.asyncio(scope="session")
191+
async def test_all_scope_async(anc_any):
192+
r = await anc_any.ocs("GET", "/ocs/v2.php/core/whatsnew")
193+
assert isinstance(r, list)
194+
195+
185196
def test_perform_login(nc_any):
186197
new_nc = Nextcloud() if isinstance(nc_any, Nextcloud) else NextcloudApp()
187198
assert not new_nc._session._capabilities

tests/actual_tests/nc_app_test.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,21 @@ async def test_get_users_list_async(anc_app):
2121

2222
def test_scope_allowed(nc_app):
2323
for i in ApiScope:
24-
assert nc_app.scope_allowed(i)
24+
if i == ApiScope.ALL.value:
25+
assert nc_app.scope_allowed(i)
26+
else:
27+
assert not nc_app.scope_allowed(i)
2528
assert not nc_app.scope_allowed(0) # noqa
2629
assert not nc_app.scope_allowed(999999999) # noqa
2730

2831

2932
@pytest.mark.asyncio(scope="session")
3033
async def test_scope_allowed_async(anc_app):
3134
for i in ApiScope:
32-
assert await anc_app.scope_allowed(i)
35+
if i == ApiScope.ALL.value:
36+
assert await anc_app.scope_allowed(i)
37+
else:
38+
assert not await anc_app.scope_allowed(i)
3339
assert not await anc_app.scope_allowed(0) # noqa
3440
assert not await anc_app.scope_allowed(999999999) # noqa
3541

@@ -50,25 +56,25 @@ async def test_app_cfg_async(anc_app):
5056

5157

5258
def test_scope_allow_app_ecosystem_disabled(nc_client, nc_app):
53-
assert nc_app.scope_allowed(ApiScope.FILES)
59+
assert nc_app.scope_allowed(ApiScope.ALL)
5460
nc_client.apps.disable("app_api")
5561
try:
56-
assert nc_app.scope_allowed(ApiScope.FILES)
62+
assert nc_app.scope_allowed(ApiScope.ALL)
5763
nc_app.update_server_info()
58-
assert not nc_app.scope_allowed(ApiScope.FILES)
64+
assert not nc_app.scope_allowed(ApiScope.ALL)
5965
finally:
6066
nc_client.apps.enable("app_api")
6167
nc_app.update_server_info()
6268

6369

6470
@pytest.mark.asyncio(scope="session")
6571
async def test_scope_allow_app_ecosystem_disabled_async(anc_client, anc_app):
66-
assert await anc_app.scope_allowed(ApiScope.FILES)
72+
assert await anc_app.scope_allowed(ApiScope.ALL)
6773
await anc_client.apps.disable("app_api")
6874
try:
69-
assert await anc_app.scope_allowed(ApiScope.FILES)
75+
assert await anc_app.scope_allowed(ApiScope.ALL)
7076
await anc_app.update_server_info()
71-
assert not await anc_app.scope_allowed(ApiScope.FILES)
77+
assert not await anc_app.scope_allowed(ApiScope.ALL)
7278
finally:
7379
await anc_client.apps.enable("app_api")
7480
await anc_app.update_server_info()

0 commit comments

Comments
 (0)