示例#1
0
    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()
示例#2
0
def watch_book(stub):
    response = stub.Watch(pb.Empty())
    try:
        for book in response:
            print(book)
    except grpc._channel._Rendezvous as err:
        print(err)
示例#3
0
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)
示例#4
0
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()))
示例#5
0
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()))
示例#6
0
 def Insert(self, request, context):
     bookList.append(request)
     return books_pb2.Empty()
示例#7
0
 def Insert(self, request, context):
     bookList.append(request)
     bookStream.emit("NewBook", request)
     return books_pb2.Empty()