"Fossies" - the Fresh Open Source Software Archive

Member "http-prompt-2.1.0/http_prompt/context/__init__.py" (5 Mar 2021, 4938 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 "__init__.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 from http_prompt.tree import Node
    2 
    3 
    4 class Context(object):
    5 
    6     def __init__(self, url=None, spec=None):
    7         self.url = url
    8         self.headers = {}
    9         self.querystring_params = {}
   10         self.body_params = {}
   11         self.body_json_params = {}
   12         self.options = {}
   13         self.should_exit = False
   14 
   15         # Create a tree for supporting API spec and ls command
   16         self.root = Node('root')
   17         if spec:
   18             if not self.url:
   19                 schemes = spec.get('schemes')
   20                 scheme = schemes[0] if schemes else 'https'
   21                 self.url = (scheme + '://' +
   22                             spec.get('host', 'http://localhost:8000') +
   23                             spec.get('basePath', ''))
   24 
   25             base_path_tokens = list(filter(lambda s: s,
   26                                     spec.get('basePath', '').split('/')))
   27             paths = spec.get('paths')
   28             if paths:
   29                 for path in paths:
   30                     path_tokens = (base_path_tokens +
   31                                    list(filter(lambda s: s, path.split('/'))))
   32                     if path == '/':  # Path is a trailing slash
   33                         path_tokens.insert(len(base_path_tokens), '/')
   34                     elif path[-1] == '/':  # Path ends with a trailing slash
   35                         path_tokens[-1] = path_tokens[-1] + '/'
   36                     self.root.add_path(*path_tokens)
   37                     endpoint = dict(paths[path])
   38                     # path parameters (apply to all paths if not overriden)
   39                     # exclude $ref as we have no system to handle that now
   40                     global_parameters = list(endpoint.pop('parameters', []))
   41                     # not used
   42                     endpoint.pop('servers', None)
   43                     endpoint.pop('$ref', None)
   44                     endpoint.pop('summary', None)
   45                     endpoint.pop('description', None)
   46                     for method, info in endpoint.items():
   47                         params = info.get('parameters', [])
   48                         params = list(global_parameters + params)
   49                         if params:
   50                             parameter_key = lambda i: (
   51                                     i.get('$ref', None),
   52                                     i.get('name', None),
   53                                     i.get('in', None)
   54                             )
   55                             # parameter is overriden based on $ref/in/name value
   56                             # last value (local definition) takes precedence
   57                             params_map = dict([
   58                                 (parameter_key(p), p)
   59                                 for p in params
   60                             ])
   61                             params = params_map.values()
   62                             for param in params:
   63                                 if param.get("$ref"):
   64                                     for section in param.get("$ref").split('/'):
   65                                         param = param.get(section) if not section == "#" else spec
   66 
   67                                 if param.get('in') != 'path':
   68                                     # Note that for completion mechanism, only
   69                                     # name/node_type is used
   70                                     # Parameters from methods/location
   71                                     # are merged
   72                                     full_path = path_tokens + [param['name']]
   73                                     self.root.add_path(*full_path,
   74                                                        node_type='file')
   75         elif not self.url:
   76             self.url = 'http://localhost:8000'
   77 
   78     def __eq__(self, other):
   79         return (self.url == other.url and
   80                 self.headers == other.headers and
   81                 self.options == other.options and
   82                 self.querystring_params == other.querystring_params and
   83                 self.body_params == other.body_params and
   84                 self.body_json_params == other.body_json_params and
   85                 self.should_exit == other.should_exit)
   86 
   87     def copy(self):
   88         context = Context(self.url)
   89         context.headers = self.headers.copy()
   90         context.querystring_params = self.querystring_params.copy()
   91         context.body_params = self.body_params.copy()
   92         context.body_json_params = self.body_json_params.copy()
   93         context.options = self.options.copy()
   94         context.should_exit = self.should_exit
   95         return context
   96 
   97     def update(self, context):
   98         if context.url:
   99             self.url = context.url
  100 
  101         self.headers.update(context.headers)
  102         self.querystring_params.update(context.querystring_params)
  103         self.body_params.update(context.body_params)
  104         self.body_json_params.update(context.body_json_params)
  105         self.options.update(context.options)
  106         self.should_exit = self.should_exit