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 __init__(self, request, code=200, entity=None, headers={}, ctype=None): self.code = code self.entity=entity if entity != None else "" self.headers=headers self.request=request self.ctype=ctype self.serialized = convertForSerialization(self.entity) if ctype=='json': self.__convertToJson() elif ctype=='jsonp': self.__convertToJsonp() elif ctype=='xml': self.__convertToXml() elif ctype=='yaml': self.__convertToYaml() else: self.__automagickConversion()
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 sendJSON(self, event='notify', data=False): ser=convertForSerialization({'data': data, 'ts': time.time()}) self.write(event=event, data=[convertToJson(ser)])