Skip to content

Commit 8cece14

Browse files
authored
added "perform_login" method (#195)
Added a separate login function named "perform_login" that can be called manually for easy check whether the login success or not Signed-off-by: Alexander Piskun <[email protected]>
1 parent 22a1964 commit 8cece14

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

nc_py_api/nextcloud.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ def theme(self) -> ThemingInfo | None:
113113
"""Returns Theme information."""
114114
return get_parsed_theme(self.capabilities["theming"]) if "theming" in self.capabilities else None
115115

116+
def perform_login(self) -> bool:
117+
"""Performs login into Nextcloud if not already logged in; manual invocation of this method is unnecessary."""
118+
try:
119+
self.update_server_info()
120+
except Exception: # noqa pylint: disable=broad-exception-caught
121+
return False
122+
return True
123+
116124
def ocs(
117125
self,
118126
method: str,
@@ -199,6 +207,14 @@ async def theme(self) -> ThemingInfo | None:
199207
"""Returns Theme information."""
200208
return get_parsed_theme((await self.capabilities)["theming"]) if "theming" in await self.capabilities else None
201209

210+
async def perform_login(self) -> bool:
211+
"""Performs login into Nextcloud if not already logged in; manual invocation of this method is unnecessary."""
212+
try:
213+
await self.update_server_info()
214+
except Exception: # noqa pylint: disable=broad-exception-caught
215+
return False
216+
return True
217+
202218
async def ocs(
203219
self,
204220
method: str,

tests/actual_tests/misc_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,18 @@ async def test_public_ocs_async(anc_any):
180180
r = await anc_any.ocs("GET", "/ocs/v1.php/cloud/capabilities")
181181
assert r == await anc_any.ocs("GET", "ocs/v1.php/cloud/capabilities")
182182
assert r == await anc_any._session.ocs("GET", "ocs/v1.php/cloud/capabilities") # noqa
183+
184+
185+
def test_perform_login(nc_any):
186+
new_nc = Nextcloud() if isinstance(nc_any, Nextcloud) else NextcloudApp()
187+
assert not new_nc._session._capabilities
188+
new_nc.perform_login()
189+
assert new_nc._session._capabilities
190+
191+
192+
@pytest.mark.asyncio(scope="session")
193+
async def test_perform_login_async(anc_any):
194+
new_nc = AsyncNextcloud() if isinstance(anc_any, Nextcloud) else AsyncNextcloudApp()
195+
assert not new_nc._session._capabilities
196+
await new_nc.perform_login()
197+
assert new_nc._session._capabilities

0 commit comments

Comments
 (0)