paint-brush
How to Fetch SAP Business Objects Universes Using Pythonby@luca1iu
1,552 reads
1,552 reads

How to Fetch SAP Business Objects Universes Using Python

by Luca LiuJanuary 12th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

With the RESTful API, developers can perform operations like fetching information about reports, universes, folders, scheduling, and other BI-related entities.
featured image - How to Fetch SAP Business Objects Universes Using Python
Luca Liu HackerNoon profile picture

The SAP BusinessObjects RESTful Web Service (RESTful API) is an application programming interface provided by SAP BusinessObjects. It allows developers to interact with SAP BusinessObjects BI (Business Intelligence) platform functionalities through HTTP requests. This RESTful API enables the integration of SAP BusinessObjects services into custom applications, facilitating tasks such as retrieving, creating, updating, and deleting BI content, as well as managing user permissions and security.


With the RESTful API, developers can perform operations like fetching information about reports, universes, folders, scheduling, and other BI-related entities. This approach promotes interoperability and simplifies the integration of SAP BusinessObjects BI capabilities into various applications and workflows.

How to Fetch universes using Python?

Part 1: Authentication

To initiate the authentication process, please replace the placeholder values for username, password, and localhost with your specific configuration details.

import requests
import pandas as pd
import xml.etree.ElementTree as ET

# define the login request parameters
user_name = 'username'
password = 'password'
localhost = 'localhost'

auth_type = 'secEnterprise'
login_url = 'http://{}:6405/biprws/logon/long'.format(localhost)
login_data = f'<attrs xmlns="http://www.sap.com/rws/bip"><attr name="userName" type="string">{user_name}</attr><attr name="password" type="string">{password}</attr><attr name="auth" type="string" possibilities="secEnterprise,secLDAP,secWinAD,secSAPR3">{auth_type}</attr></attrs>'
login_headers = {'Content-Type': 'application/xml'}

# send the login request and retrieve the response
login_response = requests.post(login_url, headers=login_headers, data=login_data)
# parse the XML response and retrieve the logonToken
root = ET.fromstring(login_response.text)
logon_token = root.find('.//{http://www.sap.com/rws/bip}attr[@name="logonToken"]').text
api_headers = {'Content-Type': 'application/xml', 'X-SAP-LogonToken': logon_token}

Part 2: Data Retrieval and DataFrame Creation

Previewing Retrieved Data: First Universe's Name

This Python snippet fetches all the information about universes from the server. If you run the code, it will print the name of the first universe.

response = requests.get("http://{}:6405/biprws/raylight/v1/universes/".format(localhost),
                        headers=api_headers)
root = ET.fromstring(response.text)
print(root.findall('universe')[0][2].tag, ":", root.findall('universe')[0][2].text)

Data Transformation Functions: Transform to DataFrame

The Python functions, get_dataframe_from_response, and get_all_dataframe, work together to simplify SAP BusinessObjects data retrieval. The first function transforms XML data into a structured pandas DataFrame, capturing document attributes. The second function efficiently handles scenarios with universes exceeding a single request's limit by appending multiple DataFrames. Collectively, these functions streamline the conversion of XML to DataFrame and provide an easy solution for handling a large number of universes.

def get_dataframe_from_response(response):
    # Parse the XML data
    root = ET.fromstring(response.text)
    # Extract the data into a list of dictionaries
    res = []
    for item in root.findall('universe'):
        doc_dict = {}
        for elem in item.iter():
            if elem.text is not None:
                doc_dict[elem.tag] = elem.text
        res.append(doc_dict)
    # Convert the list of dictionaries to a pandas dataframe
    df = pd.DataFrame(res)
    return df

def get_all_dataframe(url):
    documents = []
    for i in range(50):
        offset = i * 50
        url_offset = url + "?offset={}&limit=50".format(offset)
        response = requests.get(url_offset, headers=api_headers)
        df = get_dataframe_from_response(response=response)
        if df.empty:
            break
        else:
            documents.append(df)
    dataframe = pd.concat(documents, axis=0)
    return dataframe

Retrieve detailed information about SAP BusinessObjects universes effortlessly using a single line of Python code. Utilize the get_all_dataframe function, and the resulting df_universes DataFrame provides a straightforward overview of universes attributes.

df_universes = get_all_dataframe(url="http://{}:6405/biprws/sl/v1/universes".format(localhost))

Showcasing df_universes: What follows is a glimpse into the dataframe structure

dataframe structure


Part 3: Universe Details Extraction

If you need additional details such as the universe's path and connected status, utilize the following function. This function fetches the specified details for each universe in the df_universes DataFrame, providing a more comprehensive overview of each entry.

def get_universe_detail(universeID, detail):
    url = 'http://{}:6405/biprws/raylight/v1/universes/{}'.format(localhost, universeID)
    res = requests.get(url, headers={
        "Accept": "application/json",
        "Content-Type": "application/json",
        "X-SAP-LogonToken": logon_token
    }).json()
    return res['universe'][detail]

def get_more_information_from_universes(df):
    details = ['path', 'connected']
    for detail in details:
        df[detail] = [get_universe_detail(id, detail) for id in df['id'].values]
    return df

df_universes_more_info = get_more_information_from_universes(df_universes)

Thank you for taking the time to explore data-related insights with me. I appreciate your engagement. If you find this information helpful, I invite you to follow me or connect with me on LinkedIn. Happy exploring!👋


Also published here.