Skip to content

Commit

Permalink
Merge pull request #14 from sdominInterfax/master
Browse files Browse the repository at this point in the history
Resolves issues with handling of large files.
  • Loading branch information
ricky-shake-n-bake-bobby authored Nov 20, 2019
2 parents 5aebf5d + b44a5f6 commit ae1e101
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
14 changes: 9 additions & 5 deletions interfax/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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()
2 changes: 1 addition & 1 deletion interfax/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
10 changes: 7 additions & 3 deletions interfax/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,28 @@ 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."""
filename = 'upload-{0}{1}'.format(uuid4(), guess_extension(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)

Expand Down
34 changes: 23 additions & 11 deletions interfax/outbound.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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]})

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit ae1e101

Please sign in to comment.