GET request for multiple endpoints

Use api_get_test.py to make a GET request to certain V‑Spark endpoints.

Figure 1. api_get_test.py
#!/usr/bin/env python
# Unsupported example code - Not for production use.

import sys
import json
import urllib2
import string

# default values
PROTOCOL =  "http://"
PORT =  "3000"

if ( len(sys.argv) != 5 ):
    print "  Usage:", sys.argv[0], "HOST ROOT_TOKEN API_TO_CALL PARAMS"
    sys.exit(-1)
else:
    # get cmdline params
    HOST, ROOT_TOKEN, API_TO_CALL, PARAMS = sys.argv[1:] 

# Define the URL in a single variable for JSON load 
url =  "%s%s:%s%s?token=%s&%s" % (PROTOCOL, HOST, PORT, API_TO_CALL, ROOT_TOKEN, PARAMS)

# To get output data, return a Python object and dump it to a string that is a
#   JSON representation of that object. Complain and exit if the call fails.
try:   
    data = json.load(urllib2.urlopen(url))
except urllib2.HTTPError, error:
    print '  Error: HTTP message: ', error.msg, ' HTTP return code: ', str(error.code)
    sys.exit(-1)

# Sanitize URL and params for use in creating output file name
tmp_str = string.replace( 
    string.replace(
        string.replace(
            string.replace(API_TO_CALL+"_"+PARAMS+".json", '/', '_'), '&', '_'), '?', '_'), '%20', '_')
target = open(tmp_str[1:], 'w')

target.write(json.dumps(data, indent=4, sort_keys=True)) 
target.close()
print "  Output written to: "+tmp_str[1:]

Use api_get_test.py with the following command and arguments:

python api_get_test.py $host $token $endpoint '$options'
$host
Hostname or URL for the system running V‑Spark.
$token

Authorization token with the required read or write permissions for the request.

$endpoint
Request endpoint, including any path parameters.
$options
Optional parameters used to further refine the request and results.