Skip to content

Commit

Permalink
Merge pull request #283 from GioF71/feature/login-session-file-add-op…
Browse files Browse the repository at this point in the history
…tional-print-function-282

Add optional fn_print to Session::login_session_file
  • Loading branch information
tehkillerbee authored Oct 7, 2024
2 parents c0a6aef + 7b4ee42 commit d81b834
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/login.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ This will print the link, and then wait for the login future to complete, but it
.. testcode::

# The function is print by default, but you can use anything, here we do it to avoid the print being swallowed
session.login_oauth_simple(function=printer)
session.login_oauth_simple(fn_print=printer)
print(session.check_login())

.. testoutput::
Expand Down
13 changes: 8 additions & 5 deletions tidalapi/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,15 @@ def login_session_file(
self,
session_file: Path,
do_pkce: Optional[bool] = False,
fn_print: Callable[[str], None] = print,
) -> bool:
"""Logs in to the TIDAL api using an existing OAuth/PKCE session file. If no
session json file exists, a new one will be created after successful login.
:param session_file: The session json file
:param do_pkce: Perform PKCE login. Default: Use OAuth logon
:param fn_print: A function which will be called to print the challenge text,
defaults to `print()`.
:return: Returns true if we think the login was successful.
"""
self.load_session_from_file(session_file)
Expand All @@ -460,10 +463,10 @@ def login_session_file(
if not self.check_login():
if do_pkce:
log.info("Creating new session (PKCE)...")
self.login_pkce()
self.login_pkce(fn_print=fn_print)
else:
log.info("Creating new session (OAuth)...")
self.login_oauth_simple()
self.login_oauth_simple(fn_print=fn_print)

if self.check_login():
log.info("TIDAL Login OK")
Expand Down Expand Up @@ -576,17 +579,17 @@ def pkce_get_auth_token(self, url_redirect: str) -> dict[str, Union[str, int]]:

return token

def login_oauth_simple(self, function: Callable[[str], None] = print) -> None:
def login_oauth_simple(self, fn_print: Callable[[str], None] = print) -> None:
"""Login to TIDAL using a remote link. You can select what function you want to
use to display the link.
:param function: The function you want to display the link with
:param fn_print: The function you want to display the link with
:raises: TimeoutError: If the login takes too long
"""

login, future = self.login_oauth()
text = "Visit https://{0} to log in, the code will expire in {1} seconds"
function(text.format(login.verification_uri_complete, login.expires_in))
fn_print(text.format(login.verification_uri_complete, login.expires_in))
future.result()

def login_oauth(self) -> Tuple[LinkLogin, concurrent.futures.Future[Any]]:
Expand Down

0 comments on commit d81b834

Please sign in to comment.