diff options
Diffstat (limited to 'dcaeapplib')
-rw-r--r-- | dcaeapplib/dcaeapplib/__init__.py | 15 | ||||
-rw-r--r-- | dcaeapplib/tests/test_dcaeapplib.py | 9 |
2 files changed, 20 insertions, 4 deletions
diff --git a/dcaeapplib/dcaeapplib/__init__.py b/dcaeapplib/dcaeapplib/__init__.py index e81b6fc..18b60fe 100644 --- a/dcaeapplib/dcaeapplib/__init__.py +++ b/dcaeapplib/dcaeapplib/__init__.py @@ -50,6 +50,17 @@ class _handler(BaseHTTPRequestHandler): else: self.send_error(404) +def _genauth(sinfo): + """ + Return authentication parameters for stream, if present. + """ + user = sinfo['aaf_username'] if 'aaf_username' in sinfo else None + password = sinfo['aaf_password'] if 'aaf_password' in sinfo else None + if user and password: + return { 'auth': (user, password) } + else: + return {} + class DcaeEnv: def __init__(self, healthCB = lambda:True, reconfigCB = lambda:None): """ @@ -107,7 +118,7 @@ class DcaeEnv: del self._unread[stream] return ret gid = sinfo['client_id'] if 'client_id' in sinfo and sinfo['client_id'] else _groupid - resp = requests.get('{0}/{1}/{2}?timeout={3}&limit={4}'.format(sinfo['dmaap_info']['topic_url'], gid, _clientid, timeout_ms, limit), auth=(sinfo['aaf_username'], sinfo['aaf_password'])) + resp = requests.get('{0}/{1}/{2}?timeout={3}&limit={4}'.format(sinfo['dmaap_info']['topic_url'], gid, _clientid, timeout_ms, limit), **_genauth(sinfo)) resp.raise_for_status() x = resp.json() if len(x) == 0: @@ -125,7 +136,7 @@ class DcaeEnv: """ sinfo = self._config['streams_publishes'][stream] body = '{0}.{1}.{2}{3}'.format(len(partition), len(data), partition, data) - resp = requests.post('{0}'.format(sinfo['dmaap_info']['topic_url']), auth=(sinfo['aaf_username'], sinfo['aaf_password']), headers={'Content-Type': 'application/cambria'}, data=body) + resp = requests.post('{0}'.format(sinfo['dmaap_info']['topic_url']), headers={'Content-Type': 'application/cambria'}, data=body, **_genauth(sinfo)) resp.raise_for_status() def getconfig(self): diff --git a/dcaeapplib/tests/test_dcaeapplib.py b/dcaeapplib/tests/test_dcaeapplib.py index fabec41..9995264 100644 --- a/dcaeapplib/tests/test_dcaeapplib.py +++ b/dcaeapplib/tests/test_dcaeapplib.py @@ -37,8 +37,8 @@ class Stubs: }, "streams_publishes": { "myoutputstream": { - "aaf_username": "user2", - "aaf_password": "pass2", + "aaf_username": None, + "aaf_password": None, "dmaap_info": { "topic_url": "http://messagerouter.example.com:3904/events/topic2" } @@ -84,18 +84,23 @@ def test_todo(monkeypatch): env.start() # exercise start when already running env.stop() def stub_get(*args, **kwargs): + stuff.auth = 'auth' in kwargs return stuff def stub_post(url, data, *args, **kwargs): assert data == '4.11.asdfhello world' + stuff.auth = 'auth' in kwargs stuff.posted = True return stuff monkeypatch.setattr(requests, 'post', stub_post) stuff.posted = False + stuff.auth = True env.senddata('myoutputstream', 'asdf', 'hello world') assert stuff.posted == True + assert stuff.auth == False monkeypatch.setattr(requests, 'get', stub_get) assert env.hasdata('myinputstream') is False assert env.getdata('myinputstream') == 's1' + assert stuff.auth == True stuff.toreturn = [ 'a1' ] assert env.getdata('myinputstream') == 's2' assert env.hasdata('myinputstream') is True |