<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def fetch_text_structure(urn, endpoint="http://cts.perseids.org/api/cts"):
 """ Fetches the text structure of a given work from a CTS endpoint. :param urn: the work's CTS URN (at the work-level!, e.g."urn:cts:greekLit:tlg0012.tlg001") :type urn: string :param endpoint: the URL of the CTS endpoint to use (defaults to Perseids') :type endpoint: string :return: a dict with keys "urn", "provenance", "valid_reffs", "levels" :rtype: dict """


    structure = {
        "urn": urn,
        "provenance": endpoint,
        "valid_reffs": {}
    }

    orig_edition = None
    suffix = 'grc' if 'greekLit' in urn else 'lat'
    resolver = HttpCtsResolver(HttpCtsRetriever(endpoint))
    work_metadata = resolver.getMetadata(urn)

    # among all editions for this work, pick the one in Greek or Latin
    try:
        orig_edition = next(iter(
            work_metadata.children[edition]
            for edition in work_metadata.children
            if suffix in str(work_metadata.children[edition].urn)
        ))
    except Exception as e:
        print(e)
        return None

    assert orig_edition is not None

    # get information about the work's citation scheme
    structure["levels"] = [
        (n + 1, level.name.lower())
        for n, level in enumerate(orig_edition.citation)
    ]

    # for each hierarchical level of the text
    # fetch all citable text elements
    for level_n, level_label in structure["levels"]:

        structure["valid_reffs"][level_n] = []
        for ref in resolver.getReffs(urn, level=level_n):
            print(ref)
            element = {
                "current": "{}:{}".format(urn, ref),
            }
            if "." in ref:
                element["parent"] = "{}:{}".format(
                    urn,
                    ".".join(ref.split(".")[:level_n - 1])
                )
            # TODO: parallelize this bit
            textual_node = resolver.getTextualNode(
                textId=urn,
                subreference=ref,
                prevnext=True
            )
            if textual_node.nextId is not None:
                element["previous"] = "{}:{}".format(urn, textual_node.nextId)
            if textual_node.prevId is not None:
                element["following"] = "{}:{}".format(urn, textual_node.prevId)
            structure["valid_reffs"][level_n].append(element)

    return structure