V-Spark Online Help

Using POST with Python and the /config API

Use the /config API to update the configuration of a V‑Spark installation.

Note

The /config/system API does not accept POST requests. At this time, the /config/system API only returns system read only settings and read only settings must be updated directly using the /config/system/readonly API.

This section provides sample Python code that shows how to put information from a sample JSON file to the V‑Spark installation on the host that you provide as a command-line argument. You can prepare this JSON file manually, or you can use Python code like that shown in Using GET with Python and the /config API to retrieve V‑Spark configuration from a V‑Spark installation on another host.

Figure 1. Sample Python code to write (POST) data with 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 requests

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

if ( len(sys.argv) != 6 ):  1
    print "  Usage:", sys.argv[0], "HOST ROOT_TOKEN API_TO_CALL TARGET_HTTP_CODE INPOST_JSON_FILE"
    sys.exit(-1)
else:
    HOST, ROOT_TOKEN, API_TO_CALL, TARGET_HTTP_CODE, INPOST_JSON_FILE = sys.argv[1:]

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

print "Checking " + API_TO_CALL + ", POSTing input from " + INPOST_JSON_FILE
print "  Whole URL: "+url

with open(INPOST_JSON_FILE) as json_file: 3
    json_data = json.load(json_file)

response = requests.put(url, data=json.dumps(json_data)) 4

print '  SUMMARY (POST): ' + API_TO_CALL, ': HTTP message: ', response.reason, ' HTTP return code: ', str(response.status_code), ' expected ' + TARGET_HTTP_CODE
    


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 put data to a specified host. The major steps in this sample Python application are the following:

1

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.

2

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

3

Open the JSON file that was specified on the command line (and which contains that data that you want to POST to the specified host). Read that JSON into a JSON object.

4

Call the specified URL, passing the JSON object as a parameter. Next, print a summary that identifies the API that the program called, the HTTP message and return code that the call returned, and prints the expected return code that you provided as a parameter.

Some examples of calling this Python script from the command line are the following:

  • config-put-tests.py example.company.com 123456789012345678901234567890123 /config/orgs 200 \
        config-orgs.json

    Enables you to write the organization-level JSON configuration information stored in the file config-org.json to the V‑Spark installation of the host example.company.com using the root token 123456789012345678901234567890123, and also says that you expect that command to return success (HTTP error code 200).

  • config-put-tests.py example.company.com 123456789012345678901234567890123 /config 200 \
        config.json

    Enables you to write the company-level (top-level) JSON configuration information stored in the file config.json to the V‑Spark installation of the host example.company.com using the root token 123456789012345678901234567890123, and also says that you expect that command to return success's (HTTP error code 200).