#!/usr/bin/env python

"""
Access a Galaxy instance and get data for HistoryDatasetAssociations.
"""
import os
import sys
import pprint

import setup
import common

# note: that this is the same resource url as that for histories
# since histories contain HDAs, (the starting part of) the url remains the same
#   In each of the functions, we'll add the suffix 'contents' to access the HDA info
#   e.g. /api/histories/< the id of the history we want info from >/contents
RESOURCE_URL = '/api/histories'

# ----------------------------------------------------------------------------- functions
def get_hdas( history_id ):
    """
    Return a list of dictionaries that describe the HDAs *in* a *specifc* history.
    :param history_id: the id of the history which we want to query.
    """
    # similar users.get_users and histories.get_histories, but
    apikey = setup.get_apikey()
    container_url = setup.get_base_url() + RESOURCE_URL

    # ...note that we'll add the id of a specific history to 'look inside of'
    #   and the keyword 'contents'
    full_url = container_url + '/' + history_id + '/contents'
    return common.get( apikey, full_url )


# ----------------------------------------------------------------------------- main
if __name__ == '__main__':
    # again, using an argument - this time, a history's id
    if len( sys.argv ) <= 1:
        print 'USAGE: hdas_1.py <history id>'
        sys.exit( 1 )

    history_id = sys.argv[1]
    returned = get_hdas( history_id )
    pprint.pprint( returned, indent=2 )
