You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

43 lines
1.0 KiB

  1. import grpc
  2. import schema_pb2 as service
  3. import schema_pb2_grpc as stub
  4. def put_user(user_id, user_name):
  5. args = service.User(user_id=user_id, user_name=user_name)
  6. response = stub.PutUser(args)
  7. print(f"PutUser({user_id}, '{user_name}') = {response.status}")
  8. def get_users():
  9. args = service.EmptyMessage()
  10. response = stub.GetUsers(args)
  11. result = {}
  12. for user in response.users:
  13. result[user.user_id] = user.user_name
  14. print(f"GetUsers() = {result}")
  15. def delete_user(user_id):
  16. args = service.User(user_id=user_id)
  17. response = stub.DeleteUser(args)
  18. print(f"DeleteUser({user_id}) = {response.status}")
  19. if __name__ == '__main__':
  20. with grpc.insecure_channel('localhost:1234') as channel:
  21. stub = stub.DatabaseStub(channel)
  22. # Create four users
  23. [put_user(i, f"User{i}") for i in range(1, 5)]
  24. # Update the usename of the second user
  25. put_user(2, "User2_updated")
  26. # Delete the thrid user
  27. delete_user(3)
  28. # Retrieve all users
  29. get_users()