Merge branch 'master' of https://github.com/Javafant/diaspora-api into contacts
[diaspy.git] / diaspy / client.py
index 8eaab4461198257b7e1d2562d5af441b9bf6eba2..ce07990a70cf33be9fbcfaf94e89de21b47d9a44 100644 (file)
@@ -1,52 +1,54 @@
 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):
+    def __init__(self, pod, username='', password=''):
         """
-        :param pod: The complete url of the diaspora pod to use.
+        `pod` can also be a diaspy.connection.Connection type and
+        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
+        (or Connection() object).
         :type pod: str
         :param username: The username used to log in.
         :type username: str
         :param password: The password used to log in.
         :type password: str
         """
-        self.connection = diaspy.connection.Connection(pod, username, password)
-        self.connection.login()
-        self.pod = pod
+        if type(pod) == diaspy.connection.Connection:
+            self.connection = pod
+        else:
+            self.connection = diaspy.connection.Connection(pod, username, password)
+            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.
@@ -56,47 +58,42 @@ class Client:
         self.stream.update()
         return self.stream
 
-    def get_notifications(self):
-        """This functions returns a list of notifications.
+    def get_aspects(self):
+        """Returns aspects stream.
 
-        :returns: list -- list of json formatted notifications
+        :returns: diaspy.streams.Aspects
         """
-        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 diaspy.streams.Aspects(self.connection)
 
     def get_mentions(self):
-        """This functions returns a list of
-        posts the current user is being mentioned in.
+        """Returns /mentions stream.
 
-        :returns: list -- list of Post objects
+        :returns: diaspy.streams.Mentions
         """
-        r = self.connection.get('mentions.json')
+        return diaspy.streams.Mentions(self.connection)
 
-        if r.status_code != 200:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
+    def get_followed_tags(self):
+        """Returns followed tags stream.
 
-        mentions = r.json()
-        return [diaspy.models.Post(str(post['id']), self.connection) for post in mentions]
+        :returns: diaspy.streams.FollowedTags
+        """
+        return diaspy.streams.FollowedTags(self.connection)
 
     def get_tag(self, tag):
         """This functions returns a list of posts containing the tag.
         :param tag: Name of the tag
         :type tag: str
 
-        :returns: list -- list of Post objects
+        :returns: diaspy.streams.Generic -- stream containg posts with given tag
         """
-        r = self.connection.get('tags/{0}.json'.format(tag))
+        return diaspy.streams.Generic(self.connection, location='tags/{0}.json'.format(tag))
 
-        if r.status_code != 200:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
+    def get_notifications(self):
+        """This functions returns a list of notifications.
 
-        tagged_posts = r.json()
-        return [diaspy.models.Post(str(post['id']), self.connection) for post in tagged_posts]
+        :returns: list -- list of json formatted notifications
+        """
+        return notifications.Notifications(self.connection)
 
     def get_mailbox(self):
         """This functions returns a list of messages found in the conversation.
@@ -112,6 +109,16 @@ class Client:
         return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self.connection)
                 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.
 
@@ -121,21 +128,7 @@ class Client:
         :type aspect_id: str
 
         """
-        data = {'authenticity_token': self.connection.getToken(),
-                '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.
@@ -146,28 +139,7 @@ class Client:
         :type aspect_id: str
 
         """
-        data = {'authenticity_token': self.connection.getToken(),
-                '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 adds a new aspect.
-        """
-        data = {'authenticity_token': self.connection.getToken()}
-
-        r = self.connection.delete('aspects/{}'.format(aspect_id),
-                                   data=data)
-
-        if r.status_code != 404:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
+        return diaspy.models.Aspect(self.connection, aspect_id).removeUser(user_id)
 
     def new_conversation(self, contacts, subject, text):
         """Start a new conversation.
@@ -183,7 +155,7 @@ class Client:
                 'conversation[subject]': subject,
                 'conversation[text]': text,
                 'utf8': '✓',
-                'authenticity_token': self.connection.getToken()}
+                'authenticity_token': self.connection.get_token()}
 
         r = self.connection.post('conversations/',
                                  data=data,