From 430e87d0fd738adde494ccfe7d3fb3882fd8ca02 Mon Sep 17 00:00:00 2001 From: Mike Waldner Date: Mon, 15 Aug 2011 00:12:14 -0400 Subject: [PATCH 1/6] First commit of curl command from request --- requests/models.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/requests/models.py b/requests/models.py index 08f3e321..9e0b0ddb 100644 --- a/requests/models.py +++ b/requests/models.py @@ -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): From aad27218d58e82637c43f92c85237fa90f0178c6 Mon Sep 17 00:00:00 2001 From: Mike Waldner Date: Tue, 16 Aug 2011 01:39:02 -0400 Subject: [PATCH 2/6] Using build_url and also including headers --- requests/models.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index 9e0b0ddb..5e1474d5 100644 --- a/requests/models.py +++ b/requests/models.py @@ -333,12 +333,16 @@ class Request(object): """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 ' + #: --header - Extra header to use when getting a web page + header = '' + if self.headers: + header = header.join(['--header "' + key + ':' + value + '" ' for key, value in self.headers.iteritems()]) + if self.method.upper() == 'HEAD': #: --head - fetch headers only method_opt = '--head ' @@ -355,8 +359,11 @@ class Request(object): #if data is file: if isinstance(self.data, (list, tuple)): data = data.join(['--data ' + key + '=' + value + ' ' for key, value in self.data]) + else: + data = '--data ' + self._enc_data + ' ' - curl_cmd = curl_cmd + method_opt + data + self.url + #: Params handled in _build_url + curl_cmd = curl_cmd + method_opt + data + header + '"' + self._build_url() + '"' return curl_cmd From 2a17e5237c90a537c96b486ef30cddf8b544a966 Mon Sep 17 00:00:00 2001 From: Mike Waldner Date: Tue, 23 Aug 2011 22:03:42 -0400 Subject: [PATCH 3/6] Ive never seen anyone use the --'s unix command options --- requests/models.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/requests/models.py b/requests/models.py index 5e1474d5..9c51bb8a 100644 --- a/requests/models.py +++ b/requests/models.py @@ -332,23 +332,24 @@ class Request(object): def curl(self): """Creates a curl command from the request""" - #TODO - Auth. User names and accounts - #TODO - Files...How do I do files??? + #TODO - Auth with User names and accounts + #TODO - Files + #TODO - OAuth - #: --location - if there is a redirect, redo request on the new place - curl_cmd = 'curl --location ' + #: -L/--location - if there is a redirect, redo request on the new place + curl_cmd = 'curl -L ' - #: --header - Extra header to use when getting a web page + #: -H/--header - Extra header to use when getting a web page header = '' if self.headers: - header = header.join(['--header "' + key + ':' + value + '" ' for key, value in self.headers.iteritems()]) + header = header.join(['-H "' + key + ':' + value + '" ' for key, value in self.headers.iteritems()]) if self.method.upper() == 'HEAD': - #: --head - fetch headers only - method_opt = '--head ' + #: -I/--head - fetch headers only + method_opt = '-I ' else: - #: --request - specify request method - method_opt = '--request %s ' % self.method.upper() + #: -X/--request - specify request method + method_opt = '-X %s ' % self.method.upper() data = '' if self.method in ('PUT', 'POST', 'PATCH'): @@ -358,9 +359,9 @@ class Request(object): #if data is file: if isinstance(self.data, (list, tuple)): - data = data.join(['--data ' + key + '=' + value + ' ' for key, value in self.data]) + data = data.join(['-d ' + key + '=' + value + ' ' for key, value in self.data]) else: - data = '--data ' + self._enc_data + ' ' + data = '-d ' + self._enc_data + ' ' #: Params handled in _build_url curl_cmd = curl_cmd + method_opt + data + header + '"' + self._build_url() + '"' From c56acf0e321d7a2ae1c0726352d9d7698091d05c Mon Sep 17 00:00:00 2001 From: Mike Waldner Date: Tue, 23 Aug 2011 22:17:36 -0400 Subject: [PATCH 4/6] Small Cleanup --- requests/models.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/requests/models.py b/requests/models.py index 9c51bb8a..ec18b08c 100644 --- a/requests/models.py +++ b/requests/models.py @@ -353,20 +353,17 @@ class Request(object): 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' + #: -d/--data - send specified data in post request. + #: '-d name=John -d last=Doe' generates the + #: post chunk 'name=John&last=Doe' - #if data is file: if isinstance(self.data, (list, tuple)): data = data.join(['-d ' + key + '=' + value + ' ' for key, value in self.data]) else: data = '-d ' + self._enc_data + ' ' #: Params handled in _build_url - curl_cmd = curl_cmd + method_opt + data + header + '"' + self._build_url() + '"' - - return curl_cmd + return curl_cmd + method_opt + data + header + '"' + self._build_url() + '"' class Response(object): From 84d9ff0c5c63ec22dd10670e373b4a46d8770ff4 Mon Sep 17 00:00:00 2001 From: Mike Waldner Date: Tue, 23 Aug 2011 22:34:05 -0400 Subject: [PATCH 5/6] More Small Cleanup --- requests/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/requests/models.py b/requests/models.py index 7a9ba098..3cd8def1 100644 --- a/requests/models.py +++ b/requests/models.py @@ -421,6 +421,7 @@ class Request(object): #: Params handled in _build_url return curl_cmd + method_opt + data + header + '"' + self._build_url() + '"' + class Response(object): """The core :class:`Response ` object. All :class:`Request ` objects contain a From 0bf60c8876cddb5b203c652834ab8c6da55e4ce5 Mon Sep 17 00:00:00 2001 From: Mike Waldner Date: Tue, 23 Aug 2011 23:08:26 -0400 Subject: [PATCH 6/6] Adding check when _enc_data in None --- requests/models.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/requests/models.py b/requests/models.py index 3cd8def1..fc828e13 100644 --- a/requests/models.py +++ b/requests/models.py @@ -391,6 +391,7 @@ class Request(object): #TODO - Auth with User names and accounts #TODO - Files #TODO - OAuth + #TODO - Cookies? #: -L/--location - if there is a redirect, redo request on the new place curl_cmd = 'curl -L ' @@ -410,12 +411,9 @@ class Request(object): data = '' if self.method in ('PUT', 'POST', 'PATCH'): #: -d/--data - send specified data in post request. - #: '-d name=John -d last=Doe' generates the - #: post chunk 'name=John&last=Doe' - if isinstance(self.data, (list, tuple)): data = data.join(['-d ' + key + '=' + value + ' ' for key, value in self.data]) - else: + elif self._enc_data is not None: data = '-d ' + self._enc_data + ' ' #: Params handled in _build_url