74 lines
2.9 KiB
Python
74 lines
2.9 KiB
Python
from concurrent import futures
|
|
import logging
|
|
import grpc
|
|
from gen.python.pet.v1 import pet_pb2
|
|
|
|
class PetStoreServiceStub: # define the channel
|
|
def __init__(self, channel):
|
|
self.PutPet = channel.unary_unary(
|
|
'/pet.v1.PetStoreService/PutPet',
|
|
request_serializer=pet_pb2.PutPetRequest.SerializeToString,
|
|
response_deserializer=pet_pb2.PutPetResponse.FromString,
|
|
)
|
|
self.GetPet = channel.unary_unary(
|
|
'/pet.v1.PetStoreService/GetPet',
|
|
request_serializer=pet_pb2.GetPetRequest.SerializeToString,
|
|
response_deserializer=pet_pb2.GetPetResponse.FromString,
|
|
)
|
|
self.DeletePet = channel.unary_unary(
|
|
'/pet.v1.PetStoreService/DeletePet',
|
|
request_serializer=pet_pb2.DeletePetRequest.SerializeToString,
|
|
response_deserializer=pet_pb2.DeletePetResponse.FromString,
|
|
)
|
|
|
|
def run():
|
|
with grpc.insecure_channel("localhost:50000") as channel:
|
|
stub = PetStoreServiceStub(channel)
|
|
|
|
flag = "Y"
|
|
while flag == "Y":
|
|
symb = str(input("Please input the expected operation: put, get or delete.\n"))
|
|
|
|
if symb == "put":
|
|
type = str(input("Please input type of the pet:\n"))
|
|
# judge the type of the pet
|
|
if type == "cat":
|
|
pettype = 1
|
|
elif type == "dog":
|
|
pettype = 2
|
|
elif type == "snake":
|
|
pettype = 3
|
|
elif type == "hamster":
|
|
pettype = 4
|
|
else:
|
|
pettype = 0
|
|
name = str(input("Please input name of the pet:\n"))
|
|
response = stub.PutPet(pet_pb2.PutPetRequest(pet_type=pettype,name=name))
|
|
print("Successfully put.\n")
|
|
print(response.pet)
|
|
|
|
elif symb == "get":
|
|
id = str(input("Please input id of the pet:\n"))
|
|
response = stub.GetPet(pet_pb2.GetPetRequest(pet_id=id))
|
|
if response.pet == pet_pb2.Pet():
|
|
print("There is no such id.\n")
|
|
else:
|
|
print(response.pet)
|
|
|
|
elif symb == "delete":
|
|
id = str(input("Please input id of the pet:\n"))
|
|
response = stub.GetPet(pet_pb2.GetPetRequest(pet_id=id))
|
|
if response.pet == pet_pb2.Pet():
|
|
print("There is no such id.\n")
|
|
else:
|
|
stub.DeletePet(pet_pb2.DeletePetRequest(pet_id=id))
|
|
print("Successfully delete.\n")
|
|
|
|
else:
|
|
print("Invalid input!\n")
|
|
|
|
flag = str(input("Do you want to continue: Y(mean yes) or other keys(mean no)?\n"))
|
|
|
|
if __name__ == '__main__':
|
|
logging.basicConfig()
|
|
run() |