<commit_before>import requests
from .bug import Bug
from .errors import (BugsyException, LoginException)
from .search import Search


class Bugsy(object):
    """
        Bugsy allows easy getting and putting of Bugzilla bugs
    """

    DEFAULT_SEARCH = ['version', 'id', 'summary', 'status', 'op_sys',
                      'resolution', 'product', 'component', 'platform',
                      'whiteboard']

    def __init__(
            self,
            username=None,
            password=<PASSWORD>,
            userid=None,
            cookie=None,
            api_key=None,
            bugzilla_url='https://bugzilla.mozilla.org/rest'
    ):
        """
            Initialises a new instance of Bugsy

            :param username: Username to login with. Defaults to None
            :param password: Password to login with. Defaults to None
            :param userid: User ID to login with. Defaults to None
            :param cookie: Cookie to login with. Defaults to None
            :param apikey: API key to use. Defaults to None.
            :param bugzilla_url: URL endpoint to interact with. Defaults to
            https://bugzilla.mozilla.org/rest

            If a api_key is passed in, Bugsy will use this for authenticating
            requests. While not required to perform requests, if a username is
            passed in along with api_key, we will validate that the api key is
            valid for this username. Otherwise the api key is blindly used
            later.

            If a username AND password are passed in Bugsy will try get a login
            token from Bugzilla. If we can't login then a LoginException will
            be raised.

            If a userid AND cookie are passed in Bugsy will create a login
            token from them.
            If no username was passed in it will then try to get the username
            from Bugzilla.
        """
        self.api_key = api_key
        self.username = username
        self.password = password
        self.userid = userid
        self.cookie = cookie
        self.bugzilla_url = bugzilla_url
        self.token = None
        self.session = requests.Session()
        self._have_auth = False

        # Prefer API keys over all other auth methods.
        if self.api_key:
            if self.username:
                result = self.request(
                    'valid_login',
                    headers={'X-Bugzilla-API-Key': self.api_key},
                    params={'login': self.username}
                )

                if result is not True:
                    raise LoginException(result['message'])

            self.session.headers['X-Bugzilla-API-Key'] = self.api_key
            self._have_auth = True<commit_msg>handle valid_login error checking a bit better<commit_after>import requests
from .bug import Bug
from .errors import (BugsyException, LoginException)
from .search import Search


class Bugsy(object):
    """
        Bugsy allows easy getting and putting of Bugzilla bugs
    """

    DEFAULT_SEARCH = ['version', 'id', 'summary', 'status', 'op_sys',
                      'resolution', 'product', 'component', 'platform',
                      'whiteboard']

    def __init__(
            self,
            username=None,
            password=<PASSWORD>,
            userid=None,
            cookie=None,
            api_key=None,
            bugzilla_url='https://bugzilla.mozilla.org/rest'
    ):
        """
            Initialises a new instance of Bugsy

            :param username: Username to login with. Defaults to None
            :param password: Password to login with. Defaults to None
            :param userid: User ID to login with. Defaults to None
            :param cookie: Cookie to login with. Defaults to None
            :param apikey: API key to use. Defaults to None.
            :param bugzilla_url: URL endpoint to interact with. Defaults to
            https://bugzilla.mozilla.org/rest

            If a api_key is passed in, Bugsy will use this for authenticating
            requests. While not required to perform requests, if a username is
            passed in along with api_key, we will validate that the api key is
            valid for this username. Otherwise the api key is blindly used
            later.

            If a username AND password are passed in Bugsy will try get a login
            token from Bugzilla. If we can't login then a LoginException will
            be raised.

            If a userid AND cookie are passed in Bugsy will create a login
            token from them.
            If no username was passed in it will then try to get the username
            from Bugzilla.
        """
        self.api_key = api_key
        self.username = username
        self.password = password
        self.userid = userid
        self.cookie = cookie
        self.bugzilla_url = bugzilla_url
        self.token = None
        self.session = requests.Session()
        self._have_auth = False

        # Prefer API keys over all other auth methods.
        if self.api_key:
            if self.username:
                result = self.request(
                    'valid_login',
                    headers={'X-Bugzilla-API-Key': self.api_key},
                    params={'login': self.username}
                )

                if type(result) == bool and not result:
                    raise LoginException("login name doesn't match api key")
                elif type(result) == dict:
                    raise LoginException(result['message'])

            self.session.headers['X-Bugzilla-API-Key'] = self.api_key
            self._have_auth = True
{'fraction_non_alphanumeric': 0.06938053097345133, 'fraction_numerical': 0.0, 'mean_word_length': 1.7220616570327554, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, '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': '711076', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1359, 'n_words': 542}