def test_get_service_methods(): class Iface(object): def __init__(self): pass def hello(self): pass def world(self, foo): pass assert set(["hello", "world"]) == get_service_methods(Iface)
def client_for(service, service_module, thrift_service_name=None): """Build a synchronous client class for the given Thrift service. The generated class accepts a TChannelSyncClient and an optional hostport as initialization arguments. Given ``CommentService`` defined in ``comment.thrift`` and registered with Hyperbahn under the name "comment", here's how this might be used: .. code-block:: python from tchannel.sync import TChannelSyncClient from tchannel.sync.thrift import client_for from comment import CommentService CommentServiceClient = client_for('comment', CommentService) tchannel_sync = TChannelSyncClient('my-service') comment_client = CommentServiceClient(tchannel_sync) future = comment_client.postComment( articleId, CommentService.Comment("hi") ) result = future.result() :param service: Name of the Hyperbahn service being called. :param service_module: The Thrift-generated module for that service. This usually has the same name as definied for the service in the IDL. :param thrift_service_name: If the Thrift service has a different name than its module, use this parameter to specify it. :returns: An Thrift-like class, ready to be instantiated and used with TChannelSyncClient. """ assert service_module, 'service_module is required' service = service or '' # may be blank for non-hyperbahn use cases if not thrift_service_name: thrift_service_name = service_module.__name__.rsplit('.', 1)[-1] method_names = get_service_methods(service_module.Iface) def init(self, tchannel_sync, hostport=None, trace=False): self.async_thrift = self.__async_client_class__( tchannel_sync._async_client, hostport, trace, ) self.threadloop = tchannel_sync._threadloop init.__name__ = '__init__' methods = { '__init__': init, '__async_client_class__': async_client_for( service, service_module, thrift_service_name, ) } methods.update({ method_name: generate_method(method_name) for method_name in method_names }) return type(thrift_service_name + 'Client', (object,), methods)