def _fetchtoken(self):
        """This method tries to get token string needed for authentication on D*.

        :returns: token string
        """
        request = self.get('stream')
        token = self._token_regex.search(request.text)
        if token is None: token = self._token_regex_2.search(request.text)
        if token is not None: token = token.group(1)
        else: raise errors.TokenError('could not find valid CSRF token')
        self._token = token
        return token
Beispiel #2
0
    def get_token(self, fetch=True):
        """This function returns a token needed for authentication in most cases.
        **Notice:** using repr() is recommended method for getting token.

        Each time it is run a _fetchtoken() is called and refreshed token is stored.

        It is more safe to use than _fetchtoken().
        By setting new you can request new token or decide to get stored one.
        If no token is stored new one will be fetched anyway.

        :returns: string -- token used to authenticate
        """
        try:
            if fetch or not self._token: self._fetchtoken()
        except requests.exceptions.ConnectionError as e:
            warnings.warn('{0} was cought: reusing old token'.format(e))
        finally:
            if not self._token: raise errors.TokenError('cannot obtain token and no previous token found for reuse')
        return self._token