From 5d447aad0476fdab8173a20f2bb48fe75bc10178 Mon Sep 17 00:00:00 2001 From: Olaf Schott Date: Tue, 6 Feb 2024 20:05:30 +0100 Subject: [PATCH 1/4] Update http_urllib.py add list shorthand for params dict + allow optional params in query string --- mqttwarn/services/http_urllib.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/mqttwarn/services/http_urllib.py b/mqttwarn/services/http_urllib.py index dce5b047..acb456ea 100644 --- a/mqttwarn/services/http_urllib.py +++ b/mqttwarn/services/http_urllib.py @@ -23,7 +23,9 @@ def plugin(srv, item): - """ addrs: (method, url, dict(params), list(username, password), json) """ + """ addrs: (method, url, dict(params), list(username, password), json) + or (shorthand) + addrs: (method, url, list(params), list(username, password), json) """ srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target) @@ -53,14 +55,35 @@ def plugin(srv, item): pass if params is not None: + + # shorthand [ 'param1' ] for { 'param1': '{param1}, ...} + if isinstance(params, list): + # create dict from list and replace params + newparams = {} + for key in params: + if key.startswith('@') or key.startswith('?'): + newparams[key[1:]] = key + else: + newparams[key] = key + params = newparams + for key in list(params.keys()): # { 'q' : '@message' } # Quoted field, starts with '@'. Do not use .format, instead grab # the item's [message] and inject as parameter value. + # { 'x' : '?param' } + # optional field, add to query string only if it exists in data and is not empty if params[key].startswith('@'): # "@message" params[key] = item.get(params[key][1:], "NOP") + elif params[key].startswith('?'): + myitem = item.get(params[key][1:], None) + if myitem is None: + params.pop(key,None) + else: + params[key] = myitem + else: try: params[key] = params[key].format(**item.data).encode('utf-8') From 71b8e3b69691e2f89953df12f110ff141cfbf46d Mon Sep 17 00:00:00 2001 From: Olaf Schott Date: Tue, 6 Feb 2024 21:26:18 +0100 Subject: [PATCH 2/4] small change --- mqttwarn/services/http_urllib.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mqttwarn/services/http_urllib.py b/mqttwarn/services/http_urllib.py index acb456ea..a10387b6 100644 --- a/mqttwarn/services/http_urllib.py +++ b/mqttwarn/services/http_urllib.py @@ -56,7 +56,7 @@ def plugin(srv, item): if params is not None: - # shorthand [ 'param1' ] for { 'param1': '{param1}, ...} + # shorthand [ 'param1' ] for { 'param1': '{param1}', ...} if isinstance(params, list): # create dict from list and replace params newparams = {} @@ -64,7 +64,7 @@ def plugin(srv, item): if key.startswith('@') or key.startswith('?'): newparams[key[1:]] = key else: - newparams[key] = key + newparams[key] = '@' + key params = newparams for key in list(params.keys()): @@ -73,7 +73,7 @@ def plugin(srv, item): # Quoted field, starts with '@'. Do not use .format, instead grab # the item's [message] and inject as parameter value. # { 'x' : '?param' } - # optional field, add to query string only if it exists in data and is not empty + # optional quoted field, add to query string only if it exists in item's data and is not empty if params[key].startswith('@'): # "@message" params[key] = item.get(params[key][1:], "NOP") From aac55ebb33f222afa46b3266c11d688e1b9133ad Mon Sep 17 00:00:00 2001 From: Olaf Schott Date: Tue, 6 Feb 2024 21:33:40 +0100 Subject: [PATCH 3/4] description --- mqttwarn/services/http_urllib.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mqttwarn/services/http_urllib.py b/mqttwarn/services/http_urllib.py index a10387b6..94f1646d 100644 --- a/mqttwarn/services/http_urllib.py +++ b/mqttwarn/services/http_urllib.py @@ -24,8 +24,8 @@ def plugin(srv, item): """ addrs: (method, url, dict(params), list(username, password), json) - or (shorthand) - addrs: (method, url, list(params), list(username, password), json) """ + or (shorthand for quoted fields) + addrs: (method, url, list(param_names), list(username, password), json) """ srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target) @@ -73,7 +73,8 @@ def plugin(srv, item): # Quoted field, starts with '@'. Do not use .format, instead grab # the item's [message] and inject as parameter value. # { 'x' : '?param' } - # optional quoted field, add to query string only if it exists in item's data and is not empty + # Optional quoted field, add to query string only if it exists + # in item's data and is not empty if params[key].startswith('@'): # "@message" params[key] = item.get(params[key][1:], "NOP") From 675fbb069d4504d0e955215e86cc8695e1edd513 Mon Sep 17 00:00:00 2001 From: Olaf Schott Date: Thu, 8 Feb 2024 17:08:51 +0100 Subject: [PATCH 4/4] Update http_urllib.py --- mqttwarn/services/http_urllib.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mqttwarn/services/http_urllib.py b/mqttwarn/services/http_urllib.py index 94f1646d..10b7f97a 100644 --- a/mqttwarn/services/http_urllib.py +++ b/mqttwarn/services/http_urllib.py @@ -56,7 +56,8 @@ def plugin(srv, item): if params is not None: - # shorthand [ 'param1' ] for { 'param1': '{param1}', ...} + # shorthand [ 'param1' , '@param2', '?param3' ] + # for { 'param1': '@param1', 'param2': '@param2', 'param3': '?param3' } if isinstance(params, list): # create dict from list and replace params newparams = {} @@ -72,14 +73,14 @@ def plugin(srv, item): # { 'q' : '@message' } # Quoted field, starts with '@'. Do not use .format, instead grab # the item's [message] and inject as parameter value. - # { 'x' : '?param' } + # new { 'x' : '?param' } # Optional quoted field, add to query string only if it exists # in item's data and is not empty if params[key].startswith('@'): # "@message" - params[key] = item.get(params[key][1:], "NOP") + params[key] = item.data.get(params[key][1:], "NOP") elif params[key].startswith('?'): - myitem = item.get(params[key][1:], None) + myitem = item.data.get(params[key][1:], None) if myitem is None: params.pop(key,None) else: