"Fossies" - the Fresh Open Source Software Archive

Member "http-prompt-2.1.0/http_prompt/context/transform.py" (5 Mar 2021, 3382 Bytes) of package /linux/www/http-prompt-2.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "transform.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.0.0_vs_2.1.0.

    1 """Functions that transform a Context object to a different representation."""
    2 
    3 import json
    4 
    5 from http_prompt.utils import smart_quote
    6 
    7 
    8 def _noop(s):
    9     return s
   10 
   11 
   12 def _extract_httpie_options(context, quote=False, join_key_value=False,
   13                             excluded_keys=None):
   14     if quote:
   15         quote_func = smart_quote
   16     else:
   17         quote_func = _noop
   18 
   19     if join_key_value:
   20         def form_new_opts(k, v): return [k + '=' + v]
   21     else:
   22         def form_new_opts(k, v): return [k, v]
   23 
   24     excluded_keys = excluded_keys or []
   25 
   26     opts = []
   27     for k, v in sorted(context.options.items()):
   28         if k not in excluded_keys:
   29             if v is not None:
   30                 v = quote_func(v)
   31                 new_opts = form_new_opts(k, v)
   32             else:
   33                 new_opts = [k]
   34             opts += new_opts
   35     return opts
   36 
   37 
   38 def _extract_httpie_request_items(context, quote=False):
   39     if quote:
   40         quote_func = smart_quote
   41     else:
   42         quote_func = _noop
   43 
   44     items = []
   45     operators_and_items = [
   46         # (separator, dict_of_request_items)
   47         ('==', context.querystring_params),
   48         (':=', context.body_json_params),
   49         ('=', context.body_params),
   50         (':', context.headers)
   51     ]
   52     for sep, item_dict in operators_and_items:
   53         for k, value in sorted(item_dict.items()):
   54             if sep == ':=':
   55                 json_str = json.dumps(value,
   56                                       sort_keys=True)
   57                 item = '%s:=%s' % (k, quote_func(json_str))
   58                 items.append(item)
   59             elif isinstance(value, (list, tuple)):
   60                 for v in value:
   61                     item = quote_func('%s%s%s' % (k, sep, v))
   62                     items.append(item)
   63             else:
   64                 item = quote_func('%s%s%s' % (k, sep, value))
   65                 items.append(item)
   66     return items
   67 
   68 
   69 def extract_args_for_httpie_main(context, method=None):
   70     """Transform a Context object to a list of arguments that can be passed to
   71     HTTPie main function.
   72     """
   73     args = _extract_httpie_options(context)
   74 
   75     if method:
   76         args.append(method.upper())
   77 
   78     args.append(context.url)
   79     args += _extract_httpie_request_items(context)
   80     return args
   81 
   82 
   83 def format_to_curl(context, method=None):
   84     """Format a Context object to a cURL command."""
   85     raise NotImplementedError("curl format is not supported yet")
   86 
   87 
   88 def format_to_raw(context, method=None):
   89     """Format a Context object to HTTP raw text."""
   90     raise NotImplementedError("raw format is not supported yet")
   91 
   92 
   93 def format_to_httpie(context, method=None):
   94     """Format a Context object to an HTTPie command."""
   95     cmd = ['http'] + _extract_httpie_options(context, quote=True,
   96                                              join_key_value=True)
   97     if method:
   98         cmd.append(method.upper())
   99     cmd.append(context.url)
  100     cmd += _extract_httpie_request_items(context, quote=True)
  101     return ' '.join(cmd) + '\n'
  102 
  103 
  104 def format_to_http_prompt(context, excluded_options=None):
  105     """Format a Context object to HTTP Prompt commands."""
  106     cmds = _extract_httpie_options(context, quote=True, join_key_value=True,
  107                                    excluded_keys=excluded_options)
  108     cmds.append('cd ' + smart_quote(context.url))
  109     cmds += _extract_httpie_request_items(context, quote=True)
  110     return '\n'.join(cmds) + '\n'