【问题标题】:How to parse Smartsheet API 2.0 Python SDK error results?如何解析 Smartsheet API 2.0 Python SDK 错误结果?
【发布时间】:2017-01-09 02:46:03
【问题描述】:

我正在尝试让 update_rows 方法工作

(这里的答案还没有帮助:

cannot update row using Smartsheet API

)

并且想要捕获和解析结果

results = smartsheet.Sheets.update_rows(test_sheet_id, [row])

print(results)

给我这个:

{"requestResponse": null, "result": {"shouldRetry": false, "name":
"InvalidRowLocationError", "code": 1062, "recommendation": "Do not retry
without fixing the problem.", "message": "Invalid row location.",
"statusCode": 400}}

请注意,成功看起来像这样(大部分都被剪掉了):

{"resultCode": 0, "message": "SUCCESS", "version": 21, "result":
[{"discussions": [], "createdAt": null, "above": null, "modifiedAt": 
null, "columns": [], "toTop": null, "sheetId": null, "siblingId":
4800885606901636, "permalink": null, "id": 6067523002099588,
"accessLevel": null, "conditionalFormat": null, "attachments": [],
"cells": [{"columnType": null, "displayValue": null, "linksOutToCells":
null, "strict": true, "hyperlink": null, "formula": null, "format": null,
"conditionalFormat": null, "columnId": 7600931584927620, "linkInFromCell":
 null, "value": null}, {"columnType": null, "displayValue": null, "
... snip ...

这看起来像字典,但无法识别键、项和值。 接下来它看起来像 json - 但我没有尝试过(我对 json 还不太了解)。

如果我能从成功中获得 resultCode,那将是一个开始。 result 中的值会更好,但这似乎是失败的字典和成功的列表。

我很困惑。任何帮助表示赞赏。 我正在使用 Python 3.5、Smartsheet API 2.0 Python SDK

克雷格

【问题讨论】:

    标签: python smartsheet-api


    【解决方案1】:

    我想通了。

    results = smartsheet.Sheets.update_rows(test_sheet_id, [row])
    

    从 SDK 的 models\error_result.py 代码中返回一个 result 对象。

    该对象有两个感兴趣的方法,每个属性都可以像这样引用:

    print(results.result.code)
    

    返回代码(例如 1062)

    这两个方法是 to_dictto_json 可以像这样访问和打印:

    print(results.result.to_dict())
    

    给予:

        {'shouldRetry': False, 'name': 'InvalidRowLocationError', 'code': 1062,
        'recommendation': 'Do not retry without fixing the problem.', 'message':
        'Invalid row location.', 'statusCode': 400}
    
    my_dict = results.result.to_dict()
    for key, value in my_dict.items():
        print(key, value)
    

    给予:

        shouldRetry False
        name InvalidRowLocationError
        code 1062
        recommendation Do not retry without fixing the problem.
        message Invalid row location.
        statusCode 400
    

    to_json 代码

    print(results.result.to_json())
    

    给予

        {
            "shouldRetry": false,
            "name": "InvalidRowLocationError",
            "code": 1062,
            "recommendation": "Do not retry without fixing the problem.",
            "message": "Invalid row location.",
            "statusCode": 400
        }
    

    和:

    my_json = results.result.to_json()
    my_dict = json.loads(my_json)
    for key, value in my_dict.items():
        print(key, value)
    

    给予:

        shouldRetry False
        name InvalidRowLocationError
        code 1062
        recommendation Do not retry without fixing the problem.
        message Invalid row location.
        statusCode 400
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多