Skip to content

Allow the user to control the download #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions smartfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ def _do_request(self, request, url, **kwargs):
return response.text
else:
# This might be a file, so return it.
return response.raw
if kwargs.get('params', {}).get('raw', True):
return response.raw
else:
return response

def _request(self, method, endpoint, id=None, **kwargs):
"Handles retrying failed requests and error handling."
Expand Down Expand Up @@ -145,14 +148,19 @@ def upload(self, filename, fileobj):
arg = (filename, fileobj)
return self.post('/path/data/', file=arg)

def download(self, file_to_be_downloaded):
def download(self, file_to_be_downloaded, perform_download=True):
""" file_to_be_downloaded is a file-like object that has already
been uploaded, you cannot download folders """
response = self.get(
'/path/data/', file_to_be_downloaded, raw=False)
if not perform_download:
# The caller can decide how to process the download of the data
return response

# download uses shutil.copyfileobj to download, which copies
# the data in chunks
o = open(file_to_be_downloaded, 'wb')
return shutil.copyfileobj(self.get('/path/data/',
file_to_be_downloaded), o)
return shutil.copyfileobj(response.raw, o)

def move(self, src_path, dst_path):
# check destination folder for / at end
Expand Down
3 changes: 3 additions & 0 deletions test/test_smartfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import

import os
import requests
import unittest

try:
Expand Down Expand Up @@ -47,6 +48,8 @@ def upload(self):
self.assertEquals(self.get_data()['size'], f.tell())

def download(self):
response = self.api.download(TESTFN, False)
self.assertTrue(isinstance(response, requests.Response))
self.api.download(TESTFN)
self.assertEquals(self.get_data()['size'], os.path.getsize(TESTFN))

Expand Down