我是 python 新手。我需要从 Oracle 融合云中获取数据。我想使用 SOAP API 调用在 Oracle 融合云实例上运行 BI 发布者报告,并将数据转换为 CSV 文件。
我尝试过使用 python ZEEP 和 REQUESTS 模块,但没有得到预期的结果。
例如:我的 WSDL:https ://xxx.yy.us6.oraclecloud.com/xmlpserver/services/ExternalReportWSSService?wsdl
我需要使用的操作来自上面的 WSDL 是 'runReport'
当我从 SOAP UI 运行此请求以进行“runReport”操作时,我得到如下预期结果:
我在 python (Python 3.5) 中使用下面的代码来调用这个 API。我同时使用了 REQUESTS 和 ZEEP:
1. 请求模块:
from requests.auth import HTTPBasicAuth
from xml.etree import ElementTree
url="https://xxxx.yyyy.us6.oraclecloud.com/xmlpserver/services/ExternalReportWSSService?wsdl"
#headers = {'content-type': 'application/soap+xml'}
headers = {'content-type': 'text/xml'}
body = """<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:pub="http://xmlns.oracle.com/oxp/service/PublicReportService">
<soap:Header/>
<soap:Body>
<pub:runReport>
<pub:reportRequest>
<pub:attributeFormat>csv</pub:attributeFormat>
<!-- Flatten XML should always be false when we have XML type of output to display the XML tags as mentioned in BIP Data Model and display XML structure in as expected format -->
<pub:flattenXML>false</pub:flattenXML>
<pub:parameterNameValues>
<!--1st Parameter of BIP Report-->
<pub:item>
<pub:name>p_name</pub:name>
<pub:values>
<pub:item>tapan</pub:item>
</pub:values>
</pub:item>
<!--2nd Parameter of BIP Report-->
<!--<pub:item>
<pub:name>p_to_date</pub:name>
<pub:values>
<pub:item>10-15-2019</pub:item>
</pub:values>
</pub:item>-->
</pub:parameterNameValues>
<pub:reportAbsolutePath>/Custom/Integration/test_data_rpt.xdo</pub:reportAbsolutePath>
<!-- Setting sizeOfDataChunkDownload to -1 will return the output to the calling client -->
<pub:sizeOfDataChunkDownload>-1</pub:sizeOfDataChunkDownload>
</pub:reportRequest>
</pub:runReport>
</soap:Body>
</soap:Envelope>"""
response = requests.get(url,data=body,headers=headers,auth=HTTPBasicAuth('XXXX', 'XXXX'))
print (response.text)
上面的代码只是给了我 WSDL 中可用的操作列表
2. ZEEP模块
from zeep import Client
from requests import Session
from requests.auth import HTTPBasicAuth
from zeep.transports import Transport
wsdl = "https://XXXX.XXXX.us6.oraclecloud.com/xmlpserver/services/ExternalReportWSSService?wsdl"
session = Session()
session.auth = HTTPBasicAuth('XXXX', 'XXXX')
#An additional argument 'transport' is passed with the authentication details
client = Client(wsdl, transport=Transport(session=session))
request_payload= """<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:pub="http://xmlns.oracle.com/oxp/service/PublicReportService">
<soap:Header/>
<soap:Body>
<pub:runReport>
<pub:reportRequest>
<pub:attributeFormat>csv</pub:attributeFormat>
<!-- Flatten XML should always be false when we have XML type of output to display the XML tags as mentioned in BIP Data Model and display XML structure in as expected format -->
<pub:flattenXML>false</pub:flattenXML>
<pub:parameterNameValues>
<!--1st Parameter of BIP Report-->
<pub:item>
<pub:name>p_name</pub:name>
<pub:values>
<pub:item>tapan</pub:item>
</pub:values>
</pub:item>
<!--2nd Parameter of BIP Report-->
<!--<pub:item>
<pub:name>p_to_date</pub:name>
<pub:values>
<pub:item>10-15-2019</pub:item>
</pub:values>
</pub:item>-->
</pub:parameterNameValues>
<pub:reportAbsolutePath>/Custom/Integration/test_data_rpt.xdo</pub:reportAbsolutePath>
<!-- Setting sizeOfDataChunkDownload to -1 will return the output to the calling client -->
<pub:sizeOfDataChunkDownload>-1</pub:sizeOfDataChunkDownload>
</pub:reportRequest>
</pub:runReport>
</soap:Body>
</soap:Envelope>"""
response = client.service.runReport(request_payload)
#Here 'request_data' is the request parameter dictionary.
#Assuming that the operation named 'runReport' is defined in the passed wsdl.
上面的代码不起作用,因为我不确定如何使用 ZEEP 模块传递请求有效负载。
请帮我!!