def add(self, request: Union[Insert, Update, Delete]): """ Adds a request to the batch request object :: batch_object.add(db.delete('books').where(COND('name', '!=', 'Book_name'))) :param request: (Insert or Update or Delete) A request to add to the batch request """ if self.db_type != request.db_type: raise Exception("Cannot Batch Requests of Different Database Types") all_request = server_pb2.AllRequest() if isinstance(request, Insert): all_request.col = request.collection all_request.document = obj_to_utf8_bytes(request.document) all_request.operation = request.operation all_request.type = "create" if isinstance(request, Update): all_request.col = request.collection all_request.operation = request.operation all_request.find = obj_to_utf8_bytes(request.params['find']) all_request.update = obj_to_utf8_bytes(request.params['update']) all_request.type = "update" if isinstance(request, Delete): all_request.col = request.collection all_request.operation = request.operation all_request.find = obj_to_utf8_bytes(request.params['find']) all_request.type = "delete" self.requests.append(all_request) return self
def subscribe(self, on_snapshot: Callable, on_error: Callable) -> LiveQuerySubscription: """ Subscribes to the particular LiveQuery instance :param on_snapshot: (Callable) The function to be called when new live data is encountered (takes in docs(List), type of change(String) and the changed doc(dict)) :param on_error: (Callable) The function to be called when an error occurs (takes in an error(str)) :return: (LiveQuerySubscription) The LiveQuerySubscription instance """ self.on_snapshot = on_snapshot self.on_error = on_error self.async_result = self.run_pool.apply_async(self._run_client, (self.id, )) options = obj_to_utf8_bytes({"skipInitial": self.skip_initial}) self.pool.map( self._send, (server_pb2.RealTimeRequest(token=self.token, dbType=self.db_type, project=self.project_id, group=self.collection, options=options, type=constants.TypeRealtimeSubscribe, id=self.id, where=obj_to_utf8_bytes(self.find)), )) self.subscription = LiveQuerySubscription(self.unsubscribe, []) return self.subscription
def update(self, find, operation: str, _update, db_type: str, col: str) -> Response: """ Calls the gRPC Update function :param find: The find parameters :param operation: (str) The operation to perform :param _update: The update parameters :param db_type: (str) The database type :param col: (str) The (optional) collection name :return: (Response) The response object containing values corresponding to the request """ find = obj_to_utf8_bytes(find) _update = obj_to_utf8_bytes(_update) meta = self._make_meta(db_type, col) update_request = server_pb2.UpdateRequest(find=find, operation=operation, update=_update, meta=meta) return Response(self.stub.Update(update_request))
def unsubscribe(self): """ Unsubscribes from the particular LiveQuery instance """ options = obj_to_utf8_bytes({"skipInitial": self.skip_initial}) self.pool.map( self._send, (server_pb2.RealTimeRequest(token=self.token, dbType=self.db_type, project=self.project_id, group=self.collection, options=options, type=constants.TypeRealtimeUnsubscribe, id=self.id, where=obj_to_utf8_bytes(self.find)), )) self.client.close() self.run_pool.close() try: del self.store except KeyError: pass
def callback(kind: str, result): if kind == "response": self.pool.map( self._func, (server_pb2.FunctionsPayload( id=response.id, type=constants.TypeServiceRequest, service=self.service, params=obj_to_utf8_bytes(result)), )) else: raise ValueError( "The kind (1st parameter) should be 'response'" )
def pubsub_publish(self, subject: str, msg) -> Response: """ Calls the gRPC PubsubPublish function :param subject: (str) The subject to publish to :param msg: The message to be published :return: (Response) The response object containing values corresponding to the request """ msg = obj_to_utf8_bytes(msg) meta = self._make_meta() publish_request = server_pb2.PubsubPublishRequest(subject=subject, msg=msg, meta=meta) return Response(self.stub.PubsubPublish(publish_request))
def aggregate(self, pipeline, operation: str, db_type: str, col: str) -> Response: """ Calls the gRPC Aggregate function :param pipeline: The pipeline parameters :param operation: (str) The operation to perform :param db_type: (str) The database type :param col: (str) The (optional) collection name :return: (Response) The response object containing values corresponding to the request """ pipeline = obj_to_utf8_bytes(pipeline) meta = self._make_meta(db_type, col) aggregate_request = server_pb2.AggregateRequest(pipeline=pipeline, operation=operation, meta=meta) return Response(self.stub.Aggregate(aggregate_request))
def create(self, document, operation: str, db_type: str, col: str) -> Response: """ Calls the gRPC Create function :param document: The document to create :param operation: (str) The operation to perform :param db_type: (str) The database type :param col: (str) The (optional) collection name :return: (Response) The response object containing values corresponding to the request """ document = obj_to_utf8_bytes(document) meta = self._make_meta(db_type, col) create_request = server_pb2.CreateRequest(document=document, operation=operation, meta=meta) return Response(self.stub.Create(create_request))
def read(self, find, operation: str, options: server_pb2.ReadOptions, db_type: str, col: str) -> Response: """ Calls the gRPC Read function :param find: The find parameters :param operation: (str) The operation to perform :param options: (server_pb2.ReadOptions) :param db_type: (str) The database type :param col: (str) The (optional) collection name :return: (Response) The response object containing values corresponding to the request """ find = obj_to_utf8_bytes(find) meta = self._make_meta(db_type, col) read_request = server_pb2.ReadRequest(find=find, operation=operation, options=options, meta=meta) return Response(self.stub.Read(read_request))
def faas(self, service: str, function: str, params, timeout: int) -> Response: """ Calls the gRPC Call function :param service: (str) The name of service(engine) with which the function is registered :param function: (str) The name of function to be called :param params: The params for the function :param timeout: (int) The timeout in milliseconds :return: (Response) The response object containing values corresponding to the request """ params = obj_to_utf8_bytes(params) functions_request = server_pb2.FunctionsRequest( params=params, timeout=timeout // 1000, service=service, function=function, token=self.token, project=self.project_id) return Response(self.stub.Call(functions_request))