Skip to content

Commit 5de99d3

Browse files
author
Will Trimble
committed
Exception handling on mime types
1 parent 901d621 commit 5de99d3

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

mglib/mglib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ def obj_from_url(url, auth=None, data=None, debug=False, method=None):
8080
read = result.read()
8181
if result.headers["content-type"] == "application/x-download":
8282
return(read) # Watch out!
83+
if result.headers["content-type"][0:9] == "text/html": # json decoder won't work
84+
return(read) # Watch out!
85+
if result.headers["content-type"] == "application/json": # If header is set, this should work
86+
data = read.decode("utf8")
87+
obj = json.loads(data)
8388
else:
8489
data = read.decode("utf8")
8590
obj = json.loads(data)
@@ -129,6 +134,11 @@ def async_rest_api(url, auth=None, data=None, debug=False, delay=60):
129134
submit = obj_from_url(url, auth=auth, data=data, debug=debug)
130135
# If "status" is nor present, or if "status" is somehow not "submitted"
131136
# assume this is not an asynchronous call and it's done.
137+
if type(submit) == bytes: # can't decode
138+
try:
139+
return decode("utf-8", submit)
140+
except:
141+
return submit
132142
if ('status' in submit) and (submit['status'] != 'submitted') and (submit['status'] != "processing") and ('data' in submit):
133143
return submit
134144
if not ('url' in submit.keys()):

0 commit comments

Comments
 (0)