-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_test.py
70 lines (64 loc) · 2.01 KB
/
python_test.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import print_function
import sys
sys.path.append("grpc/python")
from google.protobuf.json_format import MessageToJson
from electionpb.election_pb2 import ListOpenElectionsRequest
from electionpb.election_pb2 import CommenceElectionRequest
from electionpb import election_pb2_grpc
import logging
import grpc
def run():
"""
This function demonstrates how to use the ElectionStub.
Examples:
>>> run() # Assuming gRPC server is running locally on port 8081
client received: {
"openElections": [
{
"electionId": "E1",
"organizerUserId": "U1",
"name": "Election Name 1",
"description": "Election Description 1",
"commencedAt": "1699900000"
},
{
"electionId": "E2",
"organizerUserId": "U1",
"name": "Election Name 2",
"description": "Election Description 2",
"commencedAt": "1699900001"
}
]
}
Note: The actual output might vary depending on the gRPC server's response.
"""
with grpc.insecure_channel("localhost:8081") as channel:
stub = election_pb2_grpc.ElectionStub(channel)
stub.CommenceElection(
CommenceElectionRequest(
election_id="E1",
organizer_user_id="U1",
name="Election Name 1",
description="Election Description 1"
)
)
stub.CommenceElection(
CommenceElectionRequest(
election_id="E2",
organizer_user_id="U1",
name="Election Name 2",
description="Election Description 2"
)
)
response = stub.ListOpenElections(
ListOpenElectionsRequest(
page=1,
items_per_page=10,
sort_by="Name",
sort_direction="ascending"
)
)
print("client received: " + MessageToJson(response))
if __name__ == "__main__":
logging.basicConfig()
run()