import json
import re
import click
from jsonpath_ng import jsonpath, parse
[docs]def snake_to_camel(snake_case_str):
"""Converts a snake_case string to camelCase.
Args:
snake_case_str: The string in snake_case format.
Returns:
The converted string in camelCase format.
"""
components = snake_case_str.split("_")
return components[0] + "".join(x.title() for x in components[1:])
[docs]def process_output(output, select, response, text=False, binary=False):
if output and binary:
output.write(response)
elif output and not binary and text:
output.write(f"{response.get('response', '')}".replace("\\n", "\n").encode())
elif output and not binary and not text:
output.write(json.dumps(response, indent=4))
if select:
jsonpath_expr = parse(select)
result = [str(match.value) for match in jsonpath_expr.find(response)]
click.echo("\n".join(result))
else:
if output is None and binary:
click.echo(response)
elif output is None and not binary and text:
click.echo(response.get("response", "").replace("\\n", "\n"))
elif output is None and not binary and not text:
click.echo(json.dumps(response, indent=4))
[docs]def add_payload_value(vars_dict, _input, binary):
payload = _input.read() if _input else None
if _input and not binary:
payload = json.loads(payload) if not isinstance(payload, dict) else payload
for var in vars_dict:
_var = snake_to_camel(var)
value = input_value_parsing(vars_dict[var])
if var in payload:
payload[var] = value
elif _var in payload:
payload[_var] = value
vars_dict["payload"] = payload
elif _input and binary:
vars_dict["data"] = payload
[docs]def create_payload(args):
vars_dict = dict(var.split("=") for var in args)
payload = {}
for k, v in vars_dict.items():
payload[k] = v
return payload