def __convertObjectToContentType(self,request,obj): """ Takes care of converting an object (non-String) response to the appropriate format, based on the what the caller can accept. Returns a tuple of (content,contentType) """ if HttpHeader.ACCEPT in request.received_headers: accept = request.received_headers[HttpHeader.ACCEPT] if MediaType.APPLICATION_JSON in accept: if not advanced_json: obj = convertForSerialization(obj) return (convertToJson(obj),MediaType.APPLICATION_JSON) elif MediaType.TEXT_YAML in accept: obj = convertForSerialization(obj) return (yaml.dump(obj),MediaType.TEXT_YAML) elif MediaType.APPLICATION_XML in accept or MediaType.TEXT_XML in accept: obj = convertForSerialization(obj) return (generateXml(obj),MediaType.APPLICATION_XML) else: # no idea, let's do JSON if not advanced_json: obj = convertForSerialization(obj) return (convertToJson(obj),MediaType.APPLICATION_JSON) else: if not advanced_json: obj = convertForSerialization(obj) # called has no accept header, let's default to JSON return (convertToJson(obj),MediaType.APPLICATION_JSON)
def __convertObjectToContentType(self, request, obj): """ Takes care of converting an object (non-String) response to the appropriate format, based on the what the caller can accept. Returns a tuple of (content,contentType) """ obj = convertForSerialization(obj) if HttpHeader.ACCEPT in request.received_headers: accept = request.received_headers[HttpHeader.ACCEPT] if MediaType.APPLICATION_JSON in accept: return (convertToJson(obj), MediaType.APPLICATION_JSON) elif MediaType.TEXT_YAML in accept: return (yaml.dump(obj), MediaType.TEXT_YAML) elif MediaType.APPLICATION_XML in accept or MediaType.TEXT_XML in accept: return (generateXml(obj), MediaType.APPLICATION_XML) else: # no idea, let's do JSON return (convertToJson(obj), MediaType.APPLICATION_JSON) else: # called has no accept header, let's default to JSON return (convertToJson(obj), MediaType.APPLICATION_JSON)
def __convertToXml(self): self.headers[HttpHeader.CONTENT_TYPE]=MediaType.APPLICATION_XML self.response=Response(self.code, generateXml(self.serialized), self.headers)