#!/usr/bin/env python

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

Now with a way to get details on a specific history.
"""
import os
import sys
import pprint

import setup
import common

# the REST URL for the user resource
RESOURCE_URL = '/api/histories'

# ----------------------------------------------------------------------------- functions
def get_histories():
    """
    Return a list of dictionaries that describe the current user's histories.
    """
    apikey = setup.get_apikey()
    full_url = setup.get_base_url() + RESOURCE_URL
    return common.get( apikey, full_url )


def get_history( history_id ):
    """
    Return a dictionary that the gives details of a
    specific current user's history.
    """
    # same as get_histories here...
    apikey = setup.get_apikey()
    resource_url = setup.get_base_url() + RESOURCE_URL
    # ...but now, we'll add the id of a specific history to the url
    # this will tell galaxy that we want to do a 'show' and get more information than an 'index' will provide
    full_url = resource_url + '/' + history_id
    return common.get( apikey, full_url )


# ----------------------------------------------------------------------------- main
if __name__ == '__main__':
    # here - we'll alter the main function to accept an argument (the module sys is useful here)
    #   BUT! we need to make sure the user sent in the argument on the command line - so, we'll check that first
    # (sys.argv is a list of the arguments the user sent in)
    if len( sys.argv ) <= 1:
        # if they didn't send an argument, we'll print some help
        print 'USAGE: histories_2.py <a history id>'
        # and exit from this script (the 1 indicates to most OS's that there was an error)
        sys.exit( 1 )

    # if we're here, the system didn't exit and we've got an argument, so...
    history_id = sys.argv[1]
    # get the resource data using the function we defined above
    returned = get_history( history_id )
    # pretty print the list of dictionaries that Galaxy sent us in response
    pprint.pprint( returned, indent=2 )
