Beispiel #1
0
def get_schema_url(routes: RoutesConfig, base_url: http.URL) -> Optional[str]:
    """
    Given the application routes, return the URL path of the API Schema.
    """
    for route in walk(routes):
        if route.view is serve_schema:
            return urljoin(base_url, route.path)
    return None  # pragma: nocover
Beispiel #2
0
def get_schema_url(routes: RoutesConfig, base_url: http.URL=None) -> Optional[str]:
    """
    Given the application routes, return the URL path of the API Schema.
    """
    for route in walk(routes):
        if route.view is serve_schema:
            return urljoin(base_url, route.path)
    return None  # pragma: nocover
Beispiel #3
0
def get_wsgi_server(app: App) -> Callable:
    lookup = app.router.lookup
    # FIFO Cache for URL lookups:
    lookup_cache = OrderedDict()  # type: OrderedDict
    lookup_cache_size = app.settings.get(['ROUTING', 'LOOKUP_CACHE_SIZE'],
                                         DEFAULT_LOOKUP_CACHE_SIZE)
    preloaded = app.preloaded

    # Pre-fill the lookup cache for URLs without path arguments.
    for path, method, view in routing.walk(app.routes):
        if '{' not in path:
            key = method.upper() + ' ' + path
            lookup_cache[key] = lookup(path, method)

    def func(environ: Mapping, start_response: Callable) -> Iterator:
        method = environ['REQUEST_METHOD']
        path = environ['PATH_INFO']
        lookup_key = method + ' ' + path
        state = {
            'wsgi_environ': environ,
            'method': method,
            'path': path,
            'exception': None,
            'view': None,
            'url_path_args': {},
        }
        state.update(preloaded)

        try:
            try:
                (state['view'], pipeline,
                 state['url_path_args']) = lookup_cache[lookup_key]
            except KeyError:
                (state['view'], pipeline,
                 state['url_path_args']) = lookup_cache[lookup_key] = lookup(
                     path, method)
                if len(lookup_cache) > lookup_cache_size:
                    lookup_cache.pop(next(iter(lookup_cache)))

            for function, inputs, output, extra_kwargs in pipeline:
                # Determine the keyword arguments for each step in the pipeline.
                kwargs = {}
                for arg_name, state_key in inputs:
                    kwargs[arg_name] = state[state_key]
                if extra_kwargs is not None:
                    kwargs.update(extra_kwargs)

                # Call the function for each step in the pipeline.
                state[output] = function(**kwargs)
        except Exception as exc:
            state['exception'] = exc
            pipelines.run_pipeline(app.router.exception_pipeline, state)

        wsgi_response = state['wsgi_response']
        start_response(wsgi_response.status, wsgi_response.headers)
        return wsgi_response.iterator

    return func
Beispiel #4
0
def get_preloaded_components(routes: routing.RoutesConfig) -> Set[type]:
    preloaded_components = set()

    for path, method, view in routing.walk(routes):
        view_signature = inspect.signature(view)
        for param in view_signature.parameters.values():
            if getattr(param.annotation, 'preload', False):
                preloaded_components.add(param.annotation)

    return preloaded_components
Beispiel #5
0
def get_preloaded_components(routes: routing.RoutesConfig) -> Set[type]:
    preloaded_components = set()

    for path, method, view in routing.walk(routes):
        view_signature = inspect.signature(view)
        for param in view_signature.parameters.values():
            if getattr(param.annotation, 'preload', False):
                preloaded_components.add(param.annotation)

    return preloaded_components
Beispiel #6
0
def get_schema_content(routes: RoutesConfig) -> Dict[str, Route]:
    """
    Given the application routes, return a dictionary containing all the
    Links that the service exposes.
    """
    content = {}
    for route in walk(routes):
        view = route.view
        if getattr(view, 'exclude_from_schema', False):
            continue
        name = view.__name__
        link = get_link(route)
        content[name] = link
    return content
Beispiel #7
0
def get_schema_content(routes: RoutesConfig) -> Dict[str, Route]:
    """
    Given the application routes, return a dictionary containing all the
    Links that the service exposes.
    """
    content = {}
    for route in walk(routes):
        view = route.view
        if getattr(view, 'exclude_from_schema', False):
            continue
        name = view.__name__
        link = get_link(route)
        content[name] = link
    return content
Beispiel #8
0
def get_wsgi_server(app: App) -> Callable:
    lookup = app.router.lookup
    # FIFO Cache for URL lookups:
    lookup_cache = OrderedDict()  # type: OrderedDict
    lookup_cache_size = app.settings.get(
        ['ROUTING', 'LOOKUP_CACHE_SIZE'],
        DEFAULT_LOOKUP_CACHE_SIZE
    )
    preloaded = app.preloaded

    # Pre-fill the lookup cache for URLs without path arguments.
    for path, method, view in routing.walk(app.routes):
        if '{' not in path:
            key = method.upper() + ' ' + path
            lookup_cache[key] = lookup(path, method)

    def func(environ: Mapping, start_response: Callable) -> Iterator:
        method = environ['REQUEST_METHOD']
        path = environ['PATH_INFO']
        lookup_key = method + ' ' + path
        state = {
            'wsgi_environ': environ,
            'method': method,
            'path': path,
            'exception': None,
            'view': None,
            'url_path_args': {},
        }
        state.update(preloaded)

        try:
            try:
                (state['view'], pipeline, state['url_path_args']) = lookup_cache[lookup_key]
            except KeyError:
                (state['view'], pipeline, state['url_path_args']) = lookup_cache[lookup_key] = lookup(path, method)
                if len(lookup_cache) > lookup_cache_size:
                    lookup_cache.pop(next(iter(lookup_cache)))

            for function, inputs, output, extra_kwargs in pipeline:
                # Determine the keyword arguments for each step in the pipeline.
                kwargs = {}
                for arg_name, state_key in inputs:
                    kwargs[arg_name] = state[state_key]
                if extra_kwargs is not None:
                    kwargs.update(extra_kwargs)

                # Call the function for each step in the pipeline.
                state[output] = function(**kwargs)
        except Exception as exc:
            state['exception'] = exc
            custom_handler = app.settings.get('exception_handler')
            if custom_handler:
                try:
                    custom_handler()
                except TypeError:
                    raise ConfigurationError("exception_handler settings must be callable.")
            core.run_pipeline(app.router.exception_pipeline, state)

        wsgi_response = state['wsgi_response']
        start_response(wsgi_response.status, wsgi_response.headers)
        return wsgi_response.iterator

    return func