V-Spark Online Help

Using the /appedit API with Python

The sample code in this section shows a generic Python application that can be used to call any V‑Spark API. While you will probably want to develop more specialized Python applications to use specific V‑Spark APIs directly, this sample code shows the basic mechanisms that make it easy to interact with the V‑Spark APIs from Python.

The arguments to the sample application shown in the sample code are the following:


The name of the host that is running V‑Spark


The V‑Spark authorization token that you are using to establish permission to retrieve information. You can either use the root token for the target V‑Spark installation (located in the file /opt/voci/state/vspark/apitoken) or the authorization token for the company that is associated with the application that you are working with. Locating a company's authorization token is shown in V‑Spark API Permission Requirements.


The API to which you want to POST the JSON input


The JSON file that contains the data that you want to post. If the JSON file is not in the current directory, you must specify a full or relative path to the file.

Figure 1. Sample Python Application for Sending JSON to APIs via POST
#!/usr/bin/env python

import sys
import json
import requests

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

if ( len(sys.argv) != 5 ):
    print "  Usage:", sys.argv[0], "HOST ROOT_TOKEN API_TO_CALL INPUT_JSON_FILE"

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

print "POSTing data from " + INPUT_JSON_FILE + " to" + API_TO_CALL
with open(INPUT_JSON_FILE) as json_file:
    json_data = json.load(json_file)
headers = {'Content-type': 'application/json'}
response = requests.post(url, headers=headers, data=json.dumps(json_data))
if response.status_code != 200:
    print '  HTTP message: ', response.reason, ' HTTP return code: ', str(response.status_code)

To perform the same activity as the cURL commands in the previous section and reuse the sample JSON files that were shown in Using the /appedit API with cURL, you could call this application (saved as the file post-json.py) three times:

  1. post-json.py hosttoken /config/apps app-create.json

    Calls the /config/apps API to create the application that is specified in the JSON shown in Creating and Populating an Application Using cURL.

  2. post-json.py hosttoken \
        /appedit/Technologies/Technologies-RD/New%20AppEdit%20Test appedit-sample.json

    Calls the /appedit API to upload the configuration for the application /Technologies/Technologies-RD/New%20AppEdit%20Test from the JSON file shown in Creating and Populating an Application Using cURL.

  3. post-json.py hosttoken /config/folders app-folder-associate.json

    Calls the /config/folders API to associate the application with the folder identified in the JSON shown in Creating and Populating an Application Using cURL.