Skip to content

Commit

Permalink
CTX-4070: Made is so streaming download is only used for samples and …
Browse files Browse the repository at this point in the history
…implemented checking for downloaded file corruption (if file size does not match expected size)
  • Loading branch information
Vuk Manojlovic committed Jul 11, 2023
1 parent 0c4d070 commit 620fac0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
8 changes: 3 additions & 5 deletions coretex/coretex/sample/network_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,10 @@ def download(self, ignoreCache: bool = False) -> bool:
bool -> False if response is failed, True otherwise
"""

if os.path.exists(self.zipPath) and not ignoreCache:
return True

response = networkManager.genericDownload(
response = networkManager.sampleDownload(
endpoint = f"{self.__class__._endpoint()}/export?id={self.id}",
destination = self.zipPath
destination = self.zipPath,
ignoreCache = ignoreCache
)

return not response.hasFailed()
Expand Down
68 changes: 67 additions & 1 deletion coretex/networking/network_manager_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,85 @@ def genericDownload(

headers = self._requestHeader()

if parameters is None:
parameters = {}

response = self._requestManager.get(endpoint, headers, jsonObject = parameters)

if self.shouldRetry(retryCount, response):
print(">> [Coretex] Retry count: {0}".format(retryCount))
return self.genericDownload(endpoint, destination, parameters, retryCount + 1)

if response.raw.ok:
destinationPath = Path(destination)
if destinationPath.is_dir():
raise RuntimeError(">> [Coretex] Destination is a directory not a file")

if destinationPath.exists():
destinationPath.unlink(missing_ok = True)

destinationPath.parent.mkdir(parents = True, exist_ok = True)

with open(destination, "wb") as downloadedFile:
downloadedFile.write(response.raw.content)

return response

def sampleDownload(
self,
endpoint: str,
destination: str,
ignoreCache: bool,
parameters: Optional[Dict[str, Any]] = None,
retryCount: int = 0
) -> NetworkResponse:
"""
Downloads file to the given destination
Parameters
----------
endpoint : str
API endpoint
destination : str
path to save file
parameters : Optional[dict[str, Any]]
request parameters (not required)
retryCount : int
number of function calls if request has failed, used
for internal retry mechanism
Returns
-------
NetworkResponse as response content to request
Example
-------
>>> from coretex import networkManager
\b
>>> response = networkManager.genericDownload(
endpoint = "dummyObject/download",
destination = "path/to/destination/folder"
)
>>> if response.hasFailed():
print("Failed to download the file")
"""

headers = self._requestHeader()

if parameters is None:
parameters = {}

networkResponse = self._requestManager.streamingDownload(
endpoint,
Path(destination),
ignoreCache,
headers,
parameters
)

if self.shouldRetry(retryCount, networkResponse):
print(">> [Coretex] Retry count: {0}".format(retryCount))
return self.genericDownload(endpoint, destination, parameters, retryCount + 1)
return self.sampleDownload(endpoint, destination, parameters, retryCount + 1)

return networkResponse

Expand Down
4 changes: 4 additions & 0 deletions coretex/networking/requests_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def streamingDownload(
self,
endpoint: str,
destinationPath: Path,
ignoreCache: bool,
headers: Dict[str, str],
parameters: Dict[str, Any]
) -> NetworkResponse:
Expand All @@ -225,6 +226,9 @@ def streamingDownload(
raise RuntimeError(">> [Coretex] Destination is a directory not a file")

if destinationPath.exists():
if int(response.headers["Content-Length"]) == destinationPath.stat().st_size and not ignoreCache:
return NetworkResponse(response, endpoint)

destinationPath.unlink(missing_ok = True)

destinationPath.parent.mkdir(parents = True, exist_ok = True)
Expand Down

0 comments on commit 620fac0

Please sign in to comment.