Commits

ddbennett  committed feaae1a

Added PistonResponse object

  • Participants
  • Parent commits 50ef9cc

Comments (0)

Files changed (2)

File piston/resource.py

 from handler import typemapper
 from doc import HandlerMethod
 from authentication import NoAuthentication
-from utils import coerce_put_post, FormValidationError, HttpStatusCode
-from utils import rc, format_error, translate_mime, MimerDataException
+from utils import (
+    coerce_put_post, FormValidationError, HttpStatusCode, rc, format_error,
+    translate_mime, MimerDataException, PistonResponse)
 from validate_jsonp import is_valid_jsonp_callback_value
 
 CHALLENGE = object()
 
         status_code = 200
 
-        # If we're looking at a response object which contains non-string
-        # content, then assume we should use the emitter to format that
-        # content
-        if isinstance(result, HttpResponse) and result._base_content_is_iter:
-            status_code = result.status_code
-            # Note: We can't use result.content here because that method attempts
-            # to convert the content into a string which we don't want.
-            # when _base_content_is_iter is True _container is the raw data
-            result = result._container
+        if isinstance(result, PistonResponse):
+            status_code = result.status
+            headers = result.items()
+            result = result.content
 
         srl = emitter(result, typemapper, handler, fields, anonymous)
 

File piston/utils.py

     return wrap
 
 require_extended = require_mime('json', 'yaml', 'xml', 'pickle')
+
+
+class PistonResponse:
+    """Container class for a single object.
+
+    Intended to be used for passing enhanced attributes into Piston without
+    accessing private members.
+
+    """
+    def __init__(self, content, status=200):
+        self.content = content
+        self.headers = {}
+        self.status = status
+
+    def __iter__(self):
+        yield self.content
+
+    def __getitem__(self, item):
+        return self.headers[item]
+
+    def __setitem__(self, item, value):
+        self.headers[item] = value
+
+    def keys(self):
+        self.headers.keys()
+
+    def items(self):
+        self.headers.items()