Others might have responded to this, and I can't remember the very latest approach, but I used the approach below... Its not easy though since api is read via <code> script [BTW, topically, I still need <code> on a regular basis!], but is passed over to command line app securely via a <configfile>. For error situation messaging back to user I was using a dynamic box radio drill_down input. All this code is in toolshed under "versioned_data". Teasing out what you need might require some work though - the code also has a chunk of stuff for maintaining use of an administrator's api key as well as the user's since the app needed to do admin level api work on behalf of a user.
Message: 11 Date: Wed, 27 Jan 2016 12:42:17 +0000 From: Matthias De Smet Matthias.DeSmet@UGent.be ... Hi again
I’m trying to create a tool that interacts with galaxy through the API. For this, I need to get the users API key (or equivalent) en the domain name of the Galaxy host.
The purpose of this tool is to get data and put it in a data library, so I suppose I need to use the API. Or is there another way to do this with a tool?
Thanks! Matthias
<command interpreter="python"> #assert $__user__, Exception( 'You must be logged in to use this tool.' ) versioned_data.py ... -O "$__app__.security.encode_id($log.id)" --api_info_path "$api_info_path" ##Actually a file path to configfile that holds api key </command> <inputs> ... <param name="api_info" display="radio" type="drill_down" label="For user with Galaxy API Key" dynamic_options="vdb_init_tool_user(__trans__)" /> </inputs> <configfiles> <configfile name="api_info_path">${__user__.api_keys[0].key} $api_info </configfile> </configfiles>
<code file="versioned_data_form.py" />
And over in versioned_data_form.py ( there's a reference to "set_trans()" which feeds through to set_user_api() below...
import vdb_retrieval
def vdb_init_tool_user(trans): """ Retrieves a user's api key if they have one, otherwise lets them know they need one This function is automatically called from versioned_data.xml form on presentation to user Note that this is how self.api_url gets back into form, for passage back to 2nd call via versioned_data.py self.api_key is passed via secure <configfile> construct. ALSO: squeezing history_id in this way since no other way to pass it. "trans" is provided only by tool form presentation via <code file="..."> BUT NOW SEE John Chilton's: https://gist.github.com/jmchilton/27c5bb05e155a611294d See galaxy source code at https://galaxy-dist.readthedocs.org/en/latest/_modules/galaxy/web/framework .html, See http://dev.list.galaxyproject.org/error-using-get-user-id-in-xml-file-in-ne w-Galaxy-td4665274.html See http://dev.list.galaxyproject.org/hg-galaxy-2780-Real-Job-tm-support-for-th e-library-upload-to-td4133384.html master api key, set in galaxy config: #self.master_api_key = trans.app.config.master_api_key """ global retrieval_obj
api_url = trans.request.application_url + '/api' history_id = str(trans.security.encode_id(trans.history.id)) user_api_key = None
if trans.user:
user_name = trans.user.username
if trans.user.api_keys and len(trans.user.api_keys) > 0: user_api_key = trans.user.api_keys[0].key #First key is always the active one? items = [ { 'name': user_name, 'value': api_url + '-' + history_id, 'options':[], 'selected': True } ]
else: items = [ { 'name': user_name + ' - Note: you need a key (see "User" menu)!', 'value': '0', 'options':[], 'selected': False } ]
else: items = [ { 'name': 'You need to be logged in to use this tool!', 'value': '1', 'options':[], 'selected': False } ]
retrieval_obj = vdb_retrieval.VDBRetrieval() retrieval_obj.set_trans(api_url, history_id, user_api_key)
return items
And over in tool xml's target code (versioned_data.py) it includes vdb_retrieval.py BUT THIS HAS A LOT OF STUFF YOU PROBABLY DON'T NEED! Sorry!:
from bioblend.galaxy import GalaxyInstance
class VDBRetrieval(object):
def __init__(self, api_key=None, api_url=None): """ This gets either trans.x.y from <code file="..."> call in versioned_data.xml, or it gets a call with api_key and api_url from versioned_data.py @param api_key_path string File path to temporary file containing user's galaxy api_key @param api_url string contains http://%5Bip%5D:%5Bport] for handling galaxy api calls.
""" # Initialized constants during the life of a request: self.global_retrieval_date = None self.library_id = None self.history_id = None self.data_stores = []
# Entire json library structure. item.url, type=file|folder, id, name (library path) # Note: changes to library during a request aren't reflected here. self.library = None
self.user_api_key = None self.user_api = None self.admin_api_key = None self.admin_api = None self.api_url = None
def set_trans(self, api_url, history_id, user_api_key=None): #master_api_key=None, """ Used only on initial presentation of versioned_data.xml form. Doesn't need admin_api """ self.history_id = history_id self.api_url = api_url self.user_api_key = user_api_key
self.set_user_api() self.set_admin_api() self.set_datastores()
... def set_user_api(self): """ Note: error message tacked on to self.data_stores for display back to user. """ self.user_api = GalaxyInstance(url=self.api_url, key=self.user_api_key)
if not self.user_api: self.data_stores.append({'name':'Error: user Galaxy API connection was not set up correctly. Try getting another user API key.', 'id':'none'}) return
galaxy-dev@lists.galaxyproject.org