过去,我使用 Power Query 编辑器PowerBI
来请求 a sessionID
,然后使用该查询来查询 API。但是,此依赖项不适用于 PowerBI 服务。
所以现在我正在尝试通过 Python 从外部进行 API 调用。
我可以使用以下代码使其工作
import pandas as pd
import xml.etree.ElementTree as ET
from zeep import Client
import query
import authentication
# implement logging to logfile:
import logging
logger = logging.getLogger()
logging.getLogger('zeep').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
handler = logging.FileHandler('./twinfield.log')
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
#setup connection to Twinfields, provide credentials and receive cluster:
sessionlogin = r'https://login.twinfield.com/webservices/session.asmx?wsdl'
login = Client(sessionlogin)
auth = login.service.Logon(authentication.username, authentication.password, authentication.organisation)
auth_header = auth['header']['Header']
cluster = auth['body']['cluster']
#Use cluster to create a session:
url_session = cluster + r'/webservices/session.asmx?wsdl'
session = Client(url_session)
#Select a company for the session:
session.service.SelectCompany('NL001', _soapheaders={'Header': auth_header})
#And then connect to the actual webservice:
proces_url = cluster + r'/webservices/processxml.asmx?wsdl'
proces = Client(proces_url)
#Send the query and get the twinfield server response (XML):
response = proces.service.ProcessXmlString(query.XML_String, _soapheaders={'Header': auth_header})
logger.debug('response received of %s characters', len(response))
#Write raw response to XML:
xmlfilename = 'twinfield.xml'
f = open(xmlfilename, 'w')
f.write(response)
f.close()
logger.debug('Raw XML file written: %s', xmlfilename)
Query.py 看起来像这样:
XML_String= r'''<columns code='030_1'>
<column>
<field>fin.trs.head.office</field>
<label>Administratie</label>
<visible>true</visible>
<ask>false</ask>
<operator>none</operator>
<from></from>
</column>
<column>
<field>fin.trs.head.year</field>
<label>Jaar</label>
<visible>true</visible>
<ask>true</ask>
<operator>none</operator>
<from>2020</from>
</column>
<column>
<field>fin.trs.head.period</field>
<label>Periode</label>
<visible>true</visible>
<ask>true</ask>
<operator>none</operator>
</column>
<column>
<field>fin.trs.head.date</field>
<label>Boekdatum</label>
<visible>true</visible>
<ask>false</ask>
<operator>none</operator>
<from></from>
<to></to>
<finderparam></finderparam>
</column>
<column>
<field>fin.trs.line.dim1</field>
<label>Grootboekrek.</label>
<visible>true</visible>
<ask>true</ask>
<operator>between</operator>
<from></from>
<to></to>
<finderparam></finderparam>
</column>
<column>
<field>fin.trs.line.dim1name</field>
<label>Grootboekrek.naam</label>
<visible>true</visible>
<ask>false</ask>
<operator>none</operator>
<from></from>
<to></to>
<finderparam></finderparam>
</column>
</columns>'''
该代码可以很好地写入 XML 文件。但是,当我开始添加更多列并运行代码时,屏幕上突然出现了大量的文本,而不是黑色的 py.exe。完成后,将写入无日志或 XML 文件。
这就是我的意思: