Пример #1
0
    def get_paths(self, endpoints, components, request, public):
        """Generate the Swagger Paths for the API from the given endpoints."""
        #pylint:disable=too-many-locals
        if not endpoints:
            return openapi.Paths(paths={}), ''

        prefix = self.determine_path_prefix(list(endpoints.keys())) or ''
        assert '{' not in prefix, "base path cannot be templated in swagger 2.0"

        paths = OrderedDict()
        for path, (view_cls, methods) in sorted(endpoints.items()):
            operations = {}
            for method, view, _ in methods:
                if not public and not self._gen.has_view_permissions(
                        path, method, view):
                    continue

                operation = self.get_operation(view, path, prefix, method,
                                               components, request)
                if operation is not None:
                    operations[method.lower()] = operation

            if operations:
                # since the common prefix is used as the API basePath,
                # it must be stripped from individual paths when writing
                # them into the swagger document
                path_suffix = path[len(prefix):]
                if not path_suffix.startswith('/'):
                    path_suffix = '/' + path_suffix
                paths[path_suffix] = self.get_path_item(
                    path, view_cls, operations)

        return openapi.Paths(paths=paths), prefix
Пример #2
0
 def get_paths_object(self, paths):
     return openapi.Paths({
         p: paths[p]
         for x in
         ["api-token-auth", "api-token-refresh", "users", "services"]
         for p in list(paths.keys()) if p.startswith(x, 1)
     })
Пример #3
0
    def get_paths(self, endpoints, components, request, public):
        """Generate the Swagger Paths for the API from the given endpoints.

        Args:
            endpoints (dict): endpoints as returned by get_endpoints
            components (ReferenceResolver): resolver/container for Swagger References
            request (Request): the request made against the schema view; can be None
            public (bool): if True, all endpoints are included regardless of access through
                           `request`

        Returns:
            tuple[openapi.Paths,str]: The :class:`openapi.Paths` object and the longest common path
                                      prefix, as a 2-tuple
        """
        if not endpoints:
            return openapi.Paths(paths={}), ''
        endpoints = self.convert_endpoint_path_params(endpoints)

        plugin_filter = None
        if 'plugin' in request.GET:
            plugin_filter = request.GET['plugin']
        prefix = ''
        resources = {}
        resource_example = {}
        paths = OrderedDict()
        for path, (view_cls, methods) in sorted(endpoints.items()):
            operations = {}
            for method, view in methods:
                if plugin_filter:
                    if view.__module__.split('.')[0] != plugin_filter:
                        continue
                if not public and not self._gen.has_view_permissions(
                        path, method, view):
                    continue
                operation = self.get_operation(view, path, prefix, method,
                                               components, request)
                if operation is not None:
                    operations[method.lower()] = operation
                    tag = operation.tags[0]
                    tag_dict = {"name": tag, "x-displayName": tag.title()}
                    tag_exists = False
                    for tag in self.tags:
                        if tag["name"] == tag_dict["name"]:
                            tag_exists = True
                    if not tag_exists:
                        self.tags.append(tag_dict)

            if operations:
                path_param = None

                if '}' in path:
                    resource_path = '%s}/' % path.rsplit(sep='}',
                                                         maxsplit=1)[0]
                    if resource_path in endpoints:
                        view = endpoints[resource_path][0]
                        if not hasattr(view,
                                       'queryset') or view.queryset is None:
                            if hasattr(view, 'model'):
                                resource_model = view.model
                            else:
                                continue
                        else:
                            resource_model = view.queryset.model
                        resource_name = self.get_parameter_name(resource_model)
                        prefix_ = None
                        if issubclass(resource_model, RepositoryVersion):
                            prefix_ = view_cls.parent_viewset.endpoint_name
                        param_name = self.get_parameter_slug_from_model(
                            resource_model, prefix_)
                        if resource_path in resources:
                            path = path.replace(
                                resource_path,
                                '{%s}' % resources[resource_path])
                        else:
                            resources[resource_path] = param_name
                            resource_example[
                                resource_path] = self.get_example_uri(path)
                            path = path.replace(
                                resource_path,
                                '{%s}' % resources[resource_path])
                        example = resource_example[resource_path]
                        resource_description = self.get_resource_description(
                            resource_name, example)
                        path_param = openapi.Parameter(
                            name=param_name,
                            description=resource_description,
                            required=True,
                            in_=openapi.IN_PATH,
                            type=openapi.TYPE_STRING,
                        )
                        paths[path] = openapi.PathItem(parameters=[path_param],
                                                       **operations)
                    else:
                        if not path.startswith('/'):
                            path = '/' + path
                        paths[path] = self.get_path_item(
                            path, view_cls, operations)
                else:
                    paths[path] = openapi.PathItem(parameters=[path_param],
                                                   **operations)

        return Paths(paths=paths), prefix
Пример #4
0
 def get_paths(self, endpoints, components, request, public):
     return openapi.Paths(paths={}), ''
Пример #5
0
    def get_paths(self, endpoints, components, request, public):
        """Generate the Swagger Paths for the API from the given endpoints.

        Args:
            endpoints (dict): endpoints as returned by get_endpoints
            components (ReferenceResolver): resolver/container for Swagger References
            request (Request): the request made against the schema view; can be None
            public (bool): if True, all endpoints are included regardless of access through
                           `request`

        Returns:
            tuple[openapi.Paths,str]: The :class:`openapi.Paths` object and the longest common path
                                      prefix, as a 2-tuple
        """
        if not endpoints:
            return openapi.Paths(paths={}), ''

        prefix = ''
        resources = {}
        resource_example = {}
        paths = OrderedDict()
        for path, (view_cls, methods) in sorted(endpoints.items()):
            operations = {}
            for method, view in methods:
                if not public and not self._gen.has_view_permissions(
                        path, method, view):
                    continue

                operation = self.get_operation(view, path, prefix, method,
                                               components, request)
                if operation is not None:
                    operation.operation_id = operation.operation_id.replace(
                        'pulp_api_v3_', '')
                    operations[method.lower()] = operation

            if operations:
                path_param = None

                if '}' in path:
                    resource_path = '%s}/' % path.rsplit(sep='}',
                                                         maxsplit=1)[0]
                    resource_other_path = self.get_resource_from_path(path)
                    if resource_other_path in endpoints:
                        resource_model = endpoints[resource_other_path][
                            0].queryset.model
                        resource_name = self.get_parameter_name(resource_model)
                        param_name = self.get_parameter_slug_from_model(
                            resource_model)
                        if resource_path in resources:
                            path = path.replace(
                                resource_path,
                                '{%s}' % resources[resource_path])
                        elif resource_other_path in resources:
                            path = path.replace(
                                resource_path,
                                '{%s}' % resources[resource_other_path])
                        else:
                            resources[resource_path] = param_name
                            resource_example[
                                resource_path] = self.get_example_uri(path)
                            path = path.replace(
                                resource_path,
                                '{%s}' % resources[resource_path])
                        example = resource_example[resource_other_path]
                        resource_description = self.get_resource_description(
                            resource_name, example)
                        path_param = openapi.Parameter(
                            name=param_name,
                            description=resource_description,
                            required=True,
                            in_=openapi.IN_PATH,
                            type=openapi.TYPE_STRING,
                        )
                        paths[path] = openapi.PathItem(parameters=[path_param],
                                                       **operations)
                    else:
                        if not path.startswith('/'):
                            path = '/' + path
                        paths[path] = self.get_path_item(
                            path, view_cls, operations)
                else:
                    paths[path] = openapi.PathItem(parameters=[path_param],
                                                   **operations)

        return Paths(paths=paths), prefix