commit/galaxy-central: carlfeberhard: Tools API: better error handling in create, encode ids returned; Scripts: add history_upload script to api
1 new commit in galaxy-central: https://bitbucket.org/galaxy/galaxy-central/commits/cc25fdb717eb/ Changeset: cc25fdb717eb User: carlfeberhard Date: 2013-05-29 23:28:47 Summary: Tools API: better error handling in create, encode ids returned; Scripts: add history_upload script to api Affected #: 2 files diff -r 93f6e07db8549411be10ba5385ba4cf9fab0c046 -r cc25fdb717ebde12121948a318bb9d141eb6710a lib/galaxy/webapps/galaxy/api/tools.py --- a/lib/galaxy/webapps/galaxy/api/tools.py +++ b/lib/galaxy/webapps/galaxy/api/tools.py @@ -66,9 +66,9 @@ # -- Execute tool. -- # Get tool. - tool_id = payload[ 'tool_id' ] - tool = trans.app.toolbox.get_tool( tool_id ) + tool = trans.app.toolbox.get_tool( payload[ 'tool_id' ] ) if 'tool_id' in payload else None if not tool: + trans.response.status = 404 return { "message": { "type": "error", "text" : trans.app.model.Dataset.conversion_messages.NO_TOOL } } # Set running history from payload parameters. @@ -82,7 +82,7 @@ target_history = None # Set up inputs. - inputs = payload[ 'inputs' ] + inputs = payload.get( 'inputs', {} ) # Find files coming in as multipart file data and add to inputs. for k, v in payload.iteritems(): if k.startswith("files_"): @@ -91,17 +91,23 @@ # HACK: add run button so that tool.handle_input will run tool. inputs['runtool_btn'] = 'Execute' # TODO: encode data ids and decode ids. + # TODO: handle dbkeys params = util.Params( inputs, sanitize = False ) - template, vars = tool.handle_input( trans, params.__dict__, history=target_history) + template, vars = tool.handle_input( trans, params.__dict__, history=target_history ) + if 'errors' in vars: + trans.response.status = 400 + return { "message": { "type": "error", "data" : vars[ 'errors' ] } } # TODO: check for errors and ensure that output dataset(s) are available. - output_datasets = vars.get('out_data', {}).values() + output_datasets = vars.get( 'out_data', {} ).values() rval = { "outputs": [] } outputs = rval[ "outputs" ] + #TODO:?? poss. only return ids? for output in output_datasets: - outputs.append( output.get_api_value() ) + output_dict = output.get_api_value() + outputs.append( trans.security.encode_dict_ids( output_dict ) ) return rval # diff -r 93f6e07db8549411be10ba5385ba4cf9fab0c046 -r cc25fdb717ebde12121948a318bb9d141eb6710a scripts/api/history_upload.py --- /dev/null +++ b/scripts/api/history_upload.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +""" +Upload a file to the desired history. +""" +_USAGE = ( "history_upload.py <api key><galaxy base url><history id><filepath to upload>\n" + + " (where galaxy base url is just the root url where your Galaxy is served; e.g. 'localhost:8080')" ) + +import os, sys, json, pprint +#sys.path.insert( 0, os.path.dirname( __file__ ) ) + +try: + import requests +except ImportError, imp_err: + log.error( "Could not import the requests module. See http://docs.python-requests.org/en/latest/ or " + + "install with 'pip install requests'" ) + raise + +def upload_file( full_url, api_key, history_id, filepath, **kwargs ): + + payload = { + 'key' : api_key, + 'tool_id' : 'upload1', + 'history_id' : history_id, + } + inputs = { + 'files_0|NAME' : kwargs.get( 'filename', os.path.basename( filepath ) ), + 'files_0|type' : 'upload_dataset', + #TODO: the following doesn't work with tools.py + #'dbkey' : kwargs.get( 'dbkey', '?' ), + 'dbkey' : '?', + 'file_type' : kwargs.get( 'file_type', 'auto' ), + 'ajax_upload' : u'true', + 'async_datasets': '1', + } + payload[ 'inputs' ] = json.dumps( inputs ) + + response = None + with open( filepath, 'rb' ) as file_to_upload: + files = { 'files_0|file_data' : file_to_upload } + response = requests.post( full_url, data=payload, files=files ) + return response + +if __name__ == '__main__': + + if len( sys.argv ) < 5: + print _USAGE + sys.exit( 1 ) + + api_key, base_url, history_id, filepath = sys.argv[1:5] + full_url = base_url + '/api/tools' + kwargs = dict([ kwarg.split('=', 1) for kwarg in sys.argv[5:]]) + + response = upload_file( full_url, api_key, history_id, filepath, **kwargs ) + print >> sys.stderr, response + print response.content Repository URL: https://bitbucket.org/galaxy/galaxy-central/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email.
participants (1)
-
commits-noreply@bitbucket.org