Anonymous avatar Anonymous committed d894ed9

add support for simple forms. just pass a dict as payload in your
resouce or body in restclient.

Comments (0)

Files changed (4)

docs/resource.txt

     >>> from restclient import Resource
     >>> res = Resource('http://friendpaste.com')
     >>> res.get('/5rOqE9XTz7lccLgZoQS4IP',headers={'Accept': 'application/json'})
-    '{"snippet": "hi!", "title": "", "id": "5rOqE9XTz7lccLgZoQS4IP", "language": "text", "revision": "386233396230"}'
+    u'{"snippet": "hi!", "title": "", "id": "5rOqE9XTz7lccLgZoQS4IP", "language": "text", "revision": "386233396230"}'
     >>> res.get('/5rOqE9XTz7lccLgZoQS4IP',headers={'Accept': 'application/json'}).http_code
     200
+    
+Data returned are unicode or binaries.
 
 Resource class
 --------------

restclient/rest.py

             elif isinstance(body, types.StringTypes):
                 size = len(body)
                 body = to_bytestring(body)
+            elif isinstance(body, dict):
+                body = form_encode(body)
+                size = len(body)
             else:
                 raise RequestError('Unable to calculate '
                     'the length of the data parameter. Specify a value for '
             urllib.quote_plus(value)))
 
     return '&'.join(tmp)
+    
+def form_encode(obj, charser="utf8"):
+    tmp = []
+    for key, value in obj.items():
+        tmp.append("%s=%s" % (url_quote(key), 
+                url_quote(value)))
+    return to_bytestring(";".join(tmp))
 
 import cgi
 

tests/_server_test.py

                 self._respond(200, extra_headers, "ok")
             else:
                 self.error_Response()
+        elif path == "/form":
+            content_type = self.headers.get('content-type', 'text/plain')
+            extra_headers.append(('Content-type', content_type))
+            content_length = int(self.headers.get('Content-length', 0))
+            body = self.rfile.read(content_length)
+            form = urlparse.parse_qs(body)
+            if form['a'] == ["a"] and form["b"] == ["b"]:
+                self._respond(200, extra_headers, "ok")
+            else:
+                self.error_Response()
         else:
             self.error_Response('Bad path')
+            
     do_PUT = do_POST
 
     def do_DELETE(self):

tests/resource_test.py

     def testPostWithQuery(self):
         result = self.res.post('/query', test="testing")
         self.assert_(self.res.response.status == 200)
+    
+    def testPostForm(self):
+        result = self.res.post('/form', payload={ "a": "a", "b": "b" })
+        self.assert_(self.res.response.status == 200)
 
     def testSimplePut(self):
         result = self.res.put(payload="test")
             res = Resource(self.url, transport)
             result = res.get('/auth')
         self.assertRaises(Unauthorized, niettest)
+        
+    
 
  
     
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.