grpc initial documentation for grpc client

This commit is contained in:
bharnden 2019-03-25 22:14:56 -07:00
parent 51d93db586
commit 088230515e

View file

@ -1,3 +1,7 @@
"""
gRpc client for interfacing with CORE, when gRPC mode is enabled.
"""
from __future__ import print_function from __future__ import print_function
import logging import logging
@ -14,6 +18,13 @@ from core.grpc import core_pb2_grpc
def stream_listener(stream, handler): def stream_listener(stream, handler):
"""
Listen for stream events and provide them to the handler.
:param stream: grpc stream that will provide events
:param handler: function that handles an event
:return: nothing
"""
try: try:
for event in stream: for event in stream:
handler(event) handler(event)
@ -25,29 +36,74 @@ def stream_listener(stream, handler):
def start_streamer(stream, handler): def start_streamer(stream, handler):
"""
Convenience method for starting a grpc stream thread for handling streamed events.
:param stream: grpc stream that will provide events
:param handler: function that handles an event
:return: nothing
"""
thread = threading.Thread(target=stream_listener, args=(stream, handler)) thread = threading.Thread(target=stream_listener, args=(stream, handler))
thread.daemon = True thread.daemon = True
thread.start() thread.start()
class CoreGrpcClient(object): class CoreGrpcClient(object):
"""
Provides convenience methods for interfacing with the CORE grpc server.
"""
def __init__(self, address="localhost:50051"): def __init__(self, address="localhost:50051"):
"""
Creates a CoreGrpcClient instance.
:param str address: grpc server address to connect to
"""
self.address = address self.address = address
self.stub = None self.stub = None
self.channel = None self.channel = None
def create_session(self, _id=None): def create_session(self, _id=None):
"""
Create a session.
:param int _id: id for session, defaults to None will be created for you
:return: response with created session id
:rtype: core_pb2.CreateSessionResponse
"""
request = core_pb2.CreateSessionRequest(id=_id) request = core_pb2.CreateSessionRequest(id=_id)
return self.stub.CreateSession(request) return self.stub.CreateSession(request)
def delete_session(self, _id): def delete_session(self, _id):
"""
Delete a session.
:param int _id: id of session to delete
:return: response with result of deletion success or failure
:rtype: core_pb2.DeleteSessionResponse
:raises grpc.RpcError: when session doesn't exist
"""
request = core_pb2.DeleteSessionRequest(id=_id) request = core_pb2.DeleteSessionRequest(id=_id)
return self.stub.DeleteSession(request) return self.stub.DeleteSession(request)
def get_sessions(self): def get_sessions(self):
"""
Retrieves all currently known sessions.
:return: response with a list of currently known session, their state and number of nodes
:rtype: core_pb2.GetSessionsResponse
"""
return self.stub.GetSessions(core_pb2.GetSessionsRequest()) return self.stub.GetSessions(core_pb2.GetSessionsRequest())
def get_session(self, _id): def get_session(self, _id):
"""
Retrieve a session.
:param int _id: id of session to get data for
:return: response with sessions state, nodes, and links
:rtype: core_pb2.GetSessionResponse
:raises grpc.RpcError: when session doesn't exist
"""
request = core_pb2.GetSessionRequest(id=_id) request = core_pb2.GetSessionRequest(id=_id)
return self.stub.GetSession(request) return self.stub.GetSession(request)