Skip to content

Commit

Permalink
Merge pull request #24 from openpolis/dev-reqresp-format
Browse files Browse the repository at this point in the history
response and request formats can be specified individually
  • Loading branch information
redodo committed Mar 13, 2015
2 parents 845fb86 + 7d90195 commit dd5757f
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions tortilla/wrappers.py
Expand Up @@ -119,9 +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 format: (optional) The type of response data to parse.
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 @@ -134,13 +138,20 @@ 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)
if format and (data is not None):
if req_format and (data is not None):
request_headers.setdefault(
'Content-Type', formats.meta(format).get('content_type'))
data = formats.compose(format, data)
'Content-Type', formats.meta(req_format).get('content_type'))
data = formats.compose(req_format, data)

# form the URL
if not isinstance(path, string_type):
Expand Down Expand Up @@ -184,15 +195,15 @@ def request(self, method, url, path=(), extension=None, params=None,
if not has_body:
parsed_response = 'No response'
else:
parsed_response = formats.parse(format, r.text)
parsed_response = formats.parse(resp_format, r.text)
except ValueError as e:
# we've failed, raise this stuff when not silent
if len(r.text) > DEBUG_MAX_TEXT_LENGTH:
text = r.text[:DEBUG_MAX_TEXT_LENGTH] + '...'
else:
text = r.text
self._log(debug_messages['incorrect_format_response'], debug,
format=format, status_code=r.status_code,
format=resp_format, status_code=r.status_code,
reason=r.reason, text=text)
if silent:
return None
Expand Down

0 comments on commit dd5757f

Please sign in to comment.