예제 #1
0
    def __init__(self, routes: RouteConfig) -> None:
        rules = []  # type: typing.List[Rule]
        views = {}  # type: typing.Dict[str, typing.Callable]

        for path, method, view, name in flatten_routes(routes):
            if name in views:
                msg = ('Route wtih name "%s" exists more than once. Use an '
                       'explicit name="..." on the Route to avoid a conflict.'
                       ) % name
                raise exceptions.ConfigurationError(msg)

            template = uritemplate.URITemplate(path)
            werkzeug_path = str(path)

            parameters = inspect.signature(view).parameters

            for arg in template.variable_names:
                converter = self._get_converter(parameters, arg, view)
                template_format = '{%s}' % arg
                werkzeug_format = '<%s:%s>' % (converter, arg)
                werkzeug_path = werkzeug_path.replace(template_format,
                                                      werkzeug_format)

            rule = Rule(werkzeug_path, methods=[method], endpoint=name)
            rules.append(rule)
            views[name] = view

        self._routes = routes
        self._adapter = Map(rules).bind('')
        self._views = views

        # Use an MRU cache for router lookups.
        self._lookup_cache = collections.OrderedDict(
        )  # type: collections.OrderedDict
        self._lookup_cache_size = 10000
예제 #2
0
def test_flatten_routes_not_order_sensitive():
    """
    Tests that prefixes from Includes don't affect later Routes
    https://github.com/encode/apistar/issues/247
    """
    flat_routes = core.flatten_routes(routes)
    assert flat_routes[10].path == '/x'
    assert flat_routes[11].path == '/another_subpath/x'
예제 #3
0
    def __init__(self, router: Router, routes: RouteConfig) -> None:
        try:
            url = router.reverse_url('serve_schema')
        except exceptions.NoReverseMatch:
            url = None

        content = {}
        for route in flatten_routes(routes):
            if getattr(route.view, 'exclude_from_schema', False):
                continue
            content[route.name] = get_link(route)

        super().__init__(url=url, content=content)
예제 #4
0
    def __init__(self, router: Router, routes: RouteConfig,
                 settings: Settings) -> None:
        try:
            url = router.reverse_url('serve_schema')
        except exceptions.NoReverseMatch:
            url = None

        content = {}
        for route in flatten_routes(routes):
            if getattr(route.view, 'exclude_from_schema', False):
                continue
            content[route.name] = get_link(route)

        schema_settings = settings.get('SCHEMA', {})
        title = schema_settings.get('TITLE', '')
        description = schema_settings.get('DESCRIPTION', '')
        url = schema_settings.get('URL', url)

        super().__init__(title=title,
                         description=description,
                         url=url,
                         content=content)