Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.
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
8 changes: 5 additions & 3 deletions hellosign_sdk/hsclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,16 @@ def get_signature_request_list(self, page=1, ux_version=None):

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

def get_signature_request_file(self, signature_request_id, filename, file_type=None):
def get_signature_request_file(self, signature_request_id, path_or_file=None, file_type=None, filename=None):
''' Download the PDF copy of the current documents

Args:

signature_request_id (str): Id of the signature request

filename (str): Filename to save the PDF file to. This should be a full path.
path_or_file (str or file): A writable File-like object or a full path to save the PDF file to.

filename (str): [DEPRECATED] Filename to save the PDF file to. This should be a full path.

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.

Expand All @@ -333,7 +335,7 @@ def get_signature_request_file(self, signature_request_id, filename, file_type=N
url = self.SIGNATURE_REQUEST_DOWNLOAD_PDF_URL + signature_request_id
if file_type:
url += '?file_type=%s' % file_type
return request.get_file(url, filename)
return request.get_file(url, path_or_file or filename)

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):
''' Creates and sends a new SignatureRequest with the submitted documents
Expand Down
21 changes: 17 additions & 4 deletions hellosign_sdk/tests/unit_tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from hellosign_sdk.utils import HSRequest, BadRequest
import tempfile
import os
import StringIO

#
# The MIT License (MIT)
Expand Down Expand Up @@ -92,15 +93,21 @@ def test_get_file(self):
f.close()
response = request.get_file(url='http://httpbin.org/robots.txt',
headers={'Custom-Header': 'Nothing'},
filename=temp_filename)
path_or_file=temp_filename)
os.unlink(temp_filename)
self.assertEquals(response, True)

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

out = StringIO.StringIO()
response = request.get_file(url='http://httpbin.org/robots.txt',
headers={'Custom-Header': 'Nothing'},
path_or_file=out)
self.assertEquals(response, True)

def test_get_file_https(self):
request = HSRequest(self.client.auth)
f = tempfile.NamedTemporaryFile(delete=True)
Expand All @@ -109,11 +116,17 @@ def test_get_file_https(self):

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

response = request.get_file(url='https://httpbin.org/robots.txt',
headers={'Custom-Header': 'Nothing'},
filename='')
path_or_file='')
self.assertEquals(response, False)

out = StringIO.StringIO()
response = request.get_file(url='http://httpbin.org/robots.txt',
headers={'Custom-Header': 'Nothing'},
path_or_file=out)
self.assertEquals(response, True)
22 changes: 14 additions & 8 deletions hellosign_sdk/utils/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class HSRequest(object):

DEFAULT_ENCODING = "UTF-8"
USER_AGENT = "hellosign-python-sdk"

parameters = None
http_status_code = 0
verify_ssl = True
Expand All @@ -66,13 +66,15 @@ def get_warnings(self):
if self.warnings and len(self.warnings) > 0:
return self.warnings

def get_file(self, url, filename, headers=None):
def get_file(self, url, path_or_file=None, headers=None, filename=None):
''' Get a file from a url and save it as `filename`

Args:
url (str): URL to send the request to

filename (str): File name to save the file as, this can be either
path_or_file (str or file): A writable File-like object or a path to save the file to.

filename (str): [DEPRECATED] File name to save the file as, this can be either
a full path or a relative path

headers (str, optional): custom headers
Expand All @@ -82,6 +84,7 @@ def get_file(self, url, filename, headers=None):
otherwise.

'''
path_or_file = path_or_file or filename

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

response = requests.get(url, headers=self.headers, auth=self.auth, verify=self.verify_ssl)

self.http_status_code = response.status_code
try:
# No need to check for warnings here
self._check_error(response)
fd = os.open(filename, os.O_CREAT | os.O_RDWR)
with os.fdopen(fd, "w+b") as f:
f.write(response.content)
try:
path_or_file.write(response.content)
except AttributeError:
fd = os.open(path_or_file, os.O_CREAT | os.O_RDWR)
with os.fdopen(fd, "w+b") as f:
f.write(response.content)
except:
return False

return True

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