Skip to content

Commit fb3149b

Browse files
author
Will Trimble
committed
Change to mglib to allow return of asynchronous, non-json return data
1 parent 71aaaa3 commit fb3149b

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

mglib/mglib.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def body_from_url(url, accept, auth=None, data=None, debug=False, method=None):
7373
# return python struct from JSON output of MG-RAST or Shock API
7474
def obj_from_url(url, auth=None, data=None, debug=False, method=None):
7575
result = body_from_url(url, 'application/json', auth=auth, data=data, debug=debug, method=method)
76-
obj = json.loads(result.read().decode("utf8"))
76+
if result.headers["content-type"] == "application/x-download":
77+
return(result.read())
78+
else:
79+
obj = json.loads(result.read().decode("utf8"))
7780
if obj is None:
7881
sys.stderr.write("ERROR: return structure not valid json format\n")
7982
sys.exit(1)
@@ -120,10 +123,10 @@ def async_rest_api(url, auth=None, data=None, debug=False, delay=60):
120123
submit = obj_from_url(url, auth=auth, data=data, debug=debug)
121124
# If "status" is nor present, or if "status" is somehow not "submitted"
122125
# assume this is not an asynchronous call and it's done.
123-
if ('status' in submit) and (submit['status'] == 'done') and ('url' in submit):
126+
if ('status' in submit) and (submit['status'] == 'done') and ('data' in submit):
124127
return submit['data']
125-
if not (('status' in submit) and (submit['status'] == 'submitted') and ('url' in submit)):
126-
return submit
128+
# if not (('status' in submit) and (submit['status'] == 'submitted') and ('url' in submit)):
129+
# return submit # No status, no url and no submitted
127130
result = obj_from_url(submit['url'], debug=debug)
128131
try:
129132
while result['status'] != 'done':
@@ -136,6 +139,8 @@ def async_rest_api(url, auth=None, data=None, debug=False, delay=60):
136139
print("Does not contain 'status' field, likely API syntax error", file=sys.stderr)
137140
print(json.dumps(result), file=sys.stderr)
138141
sys.exit(1)
142+
except TypeError: # result isn't json, return it anyway
143+
return(result.decode("utf8"))
139144
if 'ERROR' in result['data']:
140145
sys.stderr.write("ERROR: %s\n" %result['data']['ERROR'])
141146
print(json.dumps(result), file=sys.stderr)

scripts/mg-query.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@
3131
jsonstructure = async_rest_api(URI, auth=key)
3232

3333
# unpack and display the data table
34-
print(json.dumps(jsonstructure))
34+
if type(jsonstructure) == str: # If we have data, not json structure
35+
sys.stdout.write(jsonstructure)
36+
else:
37+
print(json.dumps(jsonstructure), file=sys.stdout)

0 commit comments

Comments
 (0)