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 sendJSON(self, event='notify', data=False): ser=convertForSerialization({'data': data, 'ts': time.time()}) self.write(event=event, data=[convertToJson(ser)])
def __convertToJsonp(self): self.headers[HttpHeader.CONTENT_TYPE]=MediaType.APPLICATION_JSON callback="" if 'callback' in self.request.args.keys(): callback=self.request.args['callback'][0] self.response=Response(self.code, callback+"("+convertToJson(self.serialized)+")", self.headers)
def __convertToJson(self): self.headers[HttpHeader.CONTENT_TYPE]=MediaType.APPLICATION_JSON self.response=Response(self.code, convertToJson(self.serialized), self.headers)