Skip to content

Commit

Permalink
Support file-like objects instead of explicitly requiring files. Fixe…
Browse files Browse the repository at this point in the history
…s #10.
  • Loading branch information
paulosman committed Nov 21, 2012
1 parent 2bde77f commit a3bec3e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions soundcloud/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def encode_multipart_formdata(fields, boundary=None):
requests.models.encode_multipart_formdata = encode_multipart_formdata


def is_file_like(f):
"""Check to see if ```f``` has a ```read()``` method."""
return hasattr(f, 'read') and callable(f.read)


def extract_files_from_dict(d):
"""Return any file objects from the provided dict.
Expand All @@ -83,7 +88,7 @@ def extract_files_from_dict(d):
for key, value in d.iteritems():
if isinstance(value, dict):
files[key] = extract_files_from_dict(value)
elif isinstance(value, file):
elif is_file_like(value):
files[key] = value
return files

Expand All @@ -104,7 +109,7 @@ def remove_files_from_dict(d):
for key, value in d.iteritems():
if isinstance(value, dict):
file_free[key] = remove_files_from_dict(value)
elif not isinstance(value, file):
elif not is_file_like(value):
file_free[key] = str(value)
return file_free

Expand Down

0 comments on commit a3bec3e

Please sign in to comment.