First commit of curl command from request
authorMike Waldner <mwaldner@gilt.com>
Mon, 15 Aug 2011 04:12:14 +0000 (00:12 -0400)
committerMike Waldner <mwaldner@gilt.com>
Mon, 15 Aug 2011 04:12:14 +0000 (00:12 -0400)
requests/models.py

index 08f3e32146af92b7f1e4039d9ff140e7a8345c60..9e0b0ddb827666340a02d2e7171ad14ac5f2d1a9 100644 (file)
@@ -328,6 +328,37 @@ class Request(object):
 
         return self.sent
 
+    @property
+    def curl(self):
+        """Creates a curl command from the request"""
+
+        #TODO - Auth. User names and accounts
+        #TODO - Query string...
+        #TODO - Files...How do I do files???
+
+        #: --location - if there is a redirect, redo request on the new place
+        curl_cmd = 'curl --location '
+
+        if self.method.upper() == 'HEAD':
+            #: --head - fetch headers only
+            method_opt = '--head '
+        else:
+            #: --request - specify request method
+            method_opt = '--request %s ' % self.method.upper()
+
+        data = ''
+        if self.method in ('PUT', 'POST', 'PATCH'):
+            #: --data - send specified data in post request.
+            #: '-data name=John -data skill=Doe' generates the
+            #: post chunk 'name=daniel&skill=lousy'
+
+            #if data is file:
+            if isinstance(self.data, (list, tuple)):
+                data = data.join(['--data ' + key + '=' + value + ' ' for key, value in self.data])
+
+        curl_cmd = curl_cmd + method_opt + data + self.url
+
+        return curl_cmd
 
 
 class Response(object):