Compare commits

..

1 Commits

Author SHA1 Message Date
Serghei Mihai c94efb7d1c wscalls: unflatten payload when calling webservice (#66916)
gitea/wcs/pipeline/head This commit looks good Details
2024-02-29 12:09:20 +01:00
2 changed files with 19 additions and 11 deletions

View File

@ -299,8 +299,13 @@ class WebserviceCallStatusItem(WorkflowStatusItem):
),
},
hint=_(
'Parameters names could be separated by / character to create object or array payload,'
' ex: parameter named "element/child" with value "value" will be sent as {"element": {"child": "value"}.'
'The / in parameter name allows to generate complex objects. '
'Thus a parameter named "element/child" containing "value" will generate the payload '
'"element": {"child": "value"}. If the subkey, i.e. "child", is an integer it will '
'become a list index and two elements "element/0", "element/1" containing "value1" '
'and "value2" will generate the payload "element": ["value1", "value2"]. '
'It is possible to combine the two types, for example "element/0/key1" to generate '
'a list of objects.'
),
)

View File

@ -17,7 +17,6 @@
import collections
import hashlib
import json
import re
import urllib.parse
import xml.etree.ElementTree as ET
@ -53,7 +52,7 @@ class UnflattenDataException(Exception):
pass
def unflatten_data(d, separator='/'):
def unflatten_data(d):
"""Transform:
{"a/b/0/x": "1234"}
@ -69,13 +68,13 @@ def unflatten_data(d, separator='/'):
def split_key(key):
def map_key(x):
if x.isnumeric():
if misc.is_ascii_digit(x):
return int(x)
elif isinstance(x, str):
return x.replace('%s%s' % (separator, separator), separator)
return x.replace('//', '/')
return x
return [map_key(x) for x in re.split(r'(?<!%s)%s(?!%s)' % (separator, separator, separator), key)]
return [map_key(x) for x in key.split('/')]
keys = [(split_key(key), key) for key in d]
try:
@ -105,8 +104,7 @@ def unflatten_data(d, separator='/'):
assert isinstance(d, list)
if len(d) < key:
raise UnflattenDataException(
_('incomplete array before %s in %s')
% (separator.join(map(str, path[: i + 1])), orig_key)
_('incomplete array before %s in %s') % ('/'.join(map(str, path[: i + 1])), orig_key)
)
if len(d) == key:
d.append(new)
@ -496,8 +494,13 @@ class WsCallRequestWidget(CompositeWidget):
'data-dynamic-display-value': methods.get('POST'),
},
hint=_(
'Parameters names could be separated by / character to create object or array payload,'
' ex: parameter named "element/child" with value "value" will be sent as {"element": {"child": "value"}.'
'The / in parameter name allows to generate complex objects. '
'Thus a parameter named "element/child" containing "value" will generate the payload '
'"element": {"child": "value"}. If the subkey, i.e. "child", is an integer it will '
'become a list index and two elements "element/0", "element/1" containing "value1" '
'and "value2" will generate the payload "element": ["value1", "value2"]. '
'It is possible to combine the two types, for example "element/0/key1" to generate '
'a list of objects.'
),
)