Skip to content

feat: load_token_info function #52

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

romulorosa
Copy link

@romulorosa romulorosa commented Mar 13, 2025

In Anaconda Notebooks, there are quite a few places in the code that try to get the token (and there will be new ones shortly), but I could not find a function that would help with that, therefore snippets like the one below are becoming popular in the code:

def _load_api_key() -> Optional[str]:
    if TokenInfo is None:
        return None
    global _cached_api_key
    if _cached_api_key:
        return _cached_api_key
    config = AnacondaCloudConfig()
    try:
        _cached_api_key = TokenInfo.load(domain=config.domain).api_key
        return _cached_api_key
    except TokenNotFoundError:
        return None

We will centralize all the auth things in a single place, and I think it would make sense to have a function in the auth library that could abstract all that for us. Thoughts?

@AlbertDeFusco AlbertDeFusco requested a review from mattkram March 13, 2025 19:21
@@ -226,11 +226,14 @@ def logout(config: Optional[AnacondaAuthConfig] = None) -> None:
pass


def is_logged_in() -> bool:
def load_token_info() -> Optional[TokenInfo]:
Copy link
Collaborator

@mattkram mattkram Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of a simple function. Also, just FYI, I think a lot of this desired behavior is now captured in the updated implementation of the create=True flag in TokenInfo.load(). See here.

In other words, if you do TokenInfo.load(create=True), it will return a TokenInfo object associated with the default domain from AnacondaAuthConfig().domain (by default, unless you explicitly pass a domain in. If it can be loaded from the keyring, it will be. If not, it will create a new empty instance. That object, if it was created and not loaded from the keyring, will have token_info.api_key is None.

But I'm totally open for feedback and to add a convenience wrapper as a simple function, if the @classmethod API is too confusing.

Does this align with what you are thinking?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your input, Matt! TokenInfo.load() does the job and meets all our needs. The idea for the PR actually came after using the CLI and noticing there was a straightforward way to retrieve the token. When I looked for the equivalent in the Python interface, I couldn’t find something at the same "level" of login and logout.

The wrapper simply makes it more convenient and intuitive, but it’s more of a nice-to-have rather than a necessity. Appreciate the discussion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants