2019-02-18 07:41:30 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
import logging
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
|
|
|
import grpc
|
|
|
|
|
|
|
|
import core_pb2
|
|
|
|
import core_pb2_grpc
|
|
|
|
|
|
|
|
|
|
|
|
class CoreApiClient(object):
|
|
|
|
def __init__(self, address="localhost:50051"):
|
|
|
|
self.address = address
|
|
|
|
self.stub = None
|
|
|
|
|
|
|
|
def get_sessions(self):
|
|
|
|
return self.stub.GetSessions(core_pb2.SessionsRequest())
|
|
|
|
|
2019-02-19 06:54:14 +00:00
|
|
|
def get_session(self, _id):
|
|
|
|
request = core_pb2.SessionRequest()
|
|
|
|
request.id = _id
|
|
|
|
return self.stub.GetSession(request)
|
|
|
|
|
2019-02-18 07:41:30 +00:00
|
|
|
@contextmanager
|
|
|
|
def connect(self):
|
|
|
|
channel = grpc.insecure_channel(self.address)
|
|
|
|
try:
|
|
|
|
self.stub = core_pb2_grpc.CoreApiStub(channel)
|
|
|
|
yield channel
|
|
|
|
finally:
|
|
|
|
channel.close()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
client = CoreApiClient()
|
|
|
|
with client.connect():
|
|
|
|
response = client.get_sessions()
|
|
|
|
print("core client received: %s" % response)
|
|
|
|
|
2019-02-19 06:54:14 +00:00
|
|
|
if len(response.sessions) > 0:
|
|
|
|
session_data = response.sessions[0]
|
|
|
|
session = client.get_session(session_data.id)
|
|
|
|
print(session)
|
|
|
|
|
2019-02-18 07:41:30 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
logging.basicConfig()
|
|
|
|
main()
|