Example #1
0
    def __init__(self, info, version='', url=None, patterns=None, urlconf=None):
        """

        :param openapi.Info info: information about the API
        :param str version: API version string; if omitted, `info.default_version` will be used
        :param str url: API scheme, host and port; if ``None`` is passed and ``DEFAULT_API_URL`` is not set, the url
            will be inferred from the request made against the schema view, so you should generally not need to set
            this parameter explicitly; if the empty string is passed, no host and scheme will be emitted

            If `url` is not ``None`` or the empty string, it must be a scheme-absolute uri (i.e. starting with http://
            or https://), and any path component is ignored;

            See also: :ref:`documentation on base URL construction <custom-spec-base-url>`
        :param patterns: if given, only these patterns will be enumerated for inclusion in the API spec
        :param urlconf: if patterns is not given, use this urlconf to enumerate patterns;
            if not given, the default urlconf is used
        """
        self._gen = SchemaGenerator(info.title, url, info.get('description', ''), patterns, urlconf)
        self.info = info
        self.version = version
        self.consumes = []
        self.produces = []

        if url is None and swagger_settings.DEFAULT_API_URL is not None:
            url = swagger_settings.DEFAULT_API_URL

        if url:
            parsed_url = urlparse.urlparse(url)
            if parsed_url.scheme not in ('http', 'https') or not parsed_url.netloc:
                raise SwaggerGenerationError("`url` must be an absolute HTTP(S) url")
            if parsed_url.path:
                logger.warning("path component of api base URL %s is ignored; use FORCE_SCRIPT_NAME instead" % url)
Example #2
0
    def endpoints(self) -> dict:
        """ 获得所有的端点

        return: {
            {path:method}: {callback}
        }
        """
        if not hasattr(self, '_path_method_conf'):
            self._path_method_conf = {}
            sg = SchemaGenerator()
            sg.get_schema()
            for path, method, callback in sg.endpoints:
                endpoint = path + ':' + method
                # 如果存在版本控制,遍历出所有的 version
                if '{version}' in path:
                    for allowed_versions in settings.REST_FRAMEWORK['ALLOWED_VERSIONS']:
                        version_endpoint = endpoint.replace('{version}', allowed_versions)
                        if version_endpoint not in self.path_unauthorized_conf:
                            version_path = path.replace('{version}', allowed_versions)
                            r = getattr(self.anonymous, method.lower())(version_path, )
                            if r.status_code == 404:
                                continue
                        self._path_method_conf[version_endpoint] = callback
                else:
                    self._path_method_conf[endpoint] = callback
        return self._path_method_conf
Example #3
0
    def handle(self, *args, **options):
        assert coreapi is not None, 'coreapi must be installed.'

        generator = SchemaGenerator(url=options['url'],
                                    title=options['title'],
                                    description=options['description'])

        schema = generator.get_schema(request=None, public=True)

        renderer = self.get_renderer(options['format'])
        output = renderer.render(schema, renderer_context={})
        self.stdout.write(output.decode())
Example #4
0
    def __init__(self, info, version, url=None, patterns=None, urlconf=None):
        """

        :param .Info info: information about the API
        :param str version: API version string, takes preedence over the version in `info`
        :param str url: API
        :param patterns: if given, only these patterns will be enumerated for inclusion in the API spec
        :param urlconf: if patterns is not given, use this urlconf to enumerate patterns;
            if not given, the default urlconf is used
        """
        self._gen = SchemaGenerator(info.title, url,
                                    info.get('description',
                                             ''), patterns, urlconf)
        self.info = info
        self.version = version
Example #5
0
    def __init__(self,
                 info,
                 version='',
                 url=swagger_settings.DEFAULT_API_URL,
                 patterns=None,
                 urlconf=None):
        """

        :param .Info info: information about the API
        :param str version: API version string; can be omitted to use `info.default_version`
        :param str url: API url; can be empty to remove URL info from the result
        :param patterns: if given, only these patterns will be enumerated for inclusion in the API spec
        :param urlconf: if patterns is not given, use this urlconf to enumerate patterns;
            if not given, the default urlconf is used
        """
        self._gen = SchemaGenerator(info.title, url,
                                    info.get('description',
                                             ''), patterns, urlconf)
        self.info = info
        self.version = version
 def __init__(self, name='API', version=''):
     self._gen = SchemaGenerator(name, None, '', None, None)
     self.version = version
     self.consumes = []
     self.produces = []