def Delete(self, request, context): for booked in bookList: if booked.id == request.id: bookList.remove(booked) return books_pb2.Empty() context.set_code(grpc.StatusCode.NOT_FOUND) context.set_details("No book with id {} was found".format(request.id)) return books_pb2.Empty()
def watch_book(stub): response = stub.Watch(pb.Empty()) try: for book in response: print(book) except grpc._channel._Rendezvous as err: print(err)
def doWatch(): stub = GetClient() print("Server stream data received:") stream = stub.Watch(pb.Empty()) try: for book in stream: printRespAsJson(book) except grpc._channel._Rendezvous as err: print(err)
def list_books(stub): # Exception handling. try: # List books books = stub.List(pb.Empty()) print(books) # Catch any raised errors by grpc. except grpc.RpcError as e: print('ListBooks failed with {0}: {1}'.format(e.code(), e.details()))
def doList(): stub = GetClient() # Exception handling. try: # List books books = stub.List(pb.Empty()) print("Server sent %s book(s).\n" % len(books.books)) printRespAsJson(books) # Catch any raised errors by grpc. except grpc.RpcError as e: print('ListBooks failed with {0}: {1}'.format(e.code(), e.details()))
def Insert(self, request, context): bookList.append(request) return books_pb2.Empty()
def Insert(self, request, context): bookList.append(request) bookStream.emit("NewBook", request) return books_pb2.Empty()