OceanNetworksCanada / api-python-client

Provides easy access to ONC data in Python
https://oceannetworkscanada.github.io/api-python-client/
Apache License 2.0
10 stars 9 forks source link

fix: check whether file exists before downloading the file by using stream parameter #43

Closed kan-fu closed 1 month ago

kan-fu commented 2 months ago

This PR fixes a bug that the client library should not download the file if the file exists and overwrite=False. Previosly, it downloads the file first, then checks if the file exists, which wastes bandwidth.

By using stream=True parameter, the requests method can postpone the download. saveAsFile will handle the actual stream download, and also return the download time and file size.

Initially I tried using HEAD first to avoid the download. Turns out it requires a bigger code change (download the file in status=200 branch). I also met some difficulties like HEAD does not have a body, so status=202 cannot use response.json(). Using stream can keep the current code logic unchanged.

The test failure can be ignored. It is a backend bug, which will be fixed in the backend later.