Skip to content
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

Integrate username save/restore into API #20

Open
NicoHood opened this issue Dec 16, 2017 · 2 comments
Open

Integrate username save/restore into API #20

NicoHood opened this issue Dec 16, 2017 · 2 comments

Comments

@NicoHood
Copy link

It would be nice if the code from the example can be moved directly into the API. The reason for that is, that it saves a lot of overhead. I recommend to have a default storage location for the username somewhere in the home directory. Optional a different config file could be passed instead. The username can then be omitted of course.

I am working on some code... will update.

@quentinsf
Copy link
Owner

I think part of the appeal of Qhue is its simplicity, so I'd like to keep the core classes pretty minimal, and many people might want to store the credentials in different ways.

But we could certainly add a subclass in another file called, say, AuthorizedBridge ?

@NicoHood
Copy link
Author

NicoHood commented Dec 16, 2017

It would just be another function as wrapper. My current solution looks like this:

def read_username_from_config(ip, filepath='~/.config/qhue/username.conf', retries=3, newuser=False):
    # Check for a credential file
    username = None
    if newuser or not os.path.exists(filepath):
        for x in range(retries):
            try:
                username = create_new_username(ip)
                break
            except QhueException as err:
                print("Error occurred while creating a new username: {}".format(err))

        if not username:
            raise QhueException("Failed to create new user ({} attempts).\n".format(retries))
        else:
            # Create non existing config directory
            directory = os.path.dirname(filepath)
            if not os.path.exists(directory):
                os.makedirs(directory)

            # Store the username in a credential file
            with open(filepath, "w") as cred_file:
                cred_file.write(username)

    # Read existing credential file
    else:
        with open(filepath, "r") as cred_file:
            username = cred_file.read()

    return username

def main():
    try:
        username = read_username_from_config(BRIDGE_IP)
    except QhueException as err:
        print("Error occurred while reading the username: {}".format(err))
        sys.exit(1)

    # create the bridge resource, passing the captured username
    bridge = Bridge(BRIDGE_IP, username)

It should be made a static function of course, as also suggested in #21

One thing I want to add is the option to select between newuser=None,True,False. So you can say it should create a new user(true), should not (false) or just create one if no config was found (none). This is not yet implemented.

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

No branches or pull requests

2 participants