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.
#!/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:
The | |
Uses the | |
Uses the | |
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 | |
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. | |
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.