Skip to content

Commit ae1e101

Browse files
Merge pull request #14 from sdominInterfax/master
Resolves issues with handling of large files.
2 parents 5aebf5d + b44a5f6 commit ae1e101

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

interfax/client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class InterFAX(object):
2525
USER_AGENT = 'InterFAX Python {0}'.format(__version__)
2626
DOMAIN = 'rest.interfax.net'
2727

28-
def __init__(self, username=None, password=None, timeout=None):
28+
def __init__(self, username=None, password=None):
2929
username = username or environ.get('INTERFAX_USERNAME', None)
3030
password = password or environ.get('INTERFAX_PASSWORD', None)
3131

@@ -39,7 +39,7 @@ def __init__(self, username=None, password=None, timeout=None):
3939

4040
self.username = username
4141
self.password = password
42-
self.timeout = timeout
42+
print("Authentication: done")
4343

4444
@cached_property
4545
def inbound(self):
@@ -83,7 +83,6 @@ def delete(self, path, **kwargs):
8383

8484
def _request(self, method, url, **kwargs):
8585
"""Make a HTTP request."""
86-
kwargs.setdefault('timeout', self.timeout)
8786
kwargs.setdefault('headers', {})
8887
kwargs['headers']['User-Agent'] = self.USER_AGENT
8988
kwargs['auth'] = (self.username, self.password)
@@ -108,12 +107,17 @@ def _parse_response(self, response):
108107
"""Parse a response object and return the url, json, or binary
109108
content."""
110109
if response.ok:
110+
print(response)
111111
if 'location' in response.headers:
112112
return response.headers['location']
113113
else:
114114
try:
115-
return response.json()
115+
r = response.json()
116+
return r
116117
except:
117-
return response.content
118+
r = response.content
119+
return r
118120
else:
121+
print(response)
122+
print('FAILED!')
119123
response.raise_for_status()

interfax/documents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def create(self, name, size, **kwargs):
2929
kwargs['size'] = size
3030

3131
valid_keys = ['name', 'size', 'disposition', 'shared']
32-
32+
print("Creating document upload session..")
3333
uri = self.client.post('/outbound/documents', kwargs, valid_keys)
3434

3535
return Document(self.client, {'uri': uri})

interfax/files.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,28 @@ def _init_path(self, data):
5454

5555
def _init_binary(self, data, mime_type):
5656
"""Initialise with binary data."""
57+
self.mime_type = mime_type
58+
self.body = data
59+
5760
if len(data) > self.chunk_size:
5861
return self._init_document(data, mime_type)
5962

60-
self.mime_type = mime_type
61-
self.body = data
63+
6264

6365
def _init_document(self, data, mime_type):
6466
"""Upload the data using the documents API."""
6567
filename = 'upload-{0}{1}'.format(uuid4(), guess_extension(mime_type))
6668
document = self.client.documents.create(filename, len(data))
6769

6870
cursor = 0
71+
counter = 1
6972

7073
while cursor < len(data):
7174
chunk = data[cursor:cursor + self.chunk_size]
72-
75+
print('SENDING CHUNK {}..'.format(counter))
7376
document.upload(cursor, cursor + len(chunk) - 1, chunk)
7477
cursor += len(chunk)
78+
counter += 1
7579

7680
self._init_url(document.uri)
7781

interfax/outbound.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Outbound(object):
66

77
def __init__(self, client):
88
self.client = client
9+
self.headers = {} ##
10+
911

1012
def deliver(self, fax_number, files, **kwargs):
1113
"""Submit a fax to a single destination number."""
@@ -16,8 +18,20 @@ def deliver(self, fax_number, files, **kwargs):
1618

1719
kwargs['fax_number'] = fax_number
1820

19-
result = self.client.post('/outbound/faxes', kwargs, valid_keys,
20-
files=self._generate_files(files))
21+
data = None
22+
binaryfile = None
23+
24+
for f in files: # checking if the file supplied is an URL or binary data
25+
if f.startswith('http://') or f.startswith('https://'):
26+
self.headers['Content-Location'] = f
27+
data = self._generate_files(files)
28+
else:
29+
binaryfile = self._generate_files(files)
30+
31+
32+
print('DELIVERING...')
33+
result = self.client.post('/outbound/faxes', kwargs, valid_keys, data=data, files=binaryfile,
34+
headers=self.headers) ## PARAMS: 'data' for URI, 'files' for binary data, with either one supllied the other stays empty
2135

2236
return OutboundFax(self.client, {'id': result.split('/')[-1]})
2337

@@ -47,15 +61,13 @@ def completed(self, *args):
4761
(Submitted id's which have not completed are ignored).
4862
4963
"""
50-
valid_keys = ['ids']
51-
args_str = ""
52-
for idx, arg in enumerate(args):
53-
if idx == len(args) - 1:
54-
args_str += str(arg)
55-
else:
56-
args_str += str(arg) + ", "
57-
kwargs = {'ids': args_str}
58-
faxes = self.client.get('/outbound/faxes/completed', kwargs, valid_keys)
64+
valid_keys = ['ids']
65+
66+
kwargs = {'ids': args}
67+
68+
faxes = self.client.get('/outbound/faxes/completed', kwargs,
69+
valid_keys)
70+
5971
return [OutboundFax(self.client, fax) for fax in faxes]
6072

6173
def find(self, message_id):

0 commit comments

Comments
 (0)