cancel
Showing results for 
Search instead for 
Did you mean: 

Open Bytes Object or String Object with userdefined Python Operator (Generation 2)

06-27-2023 12:57 PM
gbrungs11 Explorer
316 views 0 comments
0 Likes
SAP Managed Tags
Subscribe

Hi Community,

I have created a custom Python operator which establishes an HTTP connection and reads the content using the Python package requests. The response is read using the response.content function and sent to the outport using api.outputs.OutputSoftgarden.publish(response.content,header=header). Outport type is of type scalar (com.sap.core.string). The response.content object is of type bytes.

Code:

from urllib.parse import urljoin

import requests

import pandas as pd

import io

def gen():

# Zusammensetzung der url für die Verbindung zum Softgarden

host = api.config.softgarden['connectionProperties']['host']

path = api.config.softgarden['connectionProperties']['path']

url = urljoin(host, path)

# Request senden und Response bekommen

response = requests.get(url)

# Zeichenkodierung auf UTF-8 setzen

response.encoding = 'utf-8'

# Response Inhalt speichern (hier handelt es sich um ein Objekt der <class 'bytes'>), welche in ein Dataframe umgewandelt werden soll

data_bytes = response.content

# Header erstellen

header = api.Record([host,path,url,0,0])

header = {"diadmin.softgarden.header":header}

api.logger.info(f"Header send:{header}")

api.outputs.OutputSoftgarden.publish(data_bytes,header=header)

api.set_prestart(gen)

I now want to create a new custom Python operator which will include the response.content object and transform it as Pandas Data Frame. What is the python code for this? I can't get anywhere using the body.get() function.

I could also read the response using the response.text function. Here the same question arises how I can read it in a new Python operator and transform it into a DataFrame.

Thanks for your effort in advance!

Many greetings

Geena

0 Likes

Accepted Solutions (0)

Answers (0)