Expose api response properties and cache buffer decoding

Change-Id: I1e47be192eade575d2954d57991c0dd6d25e47d1
This commit is contained in:
Joshua Harlow 2015-06-25 17:09:10 -07:00 committed by Scott Moser
parent d072623527
commit 1479f92ab6
1 changed files with 14 additions and 3 deletions

View File

@ -13,15 +13,26 @@ class APIResponse(object):
To access the content in the binary format, use the
`buffer` attribute, while the unicode content can be
accessed by calling `str` over this.
accessed by calling `str` over this (or by accessing
the `decoded_buffer` property).
"""
def __init__(self, buffer, encoding="utf-8"):
self.buffer = buffer
self._encoding = encoding
self.encoding = encoding
self._decoded_buffer = None
@property
def decoded_buffer(self):
# Avoid computing this again and again (although multiple threads
# may decode it if they all get in here at the same time, but meh
# thats ok).
if self._decoded_buffer is None:
self._decoded_buffer = self.buffer.decode(self.encoding)
return self._decoded_buffer
def __str__(self):
return self.buffer.decode(self._encoding)
return self.decoded_buffer
@six.add_metaclass(abc.ABCMeta)