#!/usr/bin/env python

"""
Access a Galaxy instance and get data for HistoryDatasetAssociations.

Adding a function to get details on a specific HDA.
"""
import os
import sys
import pprint

import setup
import common

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 )

def get_hda( history_id, hda_id ):
    """
    Return a dictionary of detailed information for a specific HDA.
    :param history_id: the id of the history which we want to query.
    :param hda_id: the id of the HDA which we want to query.
    """
    apikey = setup.get_apikey()
    container_url = setup.get_base_url() + RESOURCE_URL

    full_url = container_url + '/' + history_id + '/contents/' + hda_id
    return common.get( apikey, full_url )


# ----------------------------------------------------------------------------- main
if __name__ == '__main__':
    if len( sys.argv ) <= 2:
        print 'USAGE: hdas_1.py <history id> <hda_id>'
        sys.exit( 1 )

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