Skip to content
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

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

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 @@
pass

if params is not None:

# shorthand [ 'param1' , '@param2', '?param3' ]
# for { 'param1': '@param1', 'param2': '@param2', 'param3': '?param3' }
if isinstance(params, list):

Check warning on line 61 in mqttwarn/services/http_urllib.py

View check run for this annotation

Codecov / codecov/patch

mqttwarn/services/http_urllib.py#L61

Added line #L61 was not covered by tests
# create dict from list and replace params
newparams = {}
for key in params:
if key.startswith('@') or key.startswith('?'):
newparams[key[1:]] = key

Check warning on line 66 in mqttwarn/services/http_urllib.py

View check run for this annotation

Codecov / codecov/patch

mqttwarn/services/http_urllib.py#L63-L66

Added lines #L63 - L66 were not covered by tests
else:
newparams[key] = '@' + key
params = newparams

Check warning on line 69 in mqttwarn/services/http_urllib.py

View check run for this annotation

Codecov / codecov/patch

mqttwarn/services/http_urllib.py#L68-L69

Added lines #L68 - L69 were not covered by tests

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")

Check warning on line 80 in mqttwarn/services/http_urllib.py

View check run for this annotation

Codecov / codecov/patch

mqttwarn/services/http_urllib.py#L80

Added line #L80 was not covered by tests

elif params[key].startswith('?'):
myitem = item.data.get(params[key][1:], None)
if myitem is None:
params.pop(key,None)

Check warning on line 85 in mqttwarn/services/http_urllib.py

View check run for this annotation

Codecov / codecov/patch

mqttwarn/services/http_urllib.py#L82-L85

Added lines #L82 - L85 were not covered by tests
else:
params[key] = myitem

Check warning on line 87 in mqttwarn/services/http_urllib.py

View check run for this annotation

Codecov / codecov/patch

mqttwarn/services/http_urllib.py#L87

Added line #L87 was not covered by tests

else:
try:
Expand Down
Loading