pschmitt / fortipy

FortiManger API bindings for Python
GNU General Public License v3.0
11 stars 11 forks source link

fortipy._get() result parsing changed #9

Closed robertpenz closed 6 years ago

robertpenz commented 6 years ago

The parsing of the result in _get(), didn't work in all cases for me. e.g. the original code did not work if result was an list and data a dict - the last return was used which through an exception. As I'm not sure which other cases are possible I'm not submitting a pull request, do show you my changes.

ps: the isinstance is just an habit as "is list" does not work for object that can be accessed like lists.

original code:

        if type(res['result']) is list:
            if 'data' in res['result'][0]:
                if not res['result'][0]['data']:
                    return
                if len(res['result'][0]['data']) > 0 and type(res['result'][0]['data']) is list:
                    if res['result'][0]['data']:
                        return [x for x in res['result'][0]['data']]
        return res['result']['data']

my changed code:

        if isinstance(res['result'], list):
            if 'data' in res['result'][0]:
                if isinstance(res['result'][0]['data'], list):
                    return [x for x in res['result'][0]['data']]
                else:
                    # its a dict, just return it
                    return res['result'][0]['data'] 
            else:
                logger.warning("unhandled case - don't know if thats possible")
        return res['result']['data']
robertpenz commented 6 years ago

I put into my PR.