def dispatch(self, message, sender): socket_message = SocketMessage(message) topics = { AppAPI.Topic.Accessories: self.dispatch_accessories, AppAPI.Topic.Notes: self.dispatch_notes, AppAPI.Topic.Tasks: self.dispatch_tasks, AppAPI.Topic.Users: self.dispatch_users, AppAPI.Topic.AccessoriesLogs: self.dispatch_accessory_log } try: if socket_message.uri is None: error = AppAPI.Error(["`uri` key not sent"]).json_object() return self.execute_read(error, sender, socket_message) topic = socket_message.uri.topic if topic is not None and not topics.has_key(topic): error = AppAPI.Error(["topic `" + topic + "` is not valid" ]).json_object() return self.execute_read(error, sender, socket_message) elif topic is not None: function = topics[topic] function(socket_message, sender, socket_message) else: error = AppAPI.Error(["Nothing to do"]).json_object() self.execute_read(error, sender, socket_message) except Exception as e: error = str(AppAPI.Error(["Unknown error: " + str(e)])) self.execute_read(error, sender)
def delete(self, request_body, as_string=False): response = {} try: validator = NotesDeleteRequestHandlerValidator() validator.validate(request_body) if validator.has_errors(): response = AppAPI.Error(validator.error_messages).json_object() else: object_id = str(request_body[NotesAPIHandler.Constants.IDKey]) if self.note_factory.delete(object_id): response = { NotesAPIHandler.Constants.DeletedKey: object_id } else: response = AppAPI.Error([ "There is not any objetc with id = `" + str(object_id) + "`" ]).json_object() except Exception as e: response = AppAPI.Error([str(e)]).json_object() if as_string: return json.dumps(response) else: return response
def dispatch_accessories(self, socket_message, sender, request_socket_message=None): accessoryapihandler = AccessoryAPIHandler() if socket_message.uri.is_get_action(): response = accessoryapihandler.get_as_objects() self.execute_read(response, sender, request_socket_message) elif socket_message.uri.is_post_action( ) and socket_message.id is not None: state = socket_message.argument( AccessoryAPIHandler.Constants.StateParam) if state == AccessoryAPIHandler.Constants.OnValue: self.accessory_manager.turn_on_accessory(socket_message.id) response = accessoryapihandler.get_as_objects() self.execute_read(response, sender, request_socket_message) elif state == AccessoryAPIHandler.Constants.OffValue: self.accessory_manager.turn_off_accessory(socket_message.id) response = accessoryapihandler.get_as_objects() self.execute_read(response, sender, request_socket_message) else: error = AppAPI.Error(["Nothing to do"]).json_object() self.execute_read(error, sender, socket_message)
def dispatch_notes(self, socket_message, sender, request_socket_message=None): notes_api_handler = NotesAPIHandler() if socket_message.uri.is_get_action(): params = NoteFactoryGetParams() params.from_date = socket_message.argument( NotesAPIHandler.Constants.FromDateParam) params.to_date = socket_message.argument( NotesAPIHandler.Constants.ToDateParam) params.accessory_id = socket_message.argument( NotesAPIHandler.Constants.AccessoryIDParam) response = notes_api_handler.get(params) self.execute_read(response, sender, request_socket_message) elif socket_message.uri.is_delete_action( ) and socket_message.id is not None: response = notes_api_handler.delete( {NotesAPIHandler.Constants.IDKey: socket_message.id}) self.execute_read(response, sender, request_socket_message) elif socket_message.uri.is_post_action( ) and socket_message.object is not None: response = notes_api_handler.create(socket_message.object) self.execute_read(response, sender, request_socket_message) else: error = AppAPI.Error(["Nothing to do"]).json_object() self.execute_read(error, sender, socket_message)
def create(self, request_body, as_string=False): response = {} try: post_handler_validator = UserPostRequestHandlerValidator() post_handler_validator.validate(request_body) if post_handler_validator.has_errors(): response = AppAPI.Error( post_handler_validator.error_messages).json_object() else: user = User(request_body) user.id = str(self.user_factory.insert(user)) response = {UsersAPIHandler.Constants.UserKey: user.to_json()} except Exception, e: response = AppAPI.Error([str(e)]).json_object()
def validate_user(self): try: user_id = self.request.headers["Userid"] if self.request.headers.has_key("Userid") else None self.user_factory.validate_user_with_id(user_id) except Exception as e: response = str(AppAPI.Error([str(e)])) self.write(response) self.finish()
def create(self,request_body, as_string = False): response = {} try: task_handler_validator = TasksPostRequestHandlerValidator() task_handler_validator.validate(request_body) if task_handler_validator.has_errors(): response = AppAPI.Error(task_handler_validator.error_messages).json_object() else: timer_task = TimerTask(request_body) timer_task.creation_date = time.time() timer_task.id = str(self.tasks_factory.insert(timer_task)) response = timer_task.to_json() except Exception, e: response = AppAPI.Error([str(e)]).json_object()
def delete(self): response = "" try: json_object = json.loads(str(self.request_body)) response = self.users_api_handler.delete(json_object) except: response = str(AppAPI.Error([str(e)])) self.write(str(response)) self.finish()
def get(self, params = TaskFactoryGetParams(), as_string = False): response = {} try: response = self.tasks_factory.get_tasks_for_api(params) except Exception as e: response = AppAPI.Error([str(e)]).json_object() if as_string: return json.dumps(response) else: return response
def post(self): response = "" try: json_object = json.loads(str(self.request.body)) response = self.users_api_handler.create(json_object) except Exception as e: response = str(AppAPI.Error([str(e)])) self.write(response) self.finish()
def get_as_objects(self, get_params=AccessoryLogFactoryGetParams()): if get_params.limit is not None: get_params.limit = DefaultMaxLimit if get_params.limit == 0 else min( get_params.limit, DefaultMaxLimit) else: get_params.limit = DefaultMaxLimit try: logs = self.log_factory.get_logs_for_api(get_params) return logs except Exception as e: return AppAPI.Error([str(e)]).json_object()
def get(self, *args): self.validate_user() response = "" try: params = TaskFactoryGetParams() params.accessory_id = self.get_query_argument(TasksAPIHandler.Constants.AccessoryIDParam, None) response = self.tasks_api_handler.get(params) except Exception as e: response = str(AppAPI.Error([str(e)])) self.write(response) self.finish()
def delete(self, request_body, as_string=False): response = {} try: validator = UserDeleteRequestHandlerValidator() validator.validate(request_body) if validator.has_errors(): response = AppAPI.Error(validator.error_messages).json_object() else: object_id = str(request_body[UsersAPIHandler.Constants.IDKey]) if self.user_factory.delete(object_id): response = { UsersAPIHandler.Constants.DeletedKey: object_id } else: response = AppAPI.Error([ "There is not any object with id = `" + str(object_id) + "`" ]).json_object() except Exception, e: response = AppAPI.Error([str(e)]).json_object()
def create(self, json_object={}, as_string=False): response = {} try: notes_handler_validator = NotesPostRequestHandlerValidator() notes_handler_validator.validate(json_object) if notes_handler_validator.has_errors(): response = AppAPI.Error( notes_handler_validator.error_messages).json_object() else: note = Note(json_object) note.creation_date = time.time() note.id = str(self.note_factory.insert(note)) response = self.note_factory.get_note_for_api(note.id) except Exception as e: response = AppAPI.Error([str(e)]).json_object() if as_string: return json.dumps(response) else: return response
def post(self): self.validate_user() response = "" try: json_object = json.loads(str(self.request.body)) json_object[TasksAPIHandler.Constants.UserIDKey] = self.authenticated_user_id() response = self.tasks_api_handler.create(json_object) self.clients_updater.update_all_clients() except Exception as e: response = str(AppAPI.Error([str(e)])) self.write(response) self.finish()
def delete(self): self.validate_user() response = "" try: json_object = json.loads(str(self.request.body)) response = self.tasks_api_handler.delete(json_object) self.clients_updater.update_all_clients() except Exception as e: response = str(AppAPI.Error([str(e)])) self.write(response) self.finish()
def dispatch_accessory_log(self, socket_message, sender, request_socket_message=None): accessory_log_api_handler = AccessoryLogAPIHandler() if socket_message.uri.is_get_action(): params = AccessoryLogFactoryGetParams() params.from_date = socket_message.argument( AccessoryAPIHandler.Constants.FromDateParam) params.to_date = socket_message.argument( AccessoryAPIHandler.Constants.ToDateParam) params.accessory_id = socket_message.argument( AccessoryAPIHandler.Constants.AccessoryIDParam) params.limit = int( socket_message.argument( AccessoryAPIHandler.Constants.LimitParam, 0)) response = accessory_log_api_handler.get_as_objects(params) self.execute_read(response, sender, request_socket_message) else: error = AppAPI.Error(["Nothing to do"]).json_object() self.execute_read(error, sender, socket_message)
def dispatch_users(self, socket_message, sender, request_socket_message=None): users_api_handler = UsersAPIHandler() if socket_message.uri.is_get_action(): self.execute_read(users_api_handler.get(), sender, request_socket_message) elif socket_message.uri.is_delete_action( ) and socket_message.id is not None: response = users_api_handler.delete( {UsersAPIHandler.Constants.IDKey: socket_message.id}) self.execute_read(response, sender, request_socket_message) elif socket_message.uri.is_post_action( ) and socket_message.object is not None: response = users_api_handler.create(socket_message.object) self.execute_read(response, sender, request_socket_message) else: error = AppAPI.Error(["Nothing to do"]).json_object() self.execute_read(error, sender, socket_message)