-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
35 lines (32 loc) · 1.1 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import grpc
import post_pb2
import post_pb2_grpc
channel = grpc.insecure_channel("localhost:50051")
stub = post_pb2_grpc.PostsServiceStub(channel)
while True:
command = input("Enter command (create/read/list): ")
if command == "create":
new_post = post_pb2.Post(
id=None,
title=input("Enter title: "),
body=input("Enter body: "),
author_id=input("Enter author ID: "),
likes=None,
tags=[
tag.strip()
for tag in input("Enter tags (comma-separated): ").split(",")
],
)
response = stub.CreatePost(new_post)
print(f"Created post with ID: {response.id}")
elif command == "read":
post_id = int(input("Enter post ID: "))
response = stub.GetPostById(post_pb2.GetPostByIdRequest(id=post_id))
print(response)
elif command == "list":
response = stub.GetPosts(post_pb2.GetPostsRequest())
print(f"Found {len(response.posts)} posts:")
for post in response.posts:
print(post)
else:
print("Invalid command")