Exemplo n.º 1
0
	def respond(self, data, content_type='application/json', cache_life=0, headers={}):
		self.security_headers()
		self.cors_headers()
		self.cache_header(cache_life)

		for header, value in headers.items():
			if header == 'Content-Type':
				content_type = value
			else:
				self.response.headers[header] = value

		if content_type == 'application/json':
			if isinstance(data, BaseModel):
				data = data.to_dict()
			elif isinstance(data, (list, tuple)) and len(data) > 0 and isinstance(data[0], BaseModel):
				data = [e.to_dict() for e in data]
			data = json_stringify(data, separators=(',',':'))

		# Let me begin with an apology. The Access-Control-Expose-Headers header is
		# not supported on most devices and prevents me from sending the session
		# token in a header. Instead of poluting the body of the response I have
		# decided to polute the Content-Type header with an extra parameter that
		# the client can read (because it is a "simple" header). I apologise for the
		# hackery that is below.
		if self.kik_session:
			content_type += '; kik-session=%s' % self.kik_session

		self.response.headers['Content-Type'] = content_type
		self.response.out.write(data)
		mixpanel.smart_flush()
Exemplo n.º 2
0
	def handle_exception(self, exception, debug):
		logging.exception(exception)
		if isinstance(exception, BadValueError) or isinstance(exception, ValidationError):
			self.response.set_status(400)
		else:
			self.response.set_status(500)
		self.response.write('An error occurred.')
		mixpanel.smart_flush()
Exemplo n.º 3
0
 def handle_exception(self, exception, debug):
     logging.exception(exception)
     if isinstance(exception, BadValueError) or isinstance(
             exception, ValidationError):
         self.response.set_status(400)
     else:
         self.response.set_status(500)
     self.response.write('An error occurred.')
     mixpanel.smart_flush()
Exemplo n.º 4
0
	def respond_error(self, code, message='', cache_life=0, headers={}):
		self.response.set_status(code)
		self.security_headers()
		self.cors_headers()
		self.cache_header(cache_life)
		for header, value in headers.items():
			self.response.headers[header] = value
		if 'Content-Type' not in headers:
			self.response.headers['Content-Type'] = 'text/plain'
		self.response.out.write(message)
		mixpanel.smart_flush()
Exemplo n.º 5
0
 def respond_error(self, code, message='', cache_life=0, headers={}):
     self.response.set_status(code)
     self.security_headers()
     self.cors_headers()
     self.cache_header(cache_life)
     for header, value in headers.items():
         self.response.headers[header] = value
     if 'Content-Type' not in headers:
         self.response.headers['Content-Type'] = 'text/plain'
     self.response.out.write(message)
     mixpanel.smart_flush()
Exemplo n.º 6
0
    def respond(self,
                data,
                content_type='application/json',
                cache_life=0,
                headers={}):
        self.security_headers()
        self.cors_headers()
        self.cache_header(cache_life)

        for header, value in headers.items():
            if header == 'Content-Type':
                content_type = value
            else:
                self.response.headers[header] = value

        if content_type == 'application/json':
            if isinstance(data, BaseModel):
                data = data.to_dict()
            elif isinstance(data,
                            (list, tuple)) and len(data) > 0 and isinstance(
                                data[0], BaseModel):
                data = [e.to_dict() for e in data]
            data = json_stringify(data, separators=(',', ':'))

        # Let me begin with an apology. The Access-Control-Expose-Headers header is
        # not supported on most devices and prevents me from sending the session
        # token in a header. Instead of poluting the body of the response I have
        # decided to polute the Content-Type header with an extra parameter that
        # the client can read (because it is a "simple" header). I apologise for the
        # hackery that is below.
        if self.kik_session:
            content_type += '; kik-session=%s' % self.kik_session

        self.response.headers['Content-Type'] = content_type
        self.response.out.write(data)
        mixpanel.smart_flush()