import json


def safeJson(result, path=[]):
    """Take the result of a requests call and format it into a structured dict."""
    if result.status_code != 200:
        output = {'code': result.status_code, 'content': result.text}
        print("ConfigManager: get secret failed (token expired?)")
        print(json.dumps(output, indent=2))
        return output
    output = result.json()
    if path:
        for level in path:
            try:
                output = output[level]
            except:
                return {'code': 200, 'content': "path not found: {0}".format(path)}
    return output