sap bo restful web service ile universe objelerini dosyaya çıkarma

Sap Business Objects RestFul Web Service Sdk kullanılarak universe (platform) objelerinin Python ile json formatında dosyaya çıkarılması. (Export universe object list to json file with Python)

Dosya olarak indir: export_universe_objects-py


#!/usr/bin/python
# -*- coding: utf-8 -*-



import requests, json

baseurl = 'http://hostname_ip:port/biprws' # change hostname ip port

headerJson = {'accept': 'application/json'}

r = requests.get(baseurl+'/logon/long', headers=headerJson)

headerJson2 = {'accept': 'application/json', 'content-type': 'application/json'}

postDataJson = '{"userName":"Administrator","password":"PassWD","auth":"secEnterprise"}' # change password

r = requests.post(baseurl+'/logon/long', headers=headerJson2, data=postDataJson)

sapBoLogonToken = '"' + json.loads(r.text)['logonToken'] + '"'

headerJson3 = {'accept': 'application/json', 'X-SAP-LogonToken': sapBoLogonToken}



def getUniverseIds(pj):
  ret = []
  if type(pj["universes"]["universe"]) is dict:
    if pj["universes"]["universe"]["id"] == "unx":
      ret.append( pj["universes"]["universe"]["id"] )
  elif type(pj["universes"]["universe"]) is list:
    pj2 = pj["universes"]["universe"]
    for pj2i in pj2:
      if pj2i["type"] == "unx":
        ret.append( pj2i["id"] )
  return ret

looperLimit = 2
looper = 0
looperOffset = looper * looperLimit

r = requests.get(baseurl+'/raylight/v1/universes?offset='+str(looperOffset)+'&limit='+str(looperLimit), headers=headerJson3)
parsedJson = json.loads(r.text)

universeIdList = []
while len(parsedJson["universes"]["universe"]) == looperLimit:
  universeIdList.extend( getUniverseIds(parsedJson) )
  looper += 1
  looperOffset = looper * looperLimit
  r = requests.get(baseurl+'/raylight/v1/universes?offset='+str(looperOffset)+'&limit='+str(looperLimit), headers=headerJson3)
  parsedJson = json.loads(r.text)

if type(parsedJson["universes"]["universe"]) is dict or ( type(parsedJson["universes"]["universe"]) is list and len(parsedJson["universes"]["universe"]) > 0 ):
  universeIdList.extend( getUniverseIds(parsedJson) )



def extractObjectsFromUniverseOutline(outline):
  r = []
  for oi in outline.keys():
    if oi == "item":
      if type(outline[oi]) is dict:
        r.append(outline[oi])
      elif type(outline[oi]) is list:
        r.extend(outline[oi])
    elif oi == "folder":
      if type(outline[oi]) is dict:
        r.extend( extractObjectsFromUniverseOutline(outline[oi]) )
      elif type(outline[oi]) is list:
        for oii in outline[oi]:
          r.extend( extractObjectsFromUniverseOutline(oii) )
  return r

universeObjectsList = []
for ui in universeIdList:
  r = requests.get(baseurl+'/raylight/v1/universes/' + str(ui) + '?aggregated=true', headers=headerJson3)
  parsedJson = json.loads(r.text)
  objectsList = []
  objectsList.append( "id:"+str(parsedJson["universe"]["id"]) )
  objectsList.append( "cuid:"+parsedJson["universe"]["cuid"] )
  objectsList.append( "name:"+parsedJson["universe"]["name"] )
  objectsList.append( "path:"+parsedJson["universe"]["path"] )
  objectsList = objectsList + extractObjectsFromUniverseOutline( parsedJson["universe"]["outline"] )
  universeObjectsList.append( objectsList )



f = open('./universe_object_list.json','w')

json.dump(universeObjectsList, f)

f.close()

Bir cevap yazın