Client() rewritten to use Stream() object
authorMarek Marecki <triviuss@gmail.com>
Tue, 30 Apr 2013 22:30:56 +0000 (00:30 +0200)
committerMarek Marecki <triviuss@gmail.com>
Tue, 30 Apr 2013 22:30:56 +0000 (00:30 +0200)
README.md [new file with mode: 0644]
diaspy/client.py
diaspy/models.py
tests.py

diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..213fab7
--- /dev/null
+++ b/README.md
@@ -0,0 +1,36 @@
+#### Python API for Diaspora (unofficial)
+
+`diaspy` is a set of modules which form API for D\* social network. 
+The API is written in Python 3.x and is not Python 2.x compatible. 
+
+Object oriented design of `diaspy` makes it easily reusable by other 
+developers who want to use only part of the API.
+
+----
+
+#### Quick intro
+
+#### 1. Posting text to your stream
+
+You only need two objects to do this: `Stream()` and `Connection()`. 
+
+    >>> import diaspy
+    >>> c = diaspy.connection.Connection(pod='https://pod.example.com',
+    ...                                  username='foo',
+    ...                                  password='bar')
+    >>> c.login()
+    >>> stream = diaspy.models.Stream(c)
+    >>> stream.post('Your first post')
+
+
+#### 2. More features
+
+There is a special `client` module in diaspy which is an example client 
+of D\* written using the `diapsy` API. It provides many features useful for 
+interactions with social network like messages, mentions, likes etc. 
+It is full of good, useful stuff.
+
+----
+
+To get more information about how the code works read 
+documentation (`./doc/` directory) and manual (`./manual/` directory).
index 028017261cc4a4dd048831bc65b41a0370a64316..3f1537832dc3a8971fc8b179c7d1c1de360d63f4 100644 (file)
@@ -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.models.Stream(self.connection)
 
     def post(self, text, aspect_ids='public', photos=None):
         """This function sends a post to an aspect
@@ -60,49 +30,24 @@ 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}
-
-        r = self.connection.post('photos', params=params, data=data, headers=headers)
-        return r
+        return self.stream.post_picture(filename)
 
     def get_stream(self):
-        """This functions returns a list of posts found in the stream.
+        """This functions returns user's stream.
 
-        :returns: list -- list of Post objects.
+        :returns: diaspy.models.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.
index 8bf71a93d3a3375017b5f76e920004c3f8a4da5e..c62d5c4f2debf4317e9992d6860b2d9cf977cb68 100644 (file)
@@ -220,7 +220,6 @@ class Stream:
                             request.status_code))
 
         post = Post(str(request.json()['id']), self._connection)
-        self.update()
         return post
 
     def post_picture(self, filename):
@@ -244,5 +243,4 @@ class Stream:
                    'x-file-name': filename}
         request = self._connection.post('photos', params=params, data=data, headers=headers)
         data.close()
-        self.update()
         return request
index 4a2ec475fd725a22d82c4e5f44100d50d9f54c73..795712b6635de172dcc8cdcdd51f07dea3570059 100644 (file)
--- a/tests.py
+++ b/tests.py
@@ -43,7 +43,7 @@ class StreamTest(unittest.TestCase):
         c = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
         c.login()
         stream = diaspy.models.Stream(c)
-        post = stream.post('`diaspy` test \n#diaspy')
+        post = stream.post('#diaspy test')
         self.assertEqual(diaspy.models.Post, type(post))
 
     def testPostingImage(self):
@@ -93,4 +93,14 @@ class ClientTests(unittest.TestCase):
         self.assertEqual(list, type(mailbox))
         self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0]))
 
+    def testPostingImage(self):
+        c = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__)
+        c.post_picture('./test-image.png')
+
+    def testPostingText(self):
+        c = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__)
+        post = c.post('#diaspy test')
+        self.assertEqual(diaspy.models.Post, type(post))
+
+
 if __name__ == '__main__': unittest.main()