def setup(app: web.Application, *, disable_login=False): log.debug("Setting up %s [service: %s] ...", __name__, SERVICE_NAME) cfg = app[APP_CONFIG_KEY][CONFIG_SECTION_NAME] if not cfg["enabled"]: log.warning("Service '%s' explicitly disabled in config", SERVICE_NAME) return # subscribe to rabbit upon startup # TODO: Define connection policies (e.g. {on-startup}, lazy). Could be defined in config-file app.on_startup.append(subscribe) # TODO: add function to "unsubscribe" # app.on_cleanup.append(unsubscribe) if not APP_OPENAPI_SPECS_KEY in app: log.warning( "rest submodule not initialised? computation routes will not be defined!" ) return specs = app[APP_OPENAPI_SPECS_KEY] routes = map_handlers_with_operations( { 'start_pipeline': computation_api.start_pipeline.__wrapped__ if disable_login else computation_api.start_pipeline, 'update_pipeline': computation_api.update_pipeline.__wrapped__ if disable_login else computation_api.update_pipeline }, filter(lambda o: "/computation" in o[1], iter_path_operations(specs)), strict=True) app.router.add_routes(routes)
def setup_catalog(app: web.Application, *, disable_auth=False): # ---------------------------------------------- # TODO: temporary, just to check compatibility between # trafaret and pydantic schemas cfg = assert_valid_config(app).copy() # --------------------------------------------- # resolve url app[f"{__name__}.catalog_origin"] = URL.build(scheme="http", host=cfg["host"], port=cfg["port"]) app[f"{__name__}.catalog_version_prefix"] = cfg["version"] specs = app[APP_OPENAPI_SPECS_KEY] # validated openapi specs # bind routes with handlers handler = (_reverse_proxy_handler.__wrapped__ if disable_auth else _reverse_proxy_handler) routes = [ web.route(method.upper(), path, handler, name=operation_id) for method, path, operation_id, tags in iter_path_operations(specs) if "catalog" in tags ] assert routes, "Got no paths tagged as catalog" # nosec # reverse proxy to catalog's API app.router.add_routes(routes)
def setup(app: web.Application, *, disable_login=False): """ Sets up director's subsystem :param app: main application :type app: web.Application :param disable_login: disabled auth requirements for subsystem's rest (for debugging), defaults to False :param disable_login: bool, optional """ cfg = app[APP_CONFIG_KEY][CONFIG_SECTION_NAME] # director service API base url, e.g. http://director:8081/v0 app[APP_DIRECTOR_API_KEY] = build_api_url(cfg) # setup routes ------------ specs = app[APP_OPENAPI_SPECS_KEY] def include_path(tup_object): _method, path, _operation_id, _tags = tup_object return any( tail in path for tail in ["/running_interactive_services", "/services"] ) handlers_dict = {"services_get": handlers.services_get} # Disables login_required decorator for testing purposes if disable_login: for name, hnds in handlers_dict.items(): if hasattr(hnds, "__wrapped__"): handlers_dict[name] = hnds.__wrapped__ routes = map_handlers_with_operations( handlers_dict, filter(include_path, iter_path_operations(specs)), strict=True ) app.router.add_routes(routes)
def setup(app: web.Application): # subscribe to rabbit upon startup # TODO: Define connection policies (e.g. {on-startup}, lazy). Could be defined in config-file app.on_startup.append(subscribe) # TODO: add function to "unsubscribe" # app.on_cleanup.append(unsubscribe) if not APP_OPENAPI_SPECS_KEY in app: log.warning( "rest submodule not initialised? computation routes will not be defined!" ) return specs = app[APP_OPENAPI_SPECS_KEY] routes = map_handlers_with_operations( { "start_pipeline": computation_handlers.start_pipeline, "update_pipeline": computation_handlers.update_pipeline, }, filter(lambda o: "/computation" in o[1], iter_path_operations(specs)), strict=True, ) app.router.add_routes(routes) setup_comp_tasks_listener(app)
def setup(app: web.Application): # routes specs = app[APP_OPENAPI_SPECS_KEY] routes = map_handlers_with_operations( get_handlers_from_namespace(tags_handlers), filter(lambda o: "tag" in o[3], iter_path_operations(specs)), strict=True, ) app.router.add_routes(routes)
def setup(app: web.Application): # routes related with users specs = app[APP_OPENAPI_SPECS_KEY] routes = map_handlers_with_operations( get_handlers_from_namespace(users_handlers), filter(lambda o: "me" in o[1].split("/"), iter_path_operations(specs)), strict=True, ) app.router.add_routes(routes)
def setup(app: web.Application, *, disable_login=False): """ Sets up director's subsystem :param app: main application :type app: web.Application :param disable_login: disabled auth requirements for subsystem's rest (for debugging), defaults to False :param disable_login: bool, optional """ logger.debug("Setting up %s ...", __name__) cfg = app[APP_CONFIG_KEY][CONFIG_SECTION_NAME] if not cfg["enabled"]: logger.warning("'%s' explicitly disabled in config", __name__) return # director service API base url, e.g. http://director:8081/v0 app[APP_DIRECTOR_API_KEY] = build_api_url(cfg) set_registry(app, InteractiveServiceLocalRegistry()) # setup routes ------------ specs = app[APP_OPENAPI_SPECS_KEY] def include_path(tup_object): _method, path, _operation_id = tup_object return any(tail in path for tail in ['/running_interactive_services', '/services']) handlers_dict = { 'running_interactive_services_post': handlers.running_interactive_services_post, 'running_interactive_services_get': handlers.running_interactive_services_get, 'running_interactive_services_delete': handlers.running_interactive_services_delete, 'running_interactive_services_delete_all': handlers.running_interactive_services_delete_all, 'services_get': handlers.services_get } # Disables login_required decorator for testing purposes if disable_login: for name, hnds in handlers_dict.items(): if hasattr(hnds, '__wrapped__'): handlers_dict[name] = hnds.__wrapped__ routes = map_handlers_with_operations(handlers_dict, filter(include_path, iter_path_operations(specs)), strict=True) app.router.add_routes(routes) # setup cleanup context -------------- app.cleanup_ctx.append(director_client_ctx)
def setup(app: web.Application, *, debug=False): logger.debug("Setting up %s %s...", __name__, "[debug]" if debug else "") assert CONFIG_SECTION_NAME not in app[APP_CONFIG_KEY], "Not section for the moment" # routes specs = app[APP_OPENAPI_SPECS_KEY] routes = map_handlers_with_operations( get_handlers_from_namespace(users_handlers), filter(lambda o: "me" in o[1].split("/"), iter_path_operations(specs)), strict=True ) app.router.add_routes(routes)
def test_filtered_routing(specs): handlers = Handlers() found = get_handlers_from_namespace(handlers) hdl_sel = {name: hdl for name, hdl in found.items() if "i" in name} opr_iter = ((mth, url, opname, _tags) for mth, url, opname, _tags in iter_path_operations(specs) if "i" in opname) routes = map_handlers_with_operations(hdl_sel, opr_iter, strict=True) for rdef in routes: assert rdef.method == "GET" assert rdef.handler in hdl_sel.values()
def setup(app: web.Application): # setup routes ------------ specs = app[APP_OPENAPI_SPECS_KEY] def include_path(tup_object): _method, path, _operation_id, _tags = tup_object return any(tail in path for tail in ["/activity/status"]) handlers_dict = {"get_status": handlers.get_status} routes = map_handlers_with_operations( handlers_dict, filter(include_path, iter_path_operations(specs)), strict=True ) app.router.add_routes(routes)
def _create_routes(prefix, handlers_module, specs, disable_login): handlers = get_handlers_from_namespace(handlers_module) if disable_login: # Disables login_required decorator for testing purposes handlers = {name: hnds.__wrapped__ for name, hnds in handlers.items()} routes = map_handlers_with_operations(handlers, filter(lambda o: prefix in o[1], iter_path_operations(specs)), strict=True) if disable_login: logger.debug("%s-%s:\n%s", CONFIG_SECTION_NAME, prefix, pformat(routes)) return routes
def _create_routes(tag, handlers_module, specs, *, disable_login=False): """ :param disable_login: Disables login_required decorator for testing purposes defaults to False :type disable_login: bool, optional """ # TODO: Remove 'disable_login' and use instead a mock.patch on the decorator! handlers = get_handlers_from_namespace(handlers_module) if disable_login: handlers = {name: hnds.__wrapped__ for name, hnds in handlers.items()} routes = map_handlers_with_operations( handlers, filter(lambda o: tag in o[3], iter_path_operations(specs)), strict=True, ) if disable_login: logger.debug("%s:\n%s", CONFIG_SECTION_NAME, pformat(routes)) return routes
def setup_activity(app: web.Application): #---------------------------------------------- # TODO: temporary, just to check compatibility between # trafaret and pydantic schemas assert_valid_config(app) #--------------------------------------------- # setup routes ------------ specs = app[APP_OPENAPI_SPECS_KEY] def include_path(tup_object): _method, path, _operation_id, _tags = tup_object return any(tail in path for tail in ["/activity/status"]) handlers_dict = {"get_status": handlers.get_status} routes = map_handlers_with_operations( handlers_dict, filter(include_path, iter_path_operations(specs)), strict=True ) app.router.add_routes(routes)
def setup_director_v2(app: web.Application): # create settings and injects in app create_settings(app) if not APP_OPENAPI_SPECS_KEY in app: log.warning( "rest submodule not initialised? computation routes will not be defined!" ) return specs = app[APP_OPENAPI_SPECS_KEY] # bind routes with handlers routes = map_handlers_with_operations( { "start_pipeline": start_pipeline, "stop_pipeline": stop_pipeline, }, filter(lambda o: "computation" in o[1], iter_path_operations(specs)), strict=True, ) app.router.add_routes(routes)
def create(specs: openapi.Spec) -> List[web.RouteDef]: """ Creates routes mapping operators_id with handler functions :param specs: validated oas :type specs: openapi.Spec :return: list of web routes for auth :rtype: List[web.RouteDef] """ log.debug("Creating %s ", __name__) base_path = openapi.get_base_path(specs) def include_path(tuple_object): _method, path, _operation_id, _tags = tuple_object return path.startswith(base_path + "/auth/") handlers_map = { "auth_register": login_handlers.register, "auth_login": login_handlers.login, "auth_logout": login_handlers.logout, "auth_reset_password": login_handlers.reset_password, "auth_reset_password_allowed": login_handlers.reset_password_allowed, "auth_change_email": login_handlers.change_email, "auth_change_password": login_handlers.change_password, "auth_confirmation": login_handlers.email_confirmation, "create_api_key": api_keys_handlers.create_api_key, "delete_api_key": api_keys_handlers.delete_api_key, "list_api_keys": api_keys_handlers.list_api_keys, } routes = map_handlers_with_operations(handlers_map, filter(include_path, iter_path_operations(specs)), strict=True) log.debug("Mapped auth routes: %s", "\n".join([pformat(r) for r in routes])) return routes
def setup_studies_dispatcher(app: web.Application) -> bool: cfg = app[APP_CONFIG_KEY]["main"] # Redirects routes redirect_handler = get_redirection_to_viewer if not cfg["studies_access_enabled"]: redirect_handler = login_required(get_redirection_to_viewer) logger.warning( "'%s' config explicitly disables anonymous users from this feature", __name__, ) app.router.add_routes( [web.get("/view", redirect_handler, name="get_redirection_to_viewer")]) # Rest-API routes: maps handlers with routes tags with "viewer" based on OAS operation_id specs = app[APP_OPENAPI_SPECS_KEY] rest_routes = map_handlers_with_operations( rest_handler_functions, filter(lambda op: "viewer" in op.tags, iter_path_operations(specs)), strict=True, ) app.router.add_routes(rest_routes)
def setup_director(app: web.Application, *, disable_login=False): """Sets up director's subsystem :param app: main application :type app: web.Application :param disable_login: disabled auth requirements for subsystem's rest (for debugging), defaults to False :param disable_login: bool, optional """ # ---------------------------------------------- # TODO: temporary, just to check compatibility between # trafaret and pydantic schemas _ , settings = assert_valid_config(app) # --------------------------------------------- # director service API base url, e.g. http://director:8081/v0 app[APP_DIRECTOR_API_KEY] = str(settings.url) # setup routes ------------ specs = app[APP_OPENAPI_SPECS_KEY] def include_path(tup_object): _method, path, _operation_id, _tags = tup_object return any(tail in path for tail in ["/running_interactive_services"]) handlers_dict = {} # Disables login_required decorator for testing purposes if disable_login: for name, hnds in handlers_dict.items(): if hasattr(hnds, "__wrapped__"): handlers_dict[name] = hnds.__wrapped__ routes = map_handlers_with_operations( handlers_dict, filter(include_path, iter_path_operations(specs)), strict=True ) app.router.add_routes(routes)