def post(self): result, error = {}, None params = params_from_request(self.request) project = params.pop('project') method = params.pop('method') data = params.get('data') if data is not None: try: data = json_decode(data) except Exception as e: logger.error(e) else: params["data"] = data project = self.application.get_project(project) if not project: error = self.application.PROJECT_NOT_FOUND else: result, error = yield self.application.process_call(project, method, params) self.set_header("Content-Type", "application/json") self.finish(json_encode({ "body": result, "error": error }))
def handle_admin_message(self, message): message = json_encode(message) for uid, connection in six.iteritems(self.application.admin_connections): if uid not in self.application.admin_connections: continue connection.send(message) raise Return((True, None))
def post(self): password = self.get_argument("password", None) if password and password == self.opts.get("password"): token = self.create_signed_value("token", "authorized") self.set_header("Content-Type", "application/json") self.finish(json_encode({"token": token})) else: raise tornado.web.HTTPError(400)
def handle_admin_message(self, message): message = json_encode(message) for uid, connection in six.iteritems( self.application.admin_connections): if uid not in self.application.admin_connections: continue connection.send(message) raise Return((True, None))
def set_flash_message(self, message, status='success'): """ Stores a Flash object as a flash cookie under a given key. """ flash = { 'message': message, 'status': status } self.set_secure_cookie(self.FLASH_COOKIE_NAME, json_encode(flash))
def on_message(self, message): """ The only method supported at moment - auth - used to authorize websocket connection. """ try: data = json_decode(message) except ValueError: self.close() return try: method = data["method"] params = data["params"] except (TypeError, KeyError): self.close() return if method == "auth": try: token = params["token"] except (KeyError, TypeError): self.close() return else: user = decode_signed_value( self.application.settings['cookie_secret'], 'token', token ) if user: self.subscribe() self.send(json_encode({ "method": "auth", "body": True })) else: self.send(json_encode({ "method": "auth", "body": False })) self.close() return else: self.close() return
def get(self): projects, error = yield self.application.structure.project_list() if error: raise tornado.web.HTTPError(500, log_message=str(error)) namespaces, error = yield self.application.structure.namespace_list() if error: raise tornado.web.HTTPError(500, log_message=str(error)) data = {"projects": projects, "namespaces": namespaces} self.set_header("Content-Type", "application/json") self.finish(json_encode(data))
def get(self): projects, error = yield self.application.structure.project_list() if error: raise tornado.web.HTTPError(500, log_message=str(error)) namespaces, error = yield self.application.structure.namespace_list() if error: raise tornado.web.HTTPError(500, log_message=str(error)) data = { "projects": projects, "namespaces": namespaces } self.set_header("Content-Type", "application/json") self.finish(json_encode(data))
def get(self): config = self.application.settings.get("config", {}) metrics_interval = config.get("metrics", {}).get("interval", self.application.METRICS_EXPORT_INTERVAL) * 1000 context = { "structure": self.application.structure, "metrics_interval": metrics_interval, "version": centrifuge.__version__, "nodes": self.application.nodes, "engine": getattr(self.application.engine, "NAME", "unknown"), "node_name": self.application.name, } self.set_header("Content-Type", "application/json") self.finish(json_encode(context))
def get(self): config = self.application.settings.get('config', {}) metrics_interval = config.get('metrics', {}).get( 'interval', self.application.METRICS_EXPORT_INTERVAL)*1000 context = { 'structure': self.application.structure, 'metrics_interval': metrics_interval, 'version': centrifuge.__version__, 'nodes': self.application.nodes, 'engine': getattr(self.application.engine, 'NAME', 'unknown'), 'node_name': self.application.name } self.set_header("Content-Type", "application/json") self.finish(json_encode(context))
def add_history_message(self, project_key, channel, message, history_size, history_lifetime): history_list_key = self.get_history_list_key(project_key, channel) try: pipeline = self.worker.pipeline() pipeline.lpush(history_list_key, json_encode(message)) pipeline.ltrim(history_list_key, 0, history_size - 1) if history_lifetime: pipeline.expire(history_list_key, history_lifetime) else: pipeline.persist(history_list_key) yield Task(pipeline.send) except StreamClosedError as e: raise Return((None, e)) else: raise Return((True, None))
def add_history_message(self, project_id, channel, message, history_size=None, history_expire=0): history_size = history_size or self.history_size history_list_key = self.get_history_list_key(project_id, channel) try: pipeline = self.worker.pipeline() pipeline.lpush(history_list_key, json_encode(message)) pipeline.ltrim(history_list_key, 0, history_size - 1) if history_expire: pipeline.expire(history_list_key, history_expire) else: pipeline.persist(history_list_key) yield Task(pipeline.send) except StreamClosedError as e: raise Return((None, e)) else: raise Return((True, None))
def add_presence(self, project_id, channel, uid, user_info, presence_timeout=None): now = int(time.time()) expire_at = now + (presence_timeout or self.presence_timeout) hash_key = self.get_presence_hash_key(project_id, channel) set_key = self.get_presence_set_key(project_id, channel) try: pipeline = self.worker.pipeline() pipeline.multi() pipeline.zadd(set_key, {uid: expire_at}) pipeline.hset(hash_key, uid, json_encode(user_info)) pipeline.execute() yield Task(pipeline.send) except StreamClosedError as e: raise Return((None, e)) else: raise Return((True, None))
def post_actions(self): params = params_from_request(self.request) method = params.pop('method') params.pop('_xsrf') data = params.get('data', None) if data is not None: try: data = json_decode(data) except Exception as e: logger.error(e) else: params["data"] = data result, error = yield self.application.process_call( self.project, method, params) self.set_header("Content-Type", "application/json") self.finish(json_encode({"body": result, "error": error}))
def post_actions(self): params = params_from_request(self.request) method = params.pop('method') params.pop('_xsrf') data = params.get('data', None) if data is not None: try: data = json_decode(data) except Exception as e: logger.error(e) else: params["data"] = data result, error = yield self.application.process_call(self.project, method, params) self.set_header("Content-Type", "application/json") self.finish(json_encode({ "body": result, "error": error }))
def as_message(self): return json_encode(self.as_dict())
def set_flash_message(self, message, status='success'): """ Stores a Flash object as a flash cookie under a given key. """ flash = {'message': message, 'status': status} self.set_secure_cookie(self.FLASH_COOKIE_NAME, json_encode(flash))
def as_message(self): return json_encode(self.as_list_of_dicts())
def publish_admin_message(self, message): result = self._publish(self.admin_channel_name, json_encode(message)) raise Return((result, None))