Skip to content

Commit 7affa84

Browse files
committed
chore: Add basic samples
1 parent 043602a commit 7affa84

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ clone-proto:
55
gen:
66
cd plugin-pb && git pull && cd ..
77
cp ./plugin-pb/plugin/v3/*.proto ./protos/cloudquery/plugin_v3/.
8-
python -m grpc_tools.protoc -I./protos --python_out=. --pyi_out=. --grpc_python_out=. ./protos/cloudquery/plugin_v3/*.proto
8+
python -m grpc_tools.protoc -I./protos --python_out=. --pyi_out=. --grpc_python_out=. ./protos/cloudquery/plugin_v3/*.proto
9+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This is a low-level auto-generate gRPC client and server for CloudQuery plugin f
1111
we recommend using virtualenv to manage your python environment.
1212

1313
```bash
14-
virtualenv -p python3.7 venv
14+
virtualenv -p python3.7 venv # or any python >= 3.7
1515
source venv/bin/activate
1616
```
1717

samples/plugin_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import logging
2+
import grpc
3+
from cloudquery.plugin_v3 import plugin_pb2, plugin_pb2_grpc
4+
5+
def run():
6+
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
7+
# used in circumstances in which the with statement does not fit the needs
8+
# of the code.
9+
with grpc.insecure_channel('localhost:50051') as channel:
10+
stub = plugin_pb2_grpc.PluginStub(channel)
11+
response = stub.GetName(plugin_pb2.GetName.Request())
12+
print(response.name)
13+
14+
if __name__ == '__main__':
15+
logging.basicConfig()
16+
run()

samples/plugin_server.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import grpc
2+
from concurrent import futures
3+
import logging
4+
from cloudquery.plugin_v3 import plugin_pb2, plugin_pb2_grpc
5+
6+
class PluginServicer(plugin_pb2_grpc.PluginServicer):
7+
def __init__(self):
8+
pass
9+
10+
def GetName(self, request, context):
11+
return plugin_pb2.GetName.Response(name="plugin test")
12+
13+
def GetVersion(self, request, context):
14+
return plugin_pb2.GetVersion.Response(version="0.0.1")
15+
16+
def Init(self, request, context):
17+
return plugin_pb2.Init.Response()
18+
19+
def GetTables(self, request, context):
20+
return plugin_pb2.GetTables.Response(tables=[])
21+
22+
def Sync(self, request, context):
23+
return plugin_pb2.Sync.Response()
24+
25+
def Read(self, request, context):
26+
return plugin_pb2.Read.Response()
27+
28+
def Write(self, request_iterator, context):
29+
for request in request_iterator:
30+
print(request)
31+
return plugin_pb2.Write.Response()
32+
33+
def Close(self, request, context):
34+
return plugin_pb2.Close.Response()
35+
36+
def serve():
37+
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
38+
plugin_pb2_grpc.add_PluginServicer_to_server(
39+
PluginServicer(), server)
40+
server.add_insecure_port('[::]:50051')
41+
print("Starting server. Listening on port 50051")
42+
server.start()
43+
server.wait_for_termination()
44+
45+
46+
if __name__ == '__main__':
47+
logging.basicConfig()
48+
serve()

0 commit comments

Comments
 (0)