commit/galaxy-central: greg: Baseline API methods for repositories in the tool shed.
1 new commit in galaxy-central: https://bitbucket.org/galaxy/galaxy-central/commits/73914d7ce261/ changeset: 73914d7ce261 user: greg date: 2013-02-21 20:38:53 summary: Baseline API methods for repositories in the tool shed. affected #: 5 files diff -r c32f70e92aa428dcab3d753ef88a5d255272e4f9 -r 73914d7ce26131b0670f82e3596d2a56494f0480 lib/galaxy/webapps/community/api/repositories.py --- /dev/null +++ b/lib/galaxy/webapps/community/api/repositories.py @@ -0,0 +1,53 @@ +import galaxy.util.shed_util_common as suc +from galaxy import web, util +from galaxy.web.base.controller import BaseAPIController +from galaxy.web.framework.helpers import is_true + +import pkg_resources +pkg_resources.require( "Routes" ) +import routes +import logging + +log = logging.getLogger( __name__ ) + +class RepositoriesController( BaseAPIController ): + """RESTful controller for interactions with repositories in the Tool Shed.""" + @web.expose_api + def index( self, trans, deleted=False, **kwd ): + """ + GET /api/repository_revisions + Displays a collection (list) of repositories. + """ + rval = [] + deleted = util.string_as_bool( deleted ) + try: + query = trans.sa_session.query( trans.app.model.Repository ) \ + .filter( trans.app.model.Repository.table.c.deleted == deleted ) \ + .order_by( trans.app.model.Repository.table.c.name ) \ + .all() + for repository in query: + item = repository.get_api_value( value_mapper={ 'id' : trans.security.encode_id( repository.id ) } ) + item[ 'url' ] = web.url_for( 'repository_contents', repository_id=trans.security.encode_id( repository.id ) ) + rval.append( item ) + except Exception, e: + message = "Error in the Tool Shed API at index: %s" % str( e ) + log.error( message, exc_info=True ) + trans.response.status = 500 + return message + return rval + @web.expose_api + def show( self, trans, id, **kwd ): + """ + GET /api/repositories/{encoded_repository_id} + Displays information about a repository in the Tool Shed. + """ + try: + repository = suc.get_repository_in_tool_shed( trans, id ) + repository_data = repository.get_api_value( view='element', value_mapper={ 'id' : trans.security.encode_id( repository.id ) } ) + repository_data[ 'contents_url' ] = web.url_for( 'repository_contents', repository_id=id ) + except Exception, e: + message = "Error in the Tool Shed API at show: %s" % str( e ) + log.error( message, exc_info=True ) + trans.response.status = 500 + return message + return repository_data \ No newline at end of file diff -r c32f70e92aa428dcab3d753ef88a5d255272e4f9 -r 73914d7ce26131b0670f82e3596d2a56494f0480 lib/galaxy/webapps/community/api/repository_contents.py --- /dev/null +++ b/lib/galaxy/webapps/community/api/repository_contents.py @@ -0,0 +1,32 @@ +import logging +import galaxy.util.shed_util_common as suc +from galaxy import web +from galaxy.web.base.controller import BaseAPIController + +import pkg_resources +pkg_resources.require( "Routes" ) +import routes + +log = logging.getLogger( __name__ ) + +class RepositoryContentsController( BaseAPIController ): + @web.expose_api + def index( self, trans, repository_id, **kwd ): + """ + GET /api/repositories/{encoded_repsository_id}/contents + Displays a collection (list) of repository contents. + + :param repository_id: an encoded id string of the `Repository` to inspect + """ + rval = [] + try: + repository = suc.get_repository_in_tool_shed( trans, repository_id ) + repository_dict = repository.as_dict( trans ) + repository_dict[ 'url' ] = web.url_for( 'repository_contents', repository_id=repository_id ) + rval.append( repository_dict ) + except Exception, e: + message = "Error in repository_contents API: %s" % str( e ) + log.error( message, exc_info=True ) + trans.response.status = 500 + return message + return rval diff -r c32f70e92aa428dcab3d753ef88a5d255272e4f9 -r 73914d7ce26131b0670f82e3596d2a56494f0480 lib/galaxy/webapps/community/api/repository_revisions.py --- a/lib/galaxy/webapps/community/api/repository_revisions.py +++ b/lib/galaxy/webapps/community/api/repository_revisions.py @@ -1,5 +1,6 @@ +import galaxy.util.shed_util_common as suc from galaxy import web, util -from galaxy.web.base.controller import BaseController, BaseAPIController +from galaxy.web.base.controller import BaseAPIController from galaxy.web.framework.helpers import is_true import pkg_resources @@ -10,7 +11,7 @@ log = logging.getLogger( __name__ ) class RepositoryRevisionsController( BaseAPIController ): - """RESTful controller for interactions with tool shed repositories.""" + """RESTful controller for interactions with tool shed repository revisions.""" @web.expose_api def index( self, trans, downloadable=True, **kwd ): """ @@ -25,11 +26,11 @@ .order_by( trans.app.model.RepositoryMetadata.table.c.repository_id ) \ .all() for repository_metadata in query: - item = repository_metadata.get_api_value( value_mapper={ 'id' : trans.security.encode_id } ) + item = repository_metadata.get_api_value( value_mapper={ 'id' : trans.security.encode_id( repository_metadata.id ) } ) item[ 'url' ] = web.url_for( 'repository_revision', id=trans.security.encode_id( repository_metadata.id ) ) rval.append( item ) except Exception, e: - rval = "Error in repository_revisions API" + rval = "Error in repository_revisions API at index: " + str( e ) log.error( rval + ": %s" % str( e ) ) trans.response.status = 500 return rval diff -r c32f70e92aa428dcab3d753ef88a5d255272e4f9 -r 73914d7ce26131b0670f82e3596d2a56494f0480 lib/galaxy/webapps/community/buildapp.py --- a/lib/galaxy/webapps/community/buildapp.py +++ b/lib/galaxy/webapps/community/buildapp.py @@ -69,8 +69,15 @@ webapp.add_route( '/:controller/:action', action='index' ) webapp.add_route( '/:action', controller='repository', action='index' ) webapp.add_route( '/repos/*path_info', controller='hg', action='handle_request', path_info='/' ) - # Add the web API + # Add the web API. webapp.add_api_controllers( 'galaxy.webapps.community.api', app ) + webapp.api_mapper.resource( 'content', + 'contents', + controller='repository_contents', + name_prefix='repository_', + path_prefix='/api/repositories/:repository_id', + parent_resources=dict( member_name='repository', collection_name='repositories' ) ) + webapp.api_mapper.resource( 'repository', 'repositories', path_prefix='/api' ) webapp.api_mapper.resource( 'repository_revision', 'repository_revisions', path_prefix='/api' ) webapp.finalize_config() # Wrap the webapp in some useful middleware diff -r c32f70e92aa428dcab3d753ef88a5d255272e4f9 -r 73914d7ce26131b0670f82e3596d2a56494f0480 lib/galaxy/webapps/community/model/__init__.py --- a/lib/galaxy/webapps/community/model/__init__.py +++ b/lib/galaxy/webapps/community/model/__init__.py @@ -114,14 +114,16 @@ self.prev_session_id = prev_session_id class Repository( object, APIItem ): - api_collection_visible_keys = ( 'id', 'name' ) - api_element_visible_keys = ( 'id', 'name', 'description' ) + api_collection_visible_keys = ( 'id', 'name', 'description', 'user_id', 'private', 'times_downloaded', 'deprecated' ) + api_element_visible_keys = ( 'id', 'name', 'description', 'long_description', 'user_id', 'private', 'times_downloaded', 'deprecated' ) file_states = Bunch( NORMAL = 'n', NEEDS_MERGING = 'm', MARKED_FOR_REMOVAL = 'r', MARKED_FOR_ADDITION = 'a', NOT_TRACKED = '?' ) - def __init__( self, name=None, description=None, long_description=None, user_id=None, private=False, email_alerts=None, times_downloaded=0, deprecated=False ): + def __init__( self, id=None, name=None, description=None, long_description=None, user_id=None, private=False, email_alerts=None, + times_downloaded=0, deprecated=False ): + self.id = id self.name = name or "Unnamed repository" self.description = description self.long_description = long_description @@ -130,6 +132,28 @@ self.email_alerts = email_alerts self.times_downloaded = times_downloaded self.deprecated = deprecated + def as_dict( self, trans ): + repository_dict = self.get_api_value( view='element' ) + repository_dict[ 'id' ] = trans.security.encode_id( self.id ) + return repository_dict + def get_api_value( self, view='collection', value_mapper=None ): + if value_mapper is None: + value_mapper = {} + rval = {} + try: + visible_keys = self.__getattribute__( 'api_' + view + '_visible_keys' ) + except AttributeError: + raise Exception( 'Unknown API view: %s' % view ) + for key in visible_keys: + try: + rval[ key ] = self.__getattribute__( key ) + if key in value_mapper: + rval[ key ] = value_mapper.get( key, rval[ key ] ) + except AttributeError: + rval[ key ] = None + if 'user_id' in rval: + rval[ 'owner' ] = self.user.username + return rval def repo_path( self, app ): return app.hgweb_config_manager.get_entry( os.path.join( "repos", self.user.username, self.name ) ) def revision( self, app ): @@ -194,7 +218,7 @@ try: rval[ key ] = self.__getattribute__( key ) if key in value_mapper: - rval[ key ] = value_mapper.get( key )( rval[ key ] ) + rval[ key ] = value_mapper.get( key, rval[ key ] ) except AttributeError: rval[ key ] = None return rval 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)
-
Bitbucket