Python API call not writing to file

207 views Asked by At

In the past, I used the Power Query Editor in PowerBI to request a sessionID and then use that query to query an API. However, this dependency does not work with PowerBI Service.

So now I'm trying to do the API call externally via Python.

I can get it to work using the following code

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 looks like this:

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>'''

The code writes fine to XML file. However, when I start adding more columns and run the code, instead of a black py.exe suddenly there is an onslaught of text running through the screen. When it finishes, no-log or XML file is written.

This is what I mean:

1

0

There are 0 answers