Skip to content

Commit

Permalink
request and response formats can be specified in the format argument …
Browse files Browse the repository at this point in the history
…of the Client.request method, by using a tuple
  • Loading branch information
guglielmo committed Mar 12, 2015
1 parent bafd7f1 commit 7d90195
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions tortilla/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _log(self, message, debug=None, **kwargs):

def request(self, method, url, path=(), extension=None, params=None,
headers=None, data=None, debug=None, cache_lifetime=None,
silent=False, ignore_cache=False, req_format='json', resp_format='json', **kwargs):
silent=False, ignore_cache=False, format='json', **kwargs):
"""Requests a URL and returns a *Bunched* response.
This method basically wraps the request method of the requests
Expand All @@ -119,11 +119,13 @@ def request(self, method, url, path=(), extension=None, params=None,
from HTTP status codes or parsing will be ignored.
:param ignore_cache: (optional) When ``True``, a previously
cached response of the same request will be ignored.
:param req_format: (optional) The type of response data to parse.
May take the values: None, 'json'(default), 'yml' (see formats)
:param resp_format: (optional) The type
When executing a POST or PUT request, the ``data`` argument
will be converted to the format type.
:param format: (optional) The type of request data to parse.
May take the following values:
- 'json', 'xml', ... both request data load and response are
converted to the specified format
- (None, 'json') a tuple, with the request data format in pos 0
and the response format in pos 1
defaults to 'json'
:param kwargs: (optional) Arguments that will be passed to
the `requests.request` method
:return: :class:`Bunch` object from JSON-parsed response
Expand All @@ -136,6 +138,13 @@ def request(self, method, url, path=(), extension=None, params=None,
if headers is not None:
request_headers.update(headers)

# extract req_format and resp_format from format arguments
if type(format) in (list, tuple) and len(format) == 2:
req_format = format[0]
resp_format = format[1]
else:
req_format = resp_format = format

# add Content-Type header & compose data, only when
# 1. content is actually sent (whatever the HTTP verb is used)
# 2. format is provided ('json' by default)
Expand Down

0 comments on commit 7d90195

Please sign in to comment.