def add_path(self, path=None, operations=None, **kwargs): """Add a new path object to the spec. https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#paths-object- """ if path and 'basePath' in self.options: pattern = '^{0}'.format(re.escape(self.options['basePath'])) path = re.sub(pattern, '', path) path = Path(path=path, operations=operations) # Execute plugins' helpers for func in self._path_helpers: ret = func( self, path=path, operations=operations, **kwargs ) if isinstance(ret, Path): path.update(ret) if not path.path: raise APISpecError('Path template is not specified') # Process response helpers for any path operations defined. # Rule is that method + http status exist in both operations and helpers methods = set(iterkeys(path.operations)) & set(iterkeys(self._response_helpers)) for method in methods: responses = path.operations[method]['responses'] statuses = set(iterkeys(responses)) & set(iterkeys(self._response_helpers[method])) for status_code in statuses: for func in self._response_helpers[method][status_code]: responses[status_code].update( func(self, **kwargs) ) self._paths.setdefault(path.path, path).update(path)
def __init__(self, path=None, operations=None, **kwargs): self.path = path operations = operations or {} invalid = set(iterkeys(operations)) - set(VALID_METHODS) if invalid: raise APISpecError( 'One or more HTTP methods are invalid: {0}'.format(", ".join(invalid)) ) self.operations = operations kwargs.update(self.operations) super(Path, self).__init__(**kwargs)
def resolve_hooks(self): """Add in the decorated processors By doing this after constructing the class, we let standard inheritance do all the hard work. """ mro = inspect.getmro(self) hooks = defaultdict(list) for attr_name in dir(self): # Need to look up the actual descriptor, not whatever might be # bound to the class. This needs to come from the __dict__ of the # declaring class. for parent in mro: try: attr = parent.__dict__[attr_name] except KeyError: continue else: break else: # In case we didn't find the attribute and didn't break above. # We should never hit this - it's just here for completeness # to exclude the possibility of attr being undefined. continue try: hook_config = attr.__marshmallow_hook__ except AttributeError: pass else: for key in iterkeys(hook_config): # Use name here so we can get the bound method later, in # case the processor was a descriptor or something. hooks[key].append(attr_name) return hooks