def view_bind(app, cls_url, view_cls: Type['BaseView']):
    """
    将 API 绑定到 web 服务上
    :param view_cls:
    :param app:
    :param cls_url:
    :return:
    """
    if view_cls._no_route: return
    cls_url = cls_url or view_cls.__class__.__name__.lower()

    def add_route(name, route_info, beacon_info):
        for method in route_info['method']:
            async def beacon(request): pass
            route_key = route_info['url'] if route_info['url'] else name
            app._raw_app.router.add_route(method, urljoin('/api', cls_url, route_key), beacon)
            app.route._beacons[beacon] = beacon_info

    # noinspection PyProtectedMember
    for name, route_info_lst in view_cls._interface.items():
        for route_info in route_info_lst:
            real_handler = getattr(view_cls, name, None)
            if real_handler is None: continue # TODO: delete
            assert real_handler is not None, "handler must be exists"

            handler_name = '%s.%s' % (view_cls.__name__, real_handler.__name__)
            assert iscoroutinefunction(real_handler), "Add 'async' before interface function %r" % handler_name

            beacon_info = {
                'view': view_cls,
                'name': name,
                'handler': real_handler,
                'route_info': route_info
            }
            add_route(name, route_info, beacon_info)
def view_bind(app, cls_url, view_cls: Type['BaseView']):
    """
    将 API 绑定到 web 服务上
    :param view_cls:
    :param app:
    :param cls_url:
    :return:
    """
    if view_cls._no_route: return
    cls_url = cls_url or view_cls.__class__.__name__.lower()

    def add_route(name, route_info, beacon_info):
        for method in route_info['method']:
            async def beacon(request): pass
            route_key = route_info['url'] if route_info['url'] else name
            app._raw_app.router.add_route(method, urljoin('/api', cls_url, route_key), beacon)
            app.route._beacons[beacon] = beacon_info

    # noinspection PyProtectedMember
    for name, route_info_lst in view_cls._interface.items():
        for route_info in route_info_lst:
            real_handler = getattr(view_cls, name, None)
            if real_handler is None: continue # TODO: delete
            assert real_handler is not None, "handler must be exists"

            handler_name = '%s.%s' % (view_cls.__name__, real_handler.__name__)
            assert iscoroutinefunction(real_handler), "Add 'async' before interface function %r" % handler_name

            beacon_info = {
                'view': view_cls,
                'name': name,
                'handler': real_handler,
                'route_info': route_info
            }
            add_route(name, route_info, beacon_info)
def view_bind ( app , cls_url , view_cls : Type [ 'BaseView' ] ) : if view_cls . _no_route : return cls_url = cls_url or view_cls . __class__ . __name__ . lower ( ) def add_route ( name , route_info , beacon_info ) : for method in route_info [ 'method' ] : async def beacon ( request ) : pass route_key = route_info [ 'url' ] if route_info [ 'url' ] else name app . _raw_app . router . add_route ( method , urljoin ( '/api' , cls_url , route_key ) , beacon ) app . route . _beacons [ beacon ] = beacon_info # noinspection PyProtectedMember for name , route_info_lst in view_cls . _interface . items ( ) : for route_info in route_info_lst : real_handler = getattr ( view_cls , name , None ) if real_handler is None : continue # TODO: delete assert real_handler is not None , "handler must be exists" handler_name = '%s.%s' % ( view_cls . __name__ , real_handler . __name__ ) assert iscoroutinefunction ( real_handler ) , "Add 'async' before interface function %r" % handler_name beacon_info = { 'view' : view_cls , 'name' : name , 'handler' : real_handler , 'route_info' : route_info } add_route ( name , route_info , beacon_info )
将 API 绑定到 web 服务上
    :param view_cls:
    :param app:
    :param cls_url:
    :return:
将 API 绑定到 web 服务上 : param view_cls : : param app : : param cls_url : : return :