Search code examples
pythonsmartsheet-api

How to parse Smartsheet API 2.0 Python SDK error results?


I'm trying to get the update_rows method working

(the answers here haven't helped yet:

cannot update row using Smartsheet API

)

and would like to capture and parse the results

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

print(results)

gives me this:

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

Note that a success looks like this (snipped most of it):

{"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 ...

This looks like a dictionary, but the keys, items, values aren't recognized. Next it looks like json - but nothing I've tried (I don't know much about json yet) has worked either.

If I could get the resultCode from a success, that would be a start. Better would be the value from result, but that appears to be a dictionary for a fail and a list for successes.

I'm confused. Any help appreciated. I'm using Python 3.5, Smartsheet API 2.0 Python SDK

Craig


Solution

  • I figured it out.

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

    returns an result object from the models\error_result.py code of the SDK.

    That object has two methods of interest and each property can be referenced like so:

    print(results.result.code)
    

    returns the code (1062 for example)

    The two methods are to_dict and to_json which can be accessed and printed like so:

    print(results.result.to_dict())
    

    gives:

        {'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)
    

    gives:

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

    and the to_json code

    print(results.result.to_json())
    

    gives

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

    and:

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

    gives:

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