Skip to Content
Author's profile photo Stéphane DUPONT

Scripting Web Intelligence : the RESTful Raylight Web Services with Python (sample)

Well, I followed Ted Ueda‘s blog posts about the RESTful Raylight Web Services and I translated his Powershell export sample to Python.

See the blog post Scripting Web Intelligence:  the RESTful Raylight Web Services for more information.

Why Python and not Powershell ?

  • Python is a cross-platform language ; you can use it on linux or unix and not only on windows ;
  • Python’s community is larger ;
  • Python is broader ; you can use it for tasks ranging from systems automation, testing, and ETL to gaming, CGI and web development.

and of course I use more Python than Powershell 😉

The original powershell script export.ps1 is here. This is the Python 3 version :

export.py

#.Synopsis
#Exports WebI to PDF.
#.Description
#Logs onto BI Platform, retrieves PDF export of a Web Intelligence document and save to specified file.
#Modify this script to enter the logon credentials, URL to the RESTful Web Services, Language, Document SI_ID and folder path.
#
import urllib.request
import urllib.parse
import json
import os
############################################################################################################################
# Input: logonInfo, hostUrl, locale, documentId and folderPath to suite your preferences
logonInfo = {
    'userName'    : 'administrator',
    'password'    : 'Password1',
    'auth'        : 'secEnterprise'
}
hostUrl = 'http://10.160.206.89:6405/biprws'
documentId = '7827'       # SI_ID for the document
locale = 'en-US'          # Format language for the WebI exporter
contentLocale = 'en-US'   # Format language for the WebI document contents
folderPath = r'C:\Users\I819039\Desktop\RESTful'  # Folder where PDF file will be saved.
############################################################################################################################
raylightUrl = hostUrl + '/raylight/v1'
documentUrl = raylightUrl + '/documents/' + documentId
# Logon and retrieve the logon token to be used in subsequent RESTful calls.
headers = {
    'Content-Type'    : 'application/json',
    'Accept'          : 'application/json',
}
d=str.encode(json.dumps(logonInfo))
result = urllib.request.urlopen(urllib.request.Request(hostUrl + "/logon/long",d,headers))
reponse=json.loads(result.read().decode('utf-8'))
logonToken=reponse['logonToken']
# Get Web Intelligence document information.
headers = {
    'X-SAP-LogonToken'  : '"'+logonToken+'"' ,
    'Accept'            : 'application/json',
    'Content-Type'      : 'application/json',
    'Accept-Language'   : locale,
    'X-SAP-PVL'         : contentLocale
}
result = urllib.request.urlopen( urllib.request.Request(documentUrl,None,headers) )
reponse=json.loads(result.read().decode('utf-8'))
document = reponse['document']
# Refresh the document by sending empty prompts (assumes document has no prompts).
headers = {
    'X-SAP-LogonToken' : '"'+logonToken+'"' ,
    'Accept'           : 'application/json' ,
    'Content-Type'     : 'application/json' ,
    'X-SAP-PVL'        : contentLocale
}
parametersUrl = documentUrl + '/parameters'
urllib.request.urlopen( urllib.request.Request(parametersUrl,None,headers) )
# Retrieve and save PDF first ensuring the file path is valid.
filePath = os.path.join(folderPath , document['name'] + '.pdf')
if( os.access(os.path.dirname(filePath), os.W_OK) ) :
    # Get PDF and save to file
    headers = {
        'X-SAP-LogonToken' : '"'+logonToken+'"' ,
        'Accept'           : 'application/pdf' ,
        'X-SAP-PVL'        : contentLocale
    }
    result = urllib.request.urlopen( urllib.request.Request(documentUrl + '/pages',None,headers) )
    f = open(filePath, 'wb')
    f.write(result.read())
    f.close()
else :
    print ('Invalid file path ' + filePath)
# Unload document from Raylight.
headers = {
    'X-SAP-LogonToken' : '"'+logonToken+'"' ,
    'Accept'           : 'application/json' ,
    'Content-Type'     : 'application/json' ,
    'X-SAP-PVL'        : contentLocale
}
data = {
  'document' : { 'state' : 'Unused' }
}  
d=str.encode(json.dumps(data))
urllib.request.urlopen(urllib.request.Request(documentUrl,d,headers))
# Log off the Session identified by the X-SAP-LogonToken HTTP Header
headers = {
    'X-SAP-LogonToken' : '"'+logonToken+'"' ,
    'Accept'           : 'application/json' ,
    'Content-Type'     : 'application/json'
}
urllib.request.urlopen(urllib.request.Request(hostUrl + '/logoff',b'',headers))

Tested with Portable Python 3.2.5.1 on Windows XP and BI4.1 SP3.

It’s not compatible with Python 2. Syntax and Modules are different.

PS : Sorry for the misspellings … I skipped some English class hours at school 😕

Assigned Tags

      9 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Anthony MULLER
      Anthony MULLER

      Wonderful 🙂

      Author's profile photo Stéphane DUPONT
      Stéphane DUPONT
      Blog Post Author

      Thanks 😎

      Author's profile photo Ludek Uher
      Ludek Uher

      Excellent stuff 🙂

      I just Tweeted this from https://twitter.com/SAPCRNetSup

      Many thanks for the contribution!

      - Ludek

      Senior Support Engineer AGS Product Support, Global Support Center Canada

      Author's profile photo Former Member
      Former Member

      arrgh awesome but i use python 2.7 mainly. Maybe i should take time to translate it into 2.7

      Author's profile photo Former Member
      Former Member

      Consider using the Requests library instead of urllib2, it will drastically cut the amount of code you need to write to perform HTTP requests and is considerably easier to use.

      Author's profile photo Stéphane DUPONT
      Stéphane DUPONT
      Blog Post Author

      Interesting 😀

      I didn't know this external library.

      And it seems working in Python 2 and 3 !

      Author's profile photo Former Member
      Former Member

      Hi all,

      Great work !

      Someone did the same in php?

      Regards,

      Sassoun

      Author's profile photo Former Member
      Former Member

      Is it possible to get and save to file list of documents etc?

      Author's profile photo Michael Gaynor
      Michael Gaynor

      This is great.  Are there any sites where people share similar code?  I'm new to Python and having base code to build off and/or reverse engineer is extremely helpful.  My ultimate goal is to be able to extract the details of universes and Webi (including query details).  Our vendor has frequent data model changes/additions and we'd like to simplify the impact analysis process.