コード例 #1
0
ファイル: normalization.py プロジェクト: asuradoll/BlackSheep
def normalize_handler(
        route: Route,
        services: Services) -> Callable[[Request], Awaitable[Response]]:
    method = route.handler

    sig = Signature.from_callable(method)
    params = sig.parameters
    params_len = len(params)

    if any(
            str(param).startswith("*")
            or param.kind.value == _ParameterKind.KEYWORD_ONLY
            for param in params.values()):
        raise UnsupportedSignatureError(method)

    if inspect.iscoroutinefunction(method):
        normalized = get_async_wrapper(services, route, method, params,
                                       params_len)
    else:
        normalized = get_sync_wrapper(services, route, method, params,
                                      params_len)

    if normalized is not method:
        copy_special_attributes(method, normalized)
        _copy_name_and_docstring(method, normalized)

    return normalized
コード例 #2
0
def get_middlewares_chain(middlewares, handler):
    fn = handler
    for middleware in reversed(middlewares):
        if not middleware:
            continue
        wrapper_fn = middleware_partial(middleware, fn)

        copy_special_attributes(fn, wrapper_fn)
        fn = wrapper_fn
    return fn
コード例 #3
0
ファイル: normalization.py プロジェクト: keshapps/BlackSheep
def normalize_handler(route, services):
    method = route.handler

    sig = Signature.from_callable(method)
    params = sig.parameters
    params_len = len(params)

    if inspect.iscoroutinefunction(method):
        normalized = get_async_wrapper(services, route, method, params,
                                       params_len)
    else:
        normalized = get_sync_wrapper(services, route, method, params,
                                      params_len)

    if normalized is not method:
        copy_special_attributes(method, normalized)
        _copy_name_and_docstring(method, normalized)

    return normalized
コード例 #4
0
ファイル: normalization.py プロジェクト: perfmjs/BlackSheep
def normalize_handler(route, services):
    method = route.handler

    sig = Signature.from_callable(method)
    params = sig.parameters
    params_len = len(params)

    if any(str(param).startswith('*') for param in params.values()):
        raise UnsupportedSignatureError(method)

    if inspect.iscoroutinefunction(method):
        normalized = get_async_wrapper(services, route, method, params,
                                       params_len)
    else:
        normalized = get_sync_wrapper(services, route, method, params,
                                      params_len)

    if normalized is not method:
        copy_special_attributes(method, normalized)
        _copy_name_and_docstring(method, normalized)

    return normalized
コード例 #5
0
def normalize_handler(
    route: Route, services: Services
) -> Callable[[Request], Awaitable[Response]]:
    method = route.handler

    sig = Signature.from_callable(method)
    params = _get_method_annotations_base(method)
    params_len = len(params)

    if any(
        str(param).startswith("*") or param.kind.value == _ParameterKind.KEYWORD_ONLY
        for param in params.values()
    ):
        raise UnsupportedSignatureError(method)

    return_type = sig.return_annotation

    # normalize input
    if inspect.iscoroutinefunction(method):
        normalized = get_async_wrapper(services, route, method, params, params_len)
    else:
        normalized = get_sync_wrapper(services, route, method, params, params_len)

    # normalize output
    if return_type is _empty or return_type is not Response:
        if return_type is not _empty:
            # this scenario enables a more accurate automatic generation of
            # OpenAPI Documentation, for responses
            setattr(route.handler, "return_type", return_type)
        normalized = get_async_wrapper_for_output(normalized)

    if normalized is not method:
        setattr(normalized, "root_fn", method)
        copy_special_attributes(method, normalized)

    return normalized