<commit_before>from .base_request import BaseRequest
from .token import Token
from .settings import Settings
from . import exceptions


class Auth(object):
    """
    This class implements all authentication functions for Resin Python SDK.

    """

    def __init__(self):
        self.base_request = BaseRequest()
        self.settings = Settings()
        self.token = Token()

    def login(self, **credentials):
        """
        This function is used for logging into Resin.io using username and password.

        Args:
            **credentials: credentials keyword arguments.
                username (str): Resin.io username.
                password (str): Password.

        Returns:
            This functions saves Auth Token to Settings and returns nothing.

        Raises:
            LoginFailed: if the username or password is invalid.

        """

        token = self.authenticate(**credentials)
        if self.token.is_valid_token(token):
            self.token.set(token)
        else:
            raise exceptions.LoginFailed()

    def login_with_token(self, token):
        """
        This function is used for logging into Resin.io using Auth Token.
        Auth Token can be found in Preferences section on Resin.io Dashboard.

        Args:
            token (str): Auth Token.

        Returns:
            This functions saves Auth Token to Settings and returns nothing.

        Raises:
            MalformedToken: if token is invalid.

        """

        if self.token.is_valid_token(token):
            self.token.set(token)
        else:
            raise exceptions.MalformedToken(token)

    def who_am_i(self):
        """
        This function retrieves username of logged in user.

        Returns:
            str: username.

        Raises:
            NotLoggedIn: if there is no user logged in.

        """

        return self.token.get_username()

    def authenticate(self, **credentials):
        """
        This function authenticates provided credentials information.
        You should use Auth.login when possible, as it takes care of saving the Auth Token and username as well.

        Args:
            **credentials: credentials keyword arguments.
                username (str): Resin.io username.
                password (str): Password.

        Returns:
            str: Auth Token,

        Raises:
            LoginFailed: if the username or password is invalid.

        """

        return self.base_request.request(
            'login_', 'POST', data=credentials,
            endpoint=self.settings.get('api_endpoint'), auth=False
        )

    def is_logged_in(self):
        """
        This function checks if you're logged in

        Returns:
            bool: True if logged in, False otherwise.

        """

        return self.base_request.request(
            '/whoami', 'GET', endpoint=self.settings.get('api_endpoint')
        )

    def get_token(self):
        """
        This function retrieves Auth Token.

        Returns:
            str: Auth Token.
<commit_msg>Fixes #18: set return value of resin.auth.is_logged_in() to bool<commit_after>from .base_request import BaseRequest
from .token import Token
from .settings import Settings
from . import exceptions


class Auth(object):
    """
    This class implements all authentication functions for Resin Python SDK.

    """

    def __init__(self):
        self.base_request = BaseRequest()
        self.settings = Settings()
        self.token = Token()

    def login(self, **credentials):
        """
        This function is used for logging into Resin.io using username and password.

        Args:
            **credentials: credentials keyword arguments.
                username (str): Resin.io username.
                password (<PASSWORD>): Password.

        Returns:
            This functions saves Auth Token to Settings and returns nothing.

        Raises:
            LoginFailed: if the username or password is invalid.

        """

        token = self.authenticate(**credentials)
        if self.token.is_valid_token(token):
            self.token.set(token)
        else:
            raise exceptions.LoginFailed()

    def login_with_token(self, token):
        """
        This function is used for logging into Resin.io using Auth Token.
        Auth Token can be found in Preferences section on Resin.io Dashboard.

        Args:
            token (str): Auth Token.

        Returns:
            This functions saves Auth Token to Settings and returns nothing.

        Raises:
            MalformedToken: if token is invalid.

        """

        if self.token.is_valid_token(token):
            self.token.set(token)
        else:
            raise exceptions.MalformedToken(token)

    def who_am_i(self):
        """
        This function retrieves username of logged in user.

        Returns:
            str: username.

        Raises:
            NotLoggedIn: if there is no user logged in.

        """

        return self.token.get_username()

    def authenticate(self, **credentials):
        """
        This function authenticates provided credentials information.
        You should use Auth.login when possible, as it takes care of saving the Auth Token and username as well.

        Args:
            **credentials: credentials keyword arguments.
                username (str): Resin.io username.
                password (str): Password.

        Returns:
            str: Auth Token,

        Raises:
            LoginFailed: if the username or password is invalid.

        """

        return self.base_request.request(
            'login_', 'POST', data=credentials,
            endpoint=self.settings.get('api_endpoint'), auth=False
        )

    def is_logged_in(self):
        """
        This function checks if you're logged in

        Returns:
            bool: True if logged in, False otherwise.

        """

        try:
            self.base_request.request(
                '/whoami', 'GET', endpoint=self.settings.get('api_endpoint')
            )
            return True
        except exceptions.RequestError:
            return False

    def get_token(self):
        """
        This function retrieves Auth Token.

        Returns:
            str: Auth Token.

{'fraction_non_alphanumeric': 0.07814738182396944, 'fraction_numerical': 0.0003183192742320548, 'mean_word_length': 1.828082808280828, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '565283', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1506, 'n_words': 534}