Skip to content

Suggested patch for a shorthand definition of quoted fields in http_urllib #693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions mqttwarn/services/http_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 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)

Expand Down Expand Up @@ -53,13 +55,36 @@ def plugin(srv, item):
pass

if params is not None:

# shorthand [ 'param1' , '@param2', '?param3' ]
# for { 'param1': '@param1', 'param2': '@param2', 'param3': '?param3' }
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.
# 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.data.get(params[key][1:], None)
if myitem is None:
params.pop(key,None)
else:
params[key] = myitem

else:
try:
Expand Down