diff --git a/Algorithmia/client.py b/Algorithmia/client.py index 5afba8e..5a34e06 100644 --- a/Algorithmia/client.py +++ b/Algorithmia/client.py @@ -152,6 +152,13 @@ def getHelper(self, url, **query_parameters): headers['Authorization'] = self.apiKey return self.requestSession.get(self.apiAddress + url, headers=headers, params=query_parameters) + # Used internally to http get a file + def getHelperStream(self, url, **query_parameters): + headers = {} + if self.apiKey is not None: + headers['Authorization'] = self.apiKey + return self.requestSession.get(self.apiAddress + url, headers=headers, params=query_parameters, stream=True) + def patchHelper(self, url, params): headers = {'content-type': 'application/json'} if self.apiKey is not None: diff --git a/Algorithmia/datafile.py b/Algorithmia/datafile.py index 4844599..faebe91 100644 --- a/Algorithmia/datafile.py +++ b/Algorithmia/datafile.py @@ -37,7 +37,7 @@ def getFile(self): if not exists: raise DataApiError('unable to get file {} - {}'.format(self.path, error)) # Make HTTP get request - response = self.client.getHelper(self.url) + response = self.client.getHelperStream(self.url) with tempfile.NamedTemporaryFile(delete = False) as f: for block in response.iter_content(1024): if not block: @@ -46,6 +46,15 @@ def getFile(self): f.flush() return open(f.name) + # Get file iterable from the data api + def getStream(self): + exists, error = self.existsWithError() + if not exists: + raise DataApiError('unable to get file {} - {}'.format(self.path, error)) + # Make HTTP get request + response = self.client.getHelperStream(self.url) + return response.iter_content(1024) + def getName(self): _, name = getParentAndBase(self.path) return name