cisco-en-programmability / dnacentersdk

Cisco DNA Center Python SDK
https://dnacentersdk.readthedocs.io/en/latest/
MIT License
70 stars 33 forks source link

download file API call returns HTTP object incorrectly #25

Closed nonstdout closed 2 years ago

nonstdout commented 3 years ago

Hi,

I believe the method file.download_a_file_by_fileid(fileid) is not working correctly. It works correctly when the save_file flag is set to True but doesn't return the expected json response when set to default False

I will be submitting a PR that fixes this issue.

Cheers

nonstdout commented 3 years ago

Submitted PR #26

wastorga commented 3 years ago

@nonstdout

Sorry for the delay.

There was an issue with the documentation for download_a_file_by_fileid, the return type is urllib3.response.HTTPResponse rather than MyDict.

The Content-Type of the DNA Center API File is usually application/octet-stream rather than application/json.

namespaces = api.file.get_list_of_available_namespaces().response
namespace = namespaces[0]
files = api.file.get_list_of_files(namespace).response
file_ids = [i.id for i in files]
file_id = file_ids[0]
response1 = api.file.download_a_file_by_fileid(file_id, save_file=False)
print("save_file=False", len(response1.data) > 0, len(response1.data)) # save_file=False True 13550 (for example)
response2 = api.file.download_a_file_by_fileid(file_id, save_file=True)

# save_file=True False 0 # Is zero as it has already read the data and wrote it to a file
print("save_file=True", len(response2.data) > 0, len(response2.data))

# response1.__dict__ # If you still want a dict

Just in case, response1.data are bytes, hopefully, you can convert that to what you need.