TheHive-Project / TheHive4py

Python API Client for TheHive
MIT License
218 stars 144 forks source link

Add the possibility to download files and keep them in a variable #322

Closed TytoCapensis closed 2 weeks ago

TytoCapensis commented 6 months ago

As today, all functions related to download of files take a file path as an argument, and download themselves the file on the filesystem (by using the functions in the session.py module)

This can cause issues because:

I intend to add a pull request that:

Kamforka commented 6 months ago

Hi TytoCapensis!

Very nice suggestion! My original idea was to overload the download_path variable so it can be a pathlike parameter or an IO buffer.

I think that would be more elegant and probably the main modification should be done solely in _process_stream_response.

There one could check if download_path is a pathlike object or a buffer. In case it's a buffer then it should just write the chunks to it.

Can you give it a try?

TytoCapensis commented 6 months ago

I just pushed a replacement commit that went with the method you suggested instead (using a io.BytesIO object passed in the argument). I still have to modify some files however, to change the type hinting definitions.

Note that I had to use a # type: ignore in session.py, because mypy got confused and was reporting that io.BytesIO was not a correct type for open() function (yet it is in another if block). If you have a better idea to go around that, I will change the code.

Kamforka commented 2 weeks ago

I close this one as it is not a feature we want to support at the moment. A possible workaround with temporary files/buffers: https://github.com/TheHive-Project/TheHive4py/pull/323#issuecomment-2092748709