class Connection():
- """Object representing connection with the server.
- It is pushed around internally and is considered private.
+ """Object representing connection with the pod.
"""
_token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token')
_userinfo_regex = re.compile(r'window.current_user_attributes = ({.*})')
- login_data = {}
- token = ''
- def __init__(self, pod, username='', password=''):
+ def __init__(self, pod, username='', password='', schema='https'):
"""
:param pod: The complete url of the diaspora pod to use.
:type pod: str
"""
self.pod = pod
self.session = requests.Session()
- try:
- self._setlogin(username, password)
- except Exception as e:
- raise LoginError('cannot create login data (caused by: {0}'.format(e))
+ self.login_data = {}
+ self.token = ''
+ try: self._setlogin(username, password)
+ except requests.exceptions.MissingSchema:
+ self.pod = '{0}://{1}'.format(schema, self.pod)
+ warnings.warn('schema was missing')
+ finally: pass
+ try: self._setlogin(username, password)
+ except Exception as e: raise LoginError('cannot create login data (caused by: {0})'.format(e))
+
+ def __repr__(self):
+ """Returns token string.
+ It will be easier to change backend if programs will just use:
+ repr(connection)
+ instead of calling a specified method.
+ """
+ return self.get_token()
def get(self, string, headers={}, params={}):
"""This method gets data from session.
:returns: dict -- json formatted user info.
"""
request = self.get('bookmarklet')
- userdata = json.loads(self._userinfo_regex.search(request.text).group(1))
+ try:
+ userdata = json.loads(self._userinfo_regex.search(request.text).group(1))
+ except AttributeError:
+ raise errors.DiaspyError('cannot find user data')
return userdata
def _fetchtoken(self):
"""
request = self.get('stream')
token = self._token_regex.search(request.text).group(1)
+ self.token = token
return token
- def get_token(self, new=False):
+ def get_token(self, fetch=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.
:returns: string -- token used to authenticate
"""
try:
- if new: self.token = self._fetchtoken()
- if not self.token: self.token = self._fetchtoken()
+ if fetch: self._fetchtoken()
+ if not self.token: self._fetchtoken()
except requests.exceptions.ConnectionError as e:
warnings.warn('{0} was cought: reusing old token'.format(e))
finally: