This is not a good commit. Broken many things.
authorMarek Marecki <triviuss@gmail.com>
Mon, 25 Mar 2013 15:58:17 +0000 (16:58 +0100)
committerMarek Marecki <triviuss@gmail.com>
Mon, 25 Mar 2013 15:58:17 +0000 (16:58 +0100)
diaspy/client.py
manual/connecting_to_pod.mdown
tests.py

index 2dc088e5942e580c94d6403dfa03d9068f0196bc..55f22eee35281b0f8ae247cd9cc2eb7970ea135c 100644 (file)
@@ -21,6 +21,7 @@ class Client:
         self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token')
         self.pod = pod
         self.session = requests.Session()
+        self._post_data = {}
         self._setlogindata(username, password)
 
     def get_token(self):
@@ -56,7 +57,29 @@ class Client:
                               headers={'accept': 'application/json'})
         
         if r.status_code != 201: raise Exception('{0}: Login failed.'.format(r.status_code))
-    
+
+    def _set_post_data(self, text, aspect_id, photos):
+        """This method prepares data for post.
+
+        :param text: Text to post.
+        :type text: str
+        :param aspect_id: Aspect id to send post to.
+        :type aspect_id: str
+        """
+        data = {'aspect_ids': aspect_id,
+                'status_message': {'text': text}}
+        if photos: data['photos'] = photos
+        self._post_data = data
+
+    def _send_post(self):
+        """This method sends a post to an aspect. 
+        It uses `self._post_data`.
+
+        .. note::
+            _set_post_data() must be called before.
+        """
+
+
     def post(self, text, aspect_id='public', photos=None):
         """This function sends a post to an aspect
 
@@ -68,13 +91,9 @@ class Client:
         :returns: diaspy.models.Post -- the Post which has been created
 
         """
-        data = {'aspect_ids': aspect_id,
-                'status_message': {'text': text}}
-
-        if photos:
-            data['photos'] = photos
+        self._set_post_data(text, aspect_id, photos)
         r = self.session.post('{0}/status_messages'.format(self.pod),
-                              data=json.dumps(data),
+                              data=json.dumps(self._post_data),
                               headers={'content-type': 'application/json',
                                        'accept': 'application/json',
                                        'x-csrf-token': self.get_token()})
index 93267bbf271a177b6e5bd88c4d02883dea28d3a0..83838c9fa3174228e8bacba5820ef0648889d074 100644 (file)
@@ -6,7 +6,7 @@ Then, if no errors are raised, you can `_login()` to the pod with given username
 
     import diaspy
 
-    client = diaspy.Client(pod="http://pod.example.com/", username="foo", password="bar")
+    client = diaspy.Client(pod="http://pod.example.com", username="foo", password="bar")
     client._login()
 
 
index 8cca3697cdc465288ae8366a53d906059181a17a..d64b235fdbb325aea7dc52d0e31d89a400bb4e72 100644 (file)
--- a/tests.py
+++ b/tests.py
@@ -1,8 +1,9 @@
 #!/usr/bin/env python3
 
 import unittest
+import getpass
 
-#   failing to import any of the modules below indicates failed tests
+#   failure to import any of the modules below indicates failed tests
 #   modules used by diaspy
 import requests, re
 #   actual diaspy code
@@ -11,22 +12,32 @@ import diaspy
 
 ####    test suite configuration variables: can be adjusted to your liking
 #   pod used by tests (has to be valid)
-__pod__ = "http://pod.orkz.net"
+__pod__ = 'http://pod.orkz.net'
+__username__ = 'testuser'
+__passwd__ = 'testpassword'
 
 
 class ClientTests(unittest.TestCase):
-    def testInit(self):
+    def testInitialization(self):
+        """This test checks initialization of Client() instance.
         """
-        This test checks initialization of Client() instance.
-        """
-        client = diaspy.client.Client(pod=__pod__, username='testuser', password='testpassword')
+        client = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__)
         self.assertEqual(__pod__, client.pod)
-        self.assertEqual('testuser', client._username)
-        self.assertEqual('testpassword', client._password)
+        self.assertEqual(__username__, client._username)
+        self.assertEqual(__passwd__, client._password)
+        self.assertEqual(None, client._post_data)
         self.assertEqual(client._token_regex, re.compile(r'content="(.*?)"\s+name="csrf-token'))
         self.assertEqual(client._login_data['user[username]'], 'testuser')
         self.assertEqual(client._login_data['user[password]'], 'testpassword')
         self.assertEqual(client._login_data['authenticity_token'], client.get_token())
 
+    def testPreparationOfPostData(self):
+        """This test checks correctness of data set for posting.
+        """
+
+
     
-if __name__ == "__main__": unittest.main()
+if __name__ == '__main__': 
+    __passwd__ = getpass.getpass(prompt='Password used for testing: ')
+    if __passwd__ == '': __passwd__ = 'testpassword'
+    unittest.main()