box-python-sdk icon indicating copy to clipboard operation
box-python-sdk copied to clipboard

Why is there a difference?

Open c-rei opened this issue 1 year ago • 4 comments

When I get a folder information with "client.folders.get_folder_by_id(folder_id, fields=['name', 'path_collection'])", I can get a path_collection that is a FolderPathCollectionField object.

But when I get a subfolder information with "client.folders.get_folder_items(folder_id, fields=['name', 'path_collection'])", I got a path_collection that is a dict type. I known the subfolder is a FolderMini type.

So, when I treat a path_collection, I must distinct the folder type (FolderFull or FolderMini). Or have any good way to treat them no need to judge them?

Thanks.

Versions Used

Python SDK: 1.7.0 Python: 3.10.12

c-rei avatar Nov 05 '24 10:11 c-rei

Hi, the reason for this happening is that FolderMini doesn't have defined path_collection field and therefore when it arrives in the response from get_folder_items method, it is not deserialised to any object, but returned as a dict. We will make our effort to change the type of entries field in Items (https://developer.box.com/reference/resources/items/) to be either Folder or FolderFull. Both of them contain path_collection field. In the meantime I advice you to make another get_folder_by_id call to get path_collection field - that way it will be always an object. Alternatively you can use raw_data field ro access full response as a dict. Best, @lukaszsocha2

lukaszsocha2 avatar Nov 05 '24 11:11 lukaszsocha2

Hi, @lukaszsocha2 Thanks for your response and advise. Because I hove to treat a lot of items, so I want to reduce API calls as much as possible. Then I still need to judge them when I use them.

Best, @c-rei

c-rei avatar Nov 05 '24 12:11 c-rei

Hi @c-rei, did you consider using raw_data as a temporary workaround? There it will be always a dict.

    root_folder: FolderFull = client.folders.get_folder_by_id('0')
    folder_items: Items = client.folders.get_folder_items(root_folder.id, fields=['path_collection'])
    for i, item in enumerate(folder_items.entries):
        print(folder_items.raw_data['entries'][i]['path_collection'])

Best, @lukaszsocha2

lukaszsocha2 avatar Nov 05 '24 13:11 lukaszsocha2

Hi, @lukaszsocha2, Surely, I should use raw_data, because not only path_collection, but also other fields, for example created_at etc. The date type fields all are different (Datetime or ISO string).

Thanks a lot. Best, @c-rei

c-rei avatar Nov 06 '24 01:11 c-rei