POST entity configuration

Use api_post_config.py to test POST requests to the /config endpoint and to verify the expected HTTP return code.

Figure 1. api_post_config.py
#!/usr/bin/env python

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

import sys
import json
import requests

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

if ( len(sys.argv) != 6 ):  
    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 
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: 
    json_data = json.load(json_file)

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

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

api_post_config.py makes a POST request with the input JSON to create or configure the specified entity, and it compares expected and actual HTTP return codes in a console output message.

Use this script with the following command and arguments:

python api_post_config.py $host $token $endpoint $http_code $input_file
$host
Hostname or URL for the system running V‑Spark.
$token

Authorization token with the required read or write permissions for the request.

$endpoint
Request endpoint, including any path parameters.
$http_code
The HTTP code expected to be returned with the response.
$input_file
File path for query input.