Regular expressions fixed
[diaspy.git] / diaspy / client.py
index 29730878a44a8c92b462309ac0bc47dc9bec0efc..a4540f74cc198677696209095d6bda60c8d959e2 100644 (file)
@@ -1,15 +1,17 @@
 import diaspy.models
 import diaspy.streams
 import diaspy.connection
+from diaspy import notifications
 
 
 class Client:
-    """This is the client class to connect to Diaspora.
+    """This is the client class used to interact with Diaspora.
+    It can be used as a reference implementation of client using diaspy.
     """
     def __init__(self, pod, username='', password=''):
         """
         `pod` can also be a diaspy.connection.Connection type and
-        Client() will detect it. When giving a connection there is no need 
+        Client() will detect it. When giving a connection there is no need
         to pass username and password.
 
         :param pod: The complete url of the diaspora pod to use
@@ -27,34 +29,26 @@ class Client:
             self.connection.login()
         self.stream = diaspy.streams.Stream(self.connection, 'stream.json')
 
-    def post(self, text, aspect_ids='public', photos=None):
+    def post(self, text, aspect_ids='public', photos=None, photo=''):
         """This function sends a post to an aspect
 
-        :param text: Text to post.
+        :param text: text to post
         :type text: str
         :param aspect_ids: Aspect ids to send post to.
         :type aspect_ids: str
-
+        :param photo: path to picture file
+        :type photo: str
         :returns: diaspy.models.Post -- the Post which has been created
         """
-        post = self.stream.post(text, aspect_ids, photos)
+        post = self.stream.post(text, aspect_ids, photos, photo)
         return post
 
-    def post_picture(self, filename):
-        """This method posts a picture to D*.
-
-        :param filename: Path to picture file.
-        :type filename: str
-        """
-        return self.stream.post_picture(filename)
-
     def get_activity(self):
         """This function returns activity stream.
 
         :returns: diaspy.streams.Activity
         """
-        activity = diaspy.streams.Activity(self.connection, 'activity.json')
-        return activity
+        return diaspy.streams.Activity(self.connection, 'activity.json')
 
     def get_stream(self):
         """This functions returns stream.
@@ -65,7 +59,7 @@ class Client:
         return self.stream
 
     def get_aspects(self):
-        """Returns /aspects stream.
+        """Returns aspects stream.
 
         :returns: diaspy.streams.Aspects
         """
@@ -85,37 +79,21 @@ class Client:
         """
         return diaspy.streams.FollowedTags(self.connection)
 
-    def get_tag(self, tag, stream=False):
+    def get_tag(self, tag):
         """This functions returns a list of posts containing the tag.
         :param tag: Name of the tag
         :type tag: str
-        :param stream: specify wheter you want a stream object (True) or
-        normal list (False)
-        :type stream: bool
 
-        :returns: list -- list of Post objects
+        :returns: diaspy.streams.Generic -- stream containg posts with given tag
         """
-        if stream:
-            tagged_posts = diaspy.streams.Generic(self.connection, location='tags/{0}.json'.format(tag))
-        else:
-            r = self.connection.get('tags/{0}.json'.format(tag))
-            if r.status_code != 200:
-                raise Exception('wrong status code: {0}'.format(r.status_code))
-            tagged_posts = [diaspy.models.Post(str(post['id']), self.connection) for post in r.json()]
-        return tagged_posts
+        return diaspy.streams.Generic(self.connection, location='tags/{0}.json'.format(tag))
 
     def get_notifications(self):
         """This functions returns a list of notifications.
 
         :returns: list -- list of json formatted notifications
         """
-        r = self.connection.get('notifications.json')
-
-        if r.status_code != 200:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
-
-        notifications = r.json()
-        return notifications
+        return notifications.Notifications(self.connection)
 
     def get_mailbox(self):
         """This functions returns a list of messages found in the conversation.
@@ -128,9 +106,19 @@ class Client:
             raise Exception('wrong status code: {0}'.format(r.status_code))
 
         mailbox = r.json()
-        return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self.connection)
+        return [diaspy.conversations.Conversation(self.connection, conversation['conversation']['id'])
                 for conversation in mailbox]
 
+    def add_aspect(self, aspect_name, visible=0):
+        """This function adds a new aspect.
+        """
+        diaspy.streams.Aspects(self.connection).add(aspect_name, visible)
+
+    def remove_aspect(self, aspect_id):
+        """This function removes an aspect.
+        """
+        diaspy.streams.Aspects(self.connection).remove(aspect_id)
+
     def add_user_to_aspect(self, user_id, aspect_id):
         """ this function adds a user to an aspect.
 
@@ -140,21 +128,7 @@ class Client:
         :type aspect_id: str
 
         """
-        data = {'authenticity_token': self.connection.get_token(),
-                'aspect_id': aspect_id,
-                'person_id': user_id}
-
-        r = self.connection.post('aspect_memberships.json', data=data)
-
-        if r.status_code != 201:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
-        return r.json()
-
-    def add_aspect(self, aspect_name, visible=0):
-        """ This function adds a new aspect.
-        """
-        aspects = diaspy.streams.Aspects(self.connection)
-        aspects.add(aspect_name, visible)
+        return diaspy.models.Aspect(self.connection, aspect_id).addUser(user_id)
 
     def remove_user_from_aspect(self, user_id, aspect_id):
         """ this function removes a user from an aspect.
@@ -165,23 +139,7 @@ class Client:
         :type aspect_id: str
 
         """
-        data = {'authenticity_token': self.connection.get_token(),
-                'aspect_id': aspect_id,
-                'person_id': user_id}
-
-        r = self.connection.delete('aspect_memberships/42.json',
-                                   data=data)
-
-        if r.status_code != 200:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
-
-        return r.json()
-
-    def remove_aspect(self, aspect_id):
-        """ This function removes an aspect.
-        """
-        aspects = diaspy.streams.Aspects(self.connection)
-        aspects.remove(aspect_id)
+        return diaspy.models.Aspect(self.connection, aspect_id).removeUser(user_id)
 
     def new_conversation(self, contacts, subject, text):
         """Start a new conversation.