pod, user = handle[1], handle[0]
return (pod, user)
- def _gethandle(self, diaspora_id, protocol='https'):
- """Get user data using handle.
+ def _postproc(self, request):
+ """Makes necessary modifications to user data and
+ sets up a stream.
+
+ :param request: request object
+ :type request: request
"""
- pod, user = self._sephandle(diaspora_id)
- request = self._connection.session.get('{0}://{1}/u/{2}.json'.format(protocol, pod, user))
if request.status_code != 200:
- raise Exception('wrong error code: {0}'.format())
+ raise Exception('wrong error code: {0}'.format(request.status_code))
else:
request = request.json()
- data = request[0]['author']
- self.data = data
+ data, final = request[0]['author'], {}
+ names = [('id', 'id'),
+ ('diaspora_id', 'diaspora_id'),
+ ('guid', 'guid'),
+ ('name', 'diaspora_name'),
+ ('avatar', 'image_urls'),
+ ]
+ for d, f in names:
+ final[f] = data[d]
+ self.data = final
self.stream = Outer(self._connection, location='people/{0}.json'.format(self.data['guid']))
+ def _gethandle(self, diaspora_id, protocol='https'):
+ """Get user data using handle.
+ """
+ pod, user = self._sephandle(diaspora_id)
+ request = self._connection.session.get('{0}://{1}/u/{2}.json'.format(protocol, pod, user))
+ self._postproc(request)
+
def _getguid(self, guid):
"""Get user data using guid.
"""
request = self._connection.get('people/{0}.json'.format(guid))
- if request.status_code != 200:
- raise Exception('wrong error code: {0}'.format())
- else:
- request = request.json()
- data = request[0]['author']
- self.data = data
- self.stream = Outer(self._connection, location='people/{0}.json'.format(self.data['guid']))
+ self._postproc(request)
def fetchguid(self, guid):
"""Fetch user data using guid.
'12345678abcdefgh'
-User object contains following items:
+User object contains following items in its `data` dict:
* `id`, `str`, id of the user;
* `guid`, `str`, guid of the user;
* `diaspora_id`, `str`, D\* id of the user;
-* `name`, `str`, name of the user;
-* `avatar`, `dict`, links to avatar of the user;
+* `diaspora_name`, `str`, name of the user;
+* `image_urls`, `dict`, links to avatar of the user;
+
+Also `User()` object contains a stream for this user.
+
* `stream`, `diaspy.streams.Outer`, stream of the user (provides all methods of generic stream);
__passwd__ = 'strong_password'
diaspora_id = 'user@pod.example.com'
guid = '12345678abcdefgh'
+ # your name as others see it
+ diaspora_name = 'Marek Marecki'
test_count_file = open('TEST_COUNT', 'w')
test_count_file.write(str(test_count))
test_count_file.close()
+
+# Test connection setup
print('Running test no. {0}'.format(test_count))
+print('Running tests on connection to pod: "{0}"'.format(__pod__))
-print('Running tests on connection to pod: "{0}"\t'.format(__pod__), end='')
-test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
-test_connection.login()
-print('[ CONNECTED ]\n')
+print('Connecting to pod...\t', end='')
+try:
+ test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
+ test_connection.login()
+ print('[ CONNECTED ]\n')
+ err = False
+except:
+ print('[ FAIL ]')
+ input('Hit [Return] to continue...')
+ err = True
+finally:
+ if err: raise
-#### Test suite code
+#######################################
+#### TEST SUITE CODE ####
+#######################################
class ConnectionTest(unittest.TestCase):
def testLoginWithoutUsername(self):
connection = diaspy.connection.Connection(pod=__pod__)
user = diaspy.people.User(test_connection)
user.fetchhandle(testconf.diaspora_id)
self.assertEqual(testconf.guid, user['guid'])
- self.assertEqual(testconf.name, user['name'])
+ self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
self.assertIn('id', user.data)
- self.assertIn('avatar', user.data)
+ self.assertIn('image_urls', user.data)
self.assertEqual(type(user.stream), diaspy.streams.Outer)
def testGettingUserByGUID(self):
user = diaspy.people.User(test_connection)
user.fetchguid(testconf.guid)
self.assertEqual(testconf.diaspora_id, user['diaspora_id'])
- self.assertEqual(testconf.name, user['name'])
+ self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
self.assertIn('id', user.data)
- self.assertIn('avatar', user.data)
+ self.assertIn('image_urls', user.data)
self.assertEqual(type(user.stream), diaspy.streams.Outer)