from __future__ import unicode_literals

import getpass
import logging
import sys

from six.moves import input

from rbtools.api.errors import AuthorizationError
from rbtools.commands import CommandError


def get_authenticated_session(api_client, api_root, auth_required=False):
    """Return an authenticated session.

    None will be returned if the user is not authenticated, unless the
    'auth_required' parameter is True, in which case the user will be prompted
    to login.
    """
    session = api_root.get_session(expand='user')

    if not session.authenticated:
        if not auth_required:
            return None

        logging.warning('You are not authenticated with the Review Board '
                        'server at %s, please login.' % api_client.url)
        sys.stderr.write('Username: ')
        username = input()
        password = getpass.getpass(b'Password: ')
        api_client.login(username, password)

        try:
            session = session.get_self()
        except AuthorizationError:
            raise CommandError('You are not authenticated.')

    return session


def get_user(api_client, api_root, auth_required=False):
    """Return the user resource for the current session."""
    session = get_authenticated_session(api_client, api_root, auth_required)

    if session:
        return session.user


def get_username(api_client, api_root, auth_required=False):
    """Return the username for the current session."""
    session = get_authenticated_session(api_client, api_root, auth_required)

    if session:
        return session.links.user.title

from __future__ import unicode_literals

import getpass
import logging
import sys

from six.moves import input

from rbtools.api.errors import AuthorizationError
from rbtools.commands import CommandError


def get_authenticated_session(api_client, api_root, auth_required=False):
    """Return an authenticated session.

    None will be returned if the user is not authenticated, unless the
    'auth_required' parameter is True, in which case the user will be prompted
    to login.
    """
    session = api_root.get_session(expand='user')

    if not session.authenticated:
        if not auth_required:
            return None

        logging.warning('You are not authenticated with the Review Board '
                        'server at %s, please login.' % api_client.url)
        sys.stderr.write('Username: ')
        username = input()
        password = getpass.getpass(b'Password: ')
        api_client.login(username, password)

        try:
            session = session.get_self()
        except AuthorizationError:
            raise CommandError('You are not authenticated.')

    return session


def get_user(api_client, api_root, auth_required=False):
    """Return the user resource for the current session."""
    session = get_authenticated_session(api_client, api_root, auth_required)

    if session:
        return session.user

    return None


def get_username(api_client, api_root, auth_required=False):
    """Return the username for the current session."""
    user = get_user(api_client, api_root, auth_required)

    if user:
        return user.username

    return None

Fix a regression in accessing the username for the session.
Fix a regression in accessing the username for the session.

My previous optimization to fetching the user resource along with the
session broke the `get_username()` function, which attempted to follow a
now non-existent link. It's been updated to get the expanded user
resource instead and access the username from that.

Testing Done:
Ran `rbt status`. Before, it would crash with an attribute error. After,
it showed me a list of my open review requests.

Reviewed at https://reviews.reviewboard.org/r/6900/
