A better script, a tastier beer?
import requests
import json
from StringIO import StringIO
import copy
#Script information, CONFIGURE
reportXid = "Your Report XID" #Report XID to add points to
removeExistingPoints = False #Remove the points that are already on the report
addDataSources = ["DS_1234"] #list of data source xids to add
additionalRQL = "limit(-1)" #use a high limit if not H2, or whatever limit is desired
#Server information, CONFIGURE
host = "192.168.1.111"
port = "80"
login = {"username": "admin", "password": "admin"}
#Begin script....
hostPort = host + ":" + port
s = requests.Session()
s.headers.update({'Cookie':'MANGO'+port+'=initial; XSRF-TOKEN=init','X-Xsrf-Token':'init','Accept': 'application/json', 'Accept-Encoding': 'gzip', 'Content-Type': 'application/json;charset=UTF-8'});
r = s.post('http://' + hostPort + "/rest/v2/login", data=json.dumps(login))
#del s.headers["Content-Type"]
del s.headers["Cookie"]
s.headers["X-Xsrf-Token"] = s.cookies["XSRF-TOKEN"]
#get the report's JSON:
reportsResponse = s.get('http://' + hostPort + "/rest/v2/json-emport?exportElements=reports")
#print reportsResponse.text
reportsList = json.load( StringIO( reportsResponse.text ))["reports"]
report = None
for rpt in reportsList :
if rpt["xid"] == reportXid :
report = rpt
break
if report is None :
print "Couldn't find report with xid: " + reportXid
exit()
if removeExistingPoints :
report.points = []
baseReportPoint = {
"pointXid":"",
"colour":"",
"consolidatedChart":True,
"plotType":1,
"pointKey":"p",
"weight":1.0
}
if len(addDataSources) > 0 :
dataPointsRql = "or("
for dsXid in addDataSources :
dataPointsRql += "eq(dataSourceXid," + dsXid + "),"
dataPointsRql = dataPointsRql[:-1] + ")&" + additionalRQL
else :
dataPointsRql = additionalRQL
#print dataPointsRql
def pointInReport(reportPoints, point) :
for pnt in reportPoints :
if pnt["pointXid"] == point["xid"] :
return True
return False
pointsResponse = s.get("http://" + hostPort + "/rest/v2/data-points?" + dataPointsRql)
#print pointsResponse.text
pointsList = json.load( StringIO( pointsResponse.text ))["items"]
for pnt in pointsList :
#You may wish to configure the report points here as well
if not pointInReport(report["points"], pnt) :
newReportPoint = copy.deepcopy(baseReportPoint)
newReportPoint["pointXid"] = pnt["xid"]
newReportPoint["pointKey"] += str(pnt["id"])
report["points"].append(newReportPoint)
#print "Added point: " + pnt["xid"]
#Save the modified report through the json-emport
saveResponse = s.post("http://" + hostPort + "/rest/v2/json-emport", data=json.dumps({"reports":[report]}))
print "Saving report returned status: ", saveResponse
You probably need to change my print statements if you're using Python 3.
Edit: Fixed up the multiple data source possibility some