From ff3d2ab42e2ef8e5f5db6b451cbafcc6e1b2b365 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Mon, 10 Jun 2013 14:26:20 +0200 Subject: [PATCH] More changes in token getting procedure, now you can specify whether you want new token or try to reuse stored one --- diaspy/connection.py | 16 +++++++++------- diaspy/streams.py | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/diaspy/connection.py b/diaspy/connection.py index d2f2044..43f8ce1 100644 --- a/diaspy/connection.py +++ b/diaspy/connection.py @@ -3,6 +3,7 @@ import re import requests import json +import warnings """This module abstracts connection to pod. @@ -154,20 +155,21 @@ class Connection(): token = self._token_regex.search(request.text).group(1) return token - def get_token(self): + def get_token(self, new=False): """This function returns a token needed for authentication in most cases. 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 fatched anyway. :returns: string -- token used to authenticate """ try: - token = self._fetchtoken() + if new: self.token = self._fetchtoken() + if not self.token: self.token = self._fetchtoken() except requests.exceptions.ConnectionError as e: - warnings.warn('{0} was cought: trying to reuse old token'.format(e)) - token = self.token + warnings.warn('{0} was cought: reusing old token'.format(e)) finally: - if not token: raise TokenError('cannot obtain token and no previous token found for reuse') - self.token = token - return token + if not self.token: raise TokenError('cannot obtain token and no previous token found for reuse') + return self.token diff --git a/diaspy/streams.py b/diaspy/streams.py index fa19d97..f9c3231 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -351,7 +351,7 @@ class FollowedTags(Generic): class Tag(Generic): """This stream contains all posts containing a tag. """ - def __init__(connection, tag): + def __init__(self, connection, tag): """ :param connection: Connection() object :type connection: diaspy.connection.Connection -- 2.25.1