def handler()

in source/backend/functions/drawio/main.py [0:0]


def handler(event, context):
    """
    Main Lambda Handler
    """
    node_dict = dict()

    data = json.loads(event['body'])['data']
    nodes = data.get('nodes', [])
    edges = data.get('edges', [])

    for node in nodes:
        node_id = node['data']['id']
        node_type = node['data']['type']
        if node_type == 'resource':
            node_type = node['data']['image'].split('/')[-1].split('.')[0]
        label = node['data']['label'].replace(' - $0', '')
        title = node['data']['title']
        level = node['data']['level']
        x = node['position']['x']
        y = node['position']['y']
        is_end_node = ('children' not in node['data'])
        parent = node['data'].get('parent')
        node = Node(node_id, node_type, label, title, level, x, y, is_end_node)

        node_dict[node_id] = node

        if parent and parent in node_dict:
            node_dict[parent].add_child(node)

    elements = list(node_dict.values())
    elements.sort(key=lambda x: x.level)

    for edge in edges:
        edge_id = edge['data']['id']
        source = edge['data']['source']
        target = edge['data']['target']
        edge = Edge(edge_id, source, target)

        elements.append(edge)

    xml_output = produce_xml_output(elements)

    # Compress and encode XML tree
    xml_output_compressed_encoded = deflate_and_base64_encode(xml_output)
    # Convert XML encoded string to URL encoded string
    xml_output_url = urllib.parse.quote(xml_output_compressed_encoded, safe='')
    # Attach XML string to Draw IO URL (Note: Draw IO is not app.diagram.net due to .io vulnerabilities)
    drawio_url = 'https://app.diagrams.net?title=AWS%20Architecture%20Diagram.xml#R' + xml_output_url

    return {
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Headers': 'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
        },
        'statusCode': 200,
        'body': json.dumps(drawio_url)
    }