V-Spark Online Help

Integrating Multiple GET Results Using Python

To read data from a V‑Spark installation, you can use the root token, but that's analogous to running every Linux command as the superuser. It is cleaner to use the authorization token that is associated with each company.

This section provides example code that enables you to explore a V‑Spark installation by using the top-level JSON configuration data for that installation, along with the JSON that describes all of its folders. You could use the sample code shown in Using GET with Python and the /config API to extract the JSON information that you need by executing the following two commands, assuming that the code has been saved to the file config-get-info.py.

Once you have retrieved the JSON for the companies and folders that have been defined in the V‑Spark installation on the host example.company.com, you can combine these two aspects of JSON configuration information about your V‑Spark installation to explore that installation, as shown in the following sample Python code.

Figure 1. Sample Python code to combine data read from 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.
#
# Application that reads company and folder information about a
# product installation on a specified host, then uses the company's
# short name to link the two. The application then prints a
# hierarchical listing of available companies (each with its
# associated authorization token), the organizations within those
# companies, and the folders within those organizations.
#

import requests

def usage(argv):
    print "Usage:", argv[0], "<sparkhost:port> <root token>"
    exit(1)

def main(argv): 1
    if len(argv) != 3: usage(argv)
    host, token = argv[1:]
    tokens = gettokens(host,token)
    folderinfo = getfolderinfo(host,token)
    printfolders(host, folderinfo, tokens)

def gettokens(host, token): 2
    url = "http://%s/config?token=%s" % (host,token)
    cfg = requests.get(url).json()
    return dict([(comp,d['uuid']) for comp,d in cfg.iteritems()])

def getfolderinfo(host, token): 3
    url = "http://%s/config/folders?token=%s" % (host,token)
    return requests.get(url).json()

def printfolders(host, folder_info, tokens):
    for comp, comp_data in folder_info.iteritems(): 4
        print comp+" (Token: "+tokens[comp]+")"
        for org, org_data in comp_data.iteritems(): 5
            print "\t", org
            for folder, folder_data in org_data.iteritems(): 6
                print "\t\t", folder

if __name__ == '__main__':
    from sys import argv
    main(argv)


The sample code shown previously does the following:

1

The main function provides a traditional main routine that shows the order in which functions are called in the application

2

Uses the /config API to retrieve the top-level configuration information from the host that was specified on the command line , and returns a dictionary that contains only the company names and their associated authorization tokens (stored in the uuid field of the per-company information).

3

Uses the /config/folders API to retrieve the folder-level configuration information from the host that was specified on the command line

4

Initiates the primary loop for the application, which is controlled by the companies that were found in the information that was retrieved from the host specified on the command line. Each company has an associated authorization token (originally stored in the uuid name/value pair), which is the other field for each company entry in the dictionary that was constructed in the gettokens()function. The short name for each company is the data item in the company JSON that provides the linkage between the data from the company and folder sources. This loop prints out the name of each company that was found on the remote V‑Spark installation and its associated authorization token.

5

Initiates a second loop for the application, which is controlled by the organizations that were retrieved in the company information which was retrieved from the host specified on the command line. This loop prints the name of each organization that was found under the current company.

6

Initiates the final internal loop that iterates through all of the folders that are associated with each organization that was retrieved in the company information which was retrieved from the host specified on the command line. This loop prints the name of each folder found under the current organization.

After the final print entry in the example code, you could expand this sample application for testing purposes by adding other API calls that would require company, authorization token, organization, and folder information.

The sample application shown previously shows how you can programmatically integrate the JSON output that you receive regarding different aspects of a V‑Spark installation. As noted in the comments at the beginning of the source code, this code is only provided as an example.