Using SpectX API from Python

Sample scripts: scripts.tar.gz

Here’s demo of using SpectX API from Python script. NB! Change the sx_api_access_key value according to your API access key. You’ll find it in User Properties dialog. Also spectx_host:port must be set to correct values.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import requests
import json

query_1 = """
    dual(100)
     .select(t as time, ip, i, array)
    """

if __name__ == "__main__":
    sx_api_uri = "https://[spectx_host]:[port]/API/v1.0/?scriptPath=/system/"
    sx_api_access_key =  "xxxxxxxxxxxxxxxx"   # get from User properties dialog

    headers = {
        "Authorization: Bearer ": sx_api_access_key,
        "Content-Type": "application/json; charset=UTF-8",
        "Accept": "application/json"
    }

    http_resp = requests.post(sx_api_uri, headers=headers, data=query_1)

    if http_resp.status_code != 200:
        print "Server returned HTTP %i" % http_resp.status_code
        exit()

    resp = http_resp.json()
    if resp.get('error') is not None:
        print "Query error:"
        print resp.get('error')
        exit()

    warnings = resp.get('warnings')
    if warnings is not None:
        print "Query had %d warnings" % len(warnings)
        for warning in warnings[:10]:
            print warning

    rows = resp.get('rows')
    print "Result %d rows" % len(rows)

    for row in rows[:10]:
        print row