Further work being done on streams; all basic streams implemented
[diaspy.git] / diaspy / client.py
index 028017261cc4a4dd048831bc65b41a0370a64316..8eaab4461198257b7e1d2562d5af441b9bf6eba2 100644 (file)
@@ -1,5 +1,5 @@
-import json
 import diaspy.models
+import diaspy.streams
 import diaspy.connection
 
 
@@ -18,37 +18,7 @@ class Client:
         self.connection = diaspy.connection.Connection(pod, username, password)
         self.connection.login()
         self.pod = pod
-
-    def _setpostdata(self, text, aspect_ids, photos):
-        """This function prepares data for posting.
-
-        :param text: Text to post.
-        :type text: str
-        :param aspect_ids: Aspect ids to send post to.
-        :type aspect_ids: str
-        """
-        data = {}
-        data['aspect_ids'] = aspect_ids
-        data['status_message'] = {'text': text}
-        if photos:
-            data['photos'] = photos
-        self._post_data = data
-
-    def _post(self):
-        """Sends post to an aspect.
-
-        :returns: diaspy.models.Post -- the Post which has been created
-        """
-        r = self.connection.post('status_messages',
-                                 data=json.dumps(self._post_data),
-                                 headers={'content-type': 'application/json',
-                                          'accept': 'application/json',
-                                          'x-csrf-token': self.get_token()})
-        if r.status_code != 201:
-            raise Exception('{0}: Post could not be posted.'.format(
-                            r.status_code))
-
-        return diaspy.models.Post(str(r.json()['id']), self.connection)
+        self.stream = diaspy.streams.Stream(self.connection, 'stream.json')
 
     def post(self, text, aspect_ids='public', photos=None):
         """This function sends a post to an aspect
@@ -60,49 +30,31 @@ class Client:
 
         :returns: diaspy.models.Post -- the Post which has been created
         """
-        self._setpostdata(text, aspect_ids, photos)
-        post = self._post()
-        self._post_data = {}
+        post = self.stream.post(text, aspect_ids, photos)
         return post
 
-    def get_user_info(self):
-        """This function returns the current user's attributes.
-
-        :returns: dict -- json formatted user info.
-        """
-        return self.connection.getUserInfo()
-
     def post_picture(self, filename):
         """This method posts a picture to D*.
 
         :param filename: Path to picture file.
         :type filename: str
         """
-        aspects = self.get_user_info()['aspects']
-        params = {}
-        params['photo[pending]'] = 'true'
-        params['set_profile_image'] = ''
-        params['qqfile'] = filename
-        for i, aspect in enumerate(aspects):
-            params['photo[aspect_ids][%d]' % (i)] = aspect['id']
-
-        data = open(filename, 'rb')
-
-        headers = {'content-type': 'application/octet-stream',
-                   'x-csrf-token': self.get_token(),
-                   'x-file-name': filename}
+        return self.stream.post_picture(filename)
 
-        r = self.connection.post('photos', params=params, data=data, headers=headers)
-        return r
+    def get_activity(self):
+        """This function returns activity stream.
+        :returns: diaspy.streams.Activity
+        """
+        activity = diaspy.streams.Activity(self.connection, 'activity.json')
+        return activity
 
     def get_stream(self):
-        """This functions returns a list of posts found in the stream.
+        """This functions returns stream.
 
-        :returns: list -- list of Post objects.
+        :returns: diaspy.streams.Stream
         """
-        stream = diaspy.models.Stream(self.connection)
-        stream.update()
-        return stream
+        self.stream.update()
+        return self.stream
 
     def get_notifications(self):
         """This functions returns a list of notifications.
@@ -169,7 +121,7 @@ class Client:
         :type aspect_id: str
 
         """
-        data = {'authenticity_token': self.get_token(),
+        data = {'authenticity_token': self.connection.getToken(),
                 'aspect_id': aspect_id,
                 'person_id': user_id}
 
@@ -182,15 +134,8 @@ class Client:
     def add_aspect(self, aspect_name, visible=0):
         """ This function adds a new aspect.
         """
-
-        data = {'authenticity_token': self.get_token(),
-                'aspect[name]': aspect_name,
-                'aspect[contacts_visible]': visible}
-
-        r = self.connection.post('aspects', data=data)
-
-        if r.status_code != 200:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
+        aspects = diaspy.streams.Aspects(self.connection)
+        aspects.add(aspect_name, visible)
 
     def remove_user_from_aspect(self, user_id, aspect_id):
         """ this function removes a user from an aspect.
@@ -201,7 +146,7 @@ class Client:
         :type aspect_id: str
 
         """
-        data = {'authenticity_token': self.get_token(),
+        data = {'authenticity_token': self.connection.getToken(),
                 'aspect_id': aspect_id,
                 'person_id': user_id}
 
@@ -216,7 +161,7 @@ class Client:
     def remove_aspect(self, aspect_id):
         """ This function adds a new aspect.
         """
-        data = {'authenticity_token': self.get_token()}
+        data = {'authenticity_token': self.connection.getToken()}
 
         r = self.connection.delete('aspects/{}'.format(aspect_id),
                                    data=data)