REST API Python Interface
Altair Accelerator software includes a Python module that provides an interface to the REST API.
import os,sys,requests, json
scriptPath = os.path.dirname(os.path.abspath(__file__))
sys.path.append(scriptPath)
from vov_rest_v3 import *
# You must use the web port for the base URL for V3 REST
nc_url = 'https://hexagon:9101'
password = "mysecret"
# Connect and get a valid JWT token using auth
conn = VOVRestV3()
conn.authorize( nc_url, password )
# Create a job
scriptPath = os.path.dirname(os.path.abspath(__file__))
logPath = os.path.join(scriptPath, "joblogs")
os.makedirs(logPath, 0o700, True)
logFilePath = os.path.join(logPath, f'job_{str(os.getpid())}.log')
print(f'\nJob log path = {logFilePath}')
jobPayload = {
"command": "sleep 1",
"env": "BASE",
"resources": "linux64",
"group": "/time/users",
"preemptable": 1,
"jpp": "fastest",
"fstokens": 1,
"rundir": ".",
"logfile": logFilePath
}
res = conn.submitRequest(method="POST", url=nc_url + "/api/v3/jobs", jsonData=jobPayload)
jobid = json.loads(res)["jobid"]
print(f'\nNew Job ID = {jobid}')
# Query jobs
query = {
"select": "id,statusnc,name",
"from": "jobs",
"order": "ID desc"
}
res = conn.submitRequest(method="GET", url=nc_url + "/api/v3/query", queryParams=query)
rowlist = json.loads(res)["rows"]
print('\nThe job list is:')
for row in rowlist:
print(f'{row}')
- Python 3
- The requests module (can be installed via easy_install: sudo python3 -m easy_install requests).
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
Job log path = /home/myuser/pyclient/joblogs/job_28107.log
New Job ID = 11480
The job list is:
['000011480', 'Running', 'vw sleep 1 > /home/myuser/pyclient/joblogs/job_28107.log']
Job log path = /home/myuser/pyclient/joblogs/job_28107.log
New Job ID = 11480
The job list is:
['000011480', 'Running', 'vw sleep 1 > /home/myuser/pyclient/joblogs/job_28107.log']
The V3 Python interface requires authentication, which you pass in as part of call to authorize(). The V3 REST API uses an OAuth2 JWT token which is passed back in response to a valid username and password. The V3 Python interface keeps this JWT on the object and uses it for subsequent submitRequest() calls. If JWT token is good for 8 hours.
If you’d like to persist the JWT token for use in multiple Python scripts, you can call getJWT() to get the token string, and call setJWT() instead of authorize() to use the JWT in another VOVRestV3 instance.