Recommend wrapping the client class rather than subclassing

Based on Doug's comments in:

  https://github.com/markmc/oslo-incubator/pull/6
This commit is contained in:
Mark McLoughlin 2013-06-07 14:36:16 +01:00
parent b2d6dcd2ef
commit 20f19d1c70
1 changed files with 15 additions and 14 deletions

View File

@ -133,24 +133,24 @@ class RPCClient(object):
and returns immediately. A call() invocation waits for the server to send and returns immediately. A call() invocation waits for the server to send
a return value. a return value.
This class is intended to be used by subclassing it and providing methods This class is intended to be used by wrapping it in another class which
on the subclass which will perform the remote invocation using call() or provides methods on the subclass to perform the remote invocation using
cast():: call() or cast()::
class TestClient(messaging.RPCClient): class TestClient(object):
def __init__(self, transport): def __init__(self, transport):
target = messaging.Target(topic='testtopic', version='2.0') target = messaging.Target(topic='testtopic', version='2.0')
super(Client, self).__init__(transport, target) self._client = messaging.RPCClient(transport, target)
def test(self, ctxt, arg): def test(self, ctxt, arg):
return self.call(ctxt, 'test', arg=arg) return self._client.call(ctxt, 'test', arg=arg)
An example of using the prepare() method to override some attributes of the An example of using the prepare() method to override some attributes of the
default target:: default target::
def test(self, ctxt, arg): def test(self, ctxt, arg):
cctxt = self.prepare(version='2.5') cctxt = self._client.prepare(version='2.5')
return cctxt.call(ctxt, 'test', arg=arg) return cctxt.call(ctxt, 'test', arg=arg)
RPCClient have a number of other properties - timeout, check_for_lock and RPCClient have a number of other properties - timeout, check_for_lock and
@ -158,18 +158,19 @@ class RPCClient(object):
so they too can be passed to prepare():: so they too can be passed to prepare()::
def test(self, ctxt, arg): def test(self, ctxt, arg):
cctxt = self.prepare(check_for_lock=False, timeout=10) cctxt = self._client.prepare(check_for_lock=False, timeout=10)
return cctxt.call(ctxt, 'test', arg=arg) return cctxt.call(ctxt, 'test', arg=arg)
However, this class can be used directly without subclassing. For example: However, this class can be used directly without wrapping it another class.
For example:
transport = messaging.get_transport(cfg.CONF) transport = messaging.get_transport(cfg.CONF)
target = messaging.Target(topic='testtopic', version='2.0') target = messaging.Target(topic='testtopic', version='2.0')
client = messaging.RPCClient(transport, target) client = messaging.RPCClient(transport, target)
client.call(ctxt, 'test', arg=arg) client.call(ctxt, 'test', arg=arg)
but this is probably only useful in limited circumstances as the subclass but this is probably only useful in limited circumstances as a wrapper
will usually help to make the code much more obvious. class will usually help to make the code much more obvious.
""" """
def __init__(self, transport, target, def __init__(self, transport, target,