V-Spark Online Help

Using GET with Python and the /config API

The sample code below shows example Python code that takes multiple parameters, and which enables you to retrieve information from a specified portion of the /config API. This code takes the following parameters, in order:


The hostname of IP address of the host that is running the V‑Spark installation which you want to query


The root token for the V‑Spark installation that you are querying. The root token for a V‑Spark installation is stored in the file /opt/voci/state/vspark/apitoken


You can use the example code to call any aspect of the /config API. For example, you could pass /config, /config/orgs, and so on, or explicitly request information about a specific company by passing /config/DocTestCo (if DocTestCo is a valid company on the V‑Spark installation that you are querying).


This is the HTTP return code that you expect to receive, and is only used in this example so that you can display its value and visually compare it against what you received from the actual API call to determine if your API call worked correctly. You could expand your code to react appropriately if you received an HTTP return code other than this one.


The file to which you want to write the JSON that you retrieved. The sample code also pretty-prints this JSON, to make it easier to read.

Figure 1. Sample Python code to query the /config API
#!/usr/bin/env python

# Copyright 2017 Voci Technologies All rights reserved.
# Contains confidential company information.
# Unsupported example code - Not for production use.

import sys
import json
import urllib2
import requests

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

    if ( len(sys.argv) != 6 ): 1
        print "  Usage:", sys.argv[0], "HOST API_ROOT_TOKEN API_TO_CALL HTTP_CODE_TARGET OUTPOST_FILE"
        # get cmdline params

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

print "Checking " + API_TO_CALL + " on " + HOST + " and writing output to " + OUTPOST_FILE
print "  URL is " + url

response = requests.get(url) 3
print '  SUMMARY (GET): ' + API_TO_CALL, ': HTTP message: ', \
         response.reason, ' HTTP return code: ', \
         str(response.status_code), ' expected ' + HTTP_CODE_TARGET

target = open(OUTPOST_FILE, 'w')

# To get output data, return a python object and dump it to a string
#   that is a JSON representation of that object
data = json.load(urllib2.urlopen(url))  4

# pretty-print the result
target.write(json.dumps(data, indent=4, sort_keys=True))


The sample code shown previously is one of the applications that are used to help test the /config API that is discussed in this section. Therefore, its focus is on providing simple, linear code that shows how to get data from a specified host. The major steps in this sample Python application are the following:


Check if the right number of command-line arguments have been provided, assign them to appropriate variables if so and identifying the expected arguments if not.


Assemble the URL that you will use to call the specified API


Make the get request to the V‑Spark installation on the host that you specified on the command-line so that you can get the HTTP response and return code to display in an output message


Issue the get call in another fashion so that you can retrieve the JSON that it returns and pretty-print that to a file.