Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.

Commit 3411556

Browse files
committed
Merge pull request #13 from arshsingh/v3
Accept a file-like object for writing the signature req file to (fix …
2 parents 169236d + 26bad37 commit 3411556

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

hellosign_sdk/hsclient.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,16 @@ def get_signature_request_list(self, page=1, ux_version=None):
314314

315315
return request.get(self.SIGNATURE_REQUEST_LIST_URL, parameters=parameters)
316316

317-
def get_signature_request_file(self, signature_request_id, filename, file_type=None):
317+
def get_signature_request_file(self, signature_request_id, path_or_file=None, file_type=None, filename=None):
318318
''' Download the PDF copy of the current documents
319319
320320
Args:
321321
322322
signature_request_id (str): Id of the signature request
323323
324-
filename (str): Filename to save the PDF file to. This should be a full path.
324+
path_or_file (str or file): A writable File-like object or a full path to save the PDF file to.
325+
326+
filename (str): [DEPRECATED] Filename to save the PDF file to. This should be a full path.
325327
326328
file_type (str): Type of file to return. Either "pdf" for a single merged document or "zip" for a collection of individual documents. Defaults to "pdf" if not specified.
327329
@@ -333,7 +335,7 @@ def get_signature_request_file(self, signature_request_id, filename, file_type=N
333335
url = self.SIGNATURE_REQUEST_DOWNLOAD_PDF_URL + signature_request_id
334336
if file_type:
335337
url += '?file_type=%s' % file_type
336-
return request.get_file(url, filename)
338+
return request.get_file(url, path_or_file or filename)
337339

338340
def send_signature_request(self, test_mode=False, files=None, file_urls=None, title=None, subject=None, message=None, signing_redirect_url=None, signers=None, cc_email_addresses=None, form_fields_per_document=None, use_text_tags=False, hide_text_tags=False, metadata=None, ux_version=None):
339341
''' Creates and sends a new SignatureRequest with the submitted documents

hellosign_sdk/tests/unit_tests/test_request.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from hellosign_sdk.utils import HSRequest, BadRequest
55
import tempfile
66
import os
7+
import StringIO
78

89
#
910
# The MIT License (MIT)
@@ -92,15 +93,21 @@ def test_get_file(self):
9293
f.close()
9394
response = request.get_file(url='http://httpbin.org/robots.txt',
9495
headers={'Custom-Header': 'Nothing'},
95-
filename=temp_filename)
96+
path_or_file=temp_filename)
9697
os.unlink(temp_filename)
9798
self.assertEquals(response, True)
9899

99100
response = request.get_file(url='http://httpbin.org/robots.txt',
100101
headers={'Custom-Header': 'Nothing'},
101-
filename='')
102+
path_or_file='')
102103
self.assertEquals(response, False)
103104

105+
out = StringIO.StringIO()
106+
response = request.get_file(url='http://httpbin.org/robots.txt',
107+
headers={'Custom-Header': 'Nothing'},
108+
path_or_file=out)
109+
self.assertEquals(response, True)
110+
104111
def test_get_file_https(self):
105112
request = HSRequest(self.client.auth)
106113
f = tempfile.NamedTemporaryFile(delete=True)
@@ -109,11 +116,17 @@ def test_get_file_https(self):
109116

110117
response = request.get_file(url='https://httpbin.org/robots.txt',
111118
headers={'Custom-Header': 'Nothing'},
112-
filename=temp_filename)
119+
path_or_file=temp_filename)
113120
os.unlink(temp_filename)
114121
self.assertEquals(response, True)
115122

116123
response = request.get_file(url='https://httpbin.org/robots.txt',
117124
headers={'Custom-Header': 'Nothing'},
118-
filename='')
125+
path_or_file='')
119126
self.assertEquals(response, False)
127+
128+
out = StringIO.StringIO()
129+
response = request.get_file(url='http://httpbin.org/robots.txt',
130+
headers={'Custom-Header': 'Nothing'},
131+
path_or_file=out)
132+
self.assertEquals(response, True)

hellosign_sdk/utils/request.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class HSRequest(object):
4747

4848
DEFAULT_ENCODING = "UTF-8"
4949
USER_AGENT = "hellosign-python-sdk"
50-
50+
5151
parameters = None
5252
http_status_code = 0
5353
verify_ssl = True
@@ -66,13 +66,15 @@ def get_warnings(self):
6666
if self.warnings and len(self.warnings) > 0:
6767
return self.warnings
6868

69-
def get_file(self, url, filename, headers=None):
69+
def get_file(self, url, path_or_file=None, headers=None, filename=None):
7070
''' Get a file from a url and save it as `filename`
7171
7272
Args:
7373
url (str): URL to send the request to
7474
75-
filename (str): File name to save the file as, this can be either
75+
path_or_file (str or file): A writable File-like object or a path to save the file to.
76+
77+
filename (str): [DEPRECATED] File name to save the file as, this can be either
7678
a full path or a relative path
7779
7880
headers (str, optional): custom headers
@@ -82,6 +84,7 @@ def get_file(self, url, filename, headers=None):
8284
otherwise.
8385
8486
'''
87+
path_or_file = path_or_file or filename
8588

8689
if self.debug:
8790
print("GET FILE: %s, headers=%s" % (url, headers))
@@ -91,17 +94,20 @@ def get_file(self, url, filename, headers=None):
9194
self.headers.update(headers)
9295

9396
response = requests.get(url, headers=self.headers, auth=self.auth, verify=self.verify_ssl)
94-
97+
9598
self.http_status_code = response.status_code
9699
try:
97100
# No need to check for warnings here
98101
self._check_error(response)
99-
fd = os.open(filename, os.O_CREAT | os.O_RDWR)
100-
with os.fdopen(fd, "w+b") as f:
101-
f.write(response.content)
102+
try:
103+
path_or_file.write(response.content)
104+
except AttributeError:
105+
fd = os.open(path_or_file, os.O_CREAT | os.O_RDWR)
106+
with os.fdopen(fd, "w+b") as f:
107+
f.write(response.content)
102108
except:
103109
return False
104-
110+
105111
return True
106112

107113
def get(self, url, headers=None, parameters=None, get_json=True):

0 commit comments

Comments
 (0)