def convert_namespaces_ast(<EOL>ast,<EOL>api_url: str = None,<EOL>namespace_targets: Mapping[str, List[str]] = None,<EOL>canonicalize: bool = False,<EOL>decanonicalize: bool = False,<EOL>):
if isinstance(ast, NSArg):<EOL><INDENT>given_term_id = "<STR_LIT>".format(ast.namespace, ast.value)<EOL>if (canonicalize and not ast.canonical) or (<EOL>decanonicalize and not ast.decanonical<EOL>):<EOL><INDENT>normalized_term = convert_nsarg(<EOL>given_term_id,<EOL>api_url=api_url,<EOL>namespace_targets=namespace_targets,<EOL>canonicalize=canonicalize,<EOL>decanonicalize=decanonicalize,<EOL>)<EOL>if canonicalize:<EOL><INDENT>ast.canonical = normalized_term<EOL><DEDENT>elif decanonicalize:<EOL><INDENT>ast.decanonical = normalized_term<EOL><DEDENT><DEDENT>if canonicalize:<EOL><INDENT>ns, value = ast.canonical.split("<STR_LIT::>")<EOL>ast.change_nsvalue(ns, value)<EOL><DEDENT>elif decanonicalize:<EOL><INDENT>ns, value = ast.canonical.split("<STR_LIT::>")<EOL>ast.change_nsvalue(ns, value)<EOL><DEDENT><DEDENT>if hasattr(ast, "<STR_LIT:args>"):<EOL><INDENT>for arg in ast.args:<EOL><INDENT>convert_namespaces_ast(<EOL>arg,<EOL>api_url=api_url,<EOL>namespace_targets=namespace_targets,<EOL>canonicalize=canonicalize,<EOL>decanonicalize=decanonicalize,<EOL>)<EOL><DEDENT><DEDENT>return ast<EOL>
Recursively convert namespaces of BEL Entities in BEL AST using API endpoint

    Canonicalization and decanonicalization is determined by endpoint used and namespace_targets.

    Args:
        ast (BEL): BEL AST
        api_url (str): endpoint url with a placeholder for the term_id (either /terms/<term_id>/canonicalized or /terms/<term_id>/decanonicalized)
        namespace_targets (Mapping[str, List[str]]): (de)canonical targets for converting BEL Entities

    Returns:
        BEL: BEL AST