From 469d06fc795ed3323c92556154835ba37b59fe95 Mon Sep 17 00:00:00 2001 From: sdominInterfax <44236637+sdominInterfax@users.noreply.github.com> Date: Wed, 16 Oct 2019 12:17:31 +0100 Subject: [PATCH 1/4] Update client.py --- interfax/client.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/interfax/client.py b/interfax/client.py index e558df9..feac1e4 100644 --- a/interfax/client.py +++ b/interfax/client.py @@ -25,7 +25,7 @@ class InterFAX(object): USER_AGENT = 'InterFAX Python {0}'.format(__version__) DOMAIN = 'rest.interfax.net' - def __init__(self, username=None, password=None, timeout=None): + def __init__(self, username=None, password=None): username = username or environ.get('INTERFAX_USERNAME', None) password = password or environ.get('INTERFAX_PASSWORD', None) @@ -39,7 +39,7 @@ def __init__(self, username=None, password=None, timeout=None): self.username = username self.password = password - self.timeout = timeout + print("Authentication: done") @cached_property def inbound(self): @@ -83,7 +83,6 @@ def delete(self, path, **kwargs): def _request(self, method, url, **kwargs): """Make a HTTP request.""" - kwargs.setdefault('timeout', self.timeout) kwargs.setdefault('headers', {}) kwargs['headers']['User-Agent'] = self.USER_AGENT kwargs['auth'] = (self.username, self.password) @@ -108,12 +107,17 @@ def _parse_response(self, response): """Parse a response object and return the url, json, or binary content.""" if response.ok: + print(response) if 'location' in response.headers: return response.headers['location'] else: try: - return response.json() + r = response.json() + return r except: - return response.content + r = response.content + return r else: + print(response) + print('FAILED!') response.raise_for_status() From abc6bd47a5efdd18509c724ac2e5600a056e6d8a Mon Sep 17 00:00:00 2001 From: sdominInterfax <44236637+sdominInterfax@users.noreply.github.com> Date: Wed, 16 Oct 2019 12:18:29 +0100 Subject: [PATCH 2/4] Update documents.py --- interfax/documents.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfax/documents.py b/interfax/documents.py index a3f3745..df1a030 100644 --- a/interfax/documents.py +++ b/interfax/documents.py @@ -29,7 +29,7 @@ def create(self, name, size, **kwargs): kwargs['size'] = size valid_keys = ['name', 'size', 'disposition', 'shared'] - + print("Creating document upload session..") uri = self.client.post('/outbound/documents', kwargs, valid_keys) return Document(self.client, {'uri': uri}) From e41ec7973cf5d3df4117522456e4bb420d4e8681 Mon Sep 17 00:00:00 2001 From: sdominInterfax <44236637+sdominInterfax@users.noreply.github.com> Date: Wed, 16 Oct 2019 12:19:26 +0100 Subject: [PATCH 3/4] Update files.py --- interfax/files.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/interfax/files.py b/interfax/files.py index cdbc1bd..a34a2b7 100644 --- a/interfax/files.py +++ b/interfax/files.py @@ -54,11 +54,13 @@ def _init_path(self, data): def _init_binary(self, data, mime_type): """Initialise with binary data.""" + self.mime_type = mime_type + self.body = data + if len(data) > self.chunk_size: return self._init_document(data, mime_type) - self.mime_type = mime_type - self.body = data + def _init_document(self, data, mime_type): """Upload the data using the documents API.""" @@ -66,12 +68,14 @@ def _init_document(self, data, mime_type): document = self.client.documents.create(filename, len(data)) cursor = 0 + counter = 1 while cursor < len(data): chunk = data[cursor:cursor + self.chunk_size] - + print('SENDING CHUNK {}..'.format(counter)) document.upload(cursor, cursor + len(chunk) - 1, chunk) cursor += len(chunk) + counter += 1 self._init_url(document.uri) From b44a5f6fc4d021954ddabca28bb0d9632d07dfd9 Mon Sep 17 00:00:00 2001 From: sdominInterfax <44236637+sdominInterfax@users.noreply.github.com> Date: Wed, 16 Oct 2019 12:20:27 +0100 Subject: [PATCH 4/4] Update outbound.py --- interfax/outbound.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/interfax/outbound.py b/interfax/outbound.py index 25aeb48..fe00ea0 100644 --- a/interfax/outbound.py +++ b/interfax/outbound.py @@ -6,6 +6,8 @@ class Outbound(object): def __init__(self, client): self.client = client + self.headers = {} ## + def deliver(self, fax_number, files, **kwargs): """Submit a fax to a single destination number.""" @@ -16,8 +18,20 @@ def deliver(self, fax_number, files, **kwargs): kwargs['fax_number'] = fax_number - result = self.client.post('/outbound/faxes', kwargs, valid_keys, - files=self._generate_files(files)) + data = None + binaryfile = None + + for f in files: # checking if the file supplied is an URL or binary data + if f.startswith('http://') or f.startswith('https://'): + self.headers['Content-Location'] = f + data = self._generate_files(files) + else: + binaryfile = self._generate_files(files) + + + print('DELIVERING...') + result = self.client.post('/outbound/faxes', kwargs, valid_keys, data=data, files=binaryfile, + headers=self.headers) ## PARAMS: 'data' for URI, 'files' for binary data, with either one supllied the other stays empty return OutboundFax(self.client, {'id': result.split('/')[-1]}) @@ -47,15 +61,13 @@ def completed(self, *args): (Submitted id's which have not completed are ignored). """ - valid_keys = ['ids'] - args_str = "" - for idx, arg in enumerate(args): - if idx == len(args) - 1: - args_str += str(arg) - else: - args_str += str(arg) + ", " - kwargs = {'ids': args_str} - faxes = self.client.get('/outbound/faxes/completed', kwargs, valid_keys) + valid_keys = ['ids'] + + kwargs = {'ids': args} + + faxes = self.client.get('/outbound/faxes/completed', kwargs, + valid_keys) + return [OutboundFax(self.client, fax) for fax in faxes] def find(self, message_id):