From 26bad374e751edf4813cbc8a8244611cd507c896 Mon Sep 17 00:00:00 2001 From: Arsh Singh Date: Fri, 4 Dec 2015 13:27:08 +0530 Subject: [PATCH] Accept a file-like object for writing the signature req file to (fix #12) for HSClient.get_signature_request_file & HSRequest.get_file: - deprecated the `filename` parameter. - introduced a new `path_or_file` parameter which accepts with a path to a file or a file-like object. --- hellosign_sdk/hsclient.py | 8 ++++--- .../tests/unit_tests/test_request.py | 21 ++++++++++++++---- hellosign_sdk/utils/request.py | 22 ++++++++++++------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/hellosign_sdk/hsclient.py b/hellosign_sdk/hsclient.py index 57806de..c2eb603 100644 --- a/hellosign_sdk/hsclient.py +++ b/hellosign_sdk/hsclient.py @@ -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. @@ -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 diff --git a/hellosign_sdk/tests/unit_tests/test_request.py b/hellosign_sdk/tests/unit_tests/test_request.py index d24c0ef..7caef00 100644 --- a/hellosign_sdk/tests/unit_tests/test_request.py +++ b/hellosign_sdk/tests/unit_tests/test_request.py @@ -4,6 +4,7 @@ from hellosign_sdk.utils import HSRequest, BadRequest import tempfile import os +import StringIO # # The MIT License (MIT) @@ -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) @@ -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) diff --git a/hellosign_sdk/utils/request.py b/hellosign_sdk/utils/request.py index 61985d6..517ce8f 100644 --- a/hellosign_sdk/utils/request.py +++ b/hellosign_sdk/utils/request.py @@ -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 @@ -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 @@ -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)) @@ -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):