0

I would like to expose a Pandas data frame with out_protocol=XmlDocument() as a SOAP web service.

As of now, I only managed to expose a String by calling the web service with HTTP in_protocol. Here is the working code.

Server code:

from spyne import Application, srpc, ServiceBase, \
    Integer, Unicode, String

from spyne import Iterable
from spyne.protocol.http import HttpRpc
from spyne.protocol.soap import Soap11
from spyne.protocol.json import JsonDocument
from spyne.protocol.xml import XmlDocument

from spyne.server.wsgi import WsgiApplication

class HelloWorldService(ServiceBase):
    @srpc(String, Integer, _returns=String)
    def say_hello(name, times):
        s = ('Hi' + str(name)+' ')*times
        return s

application = Application([HelloWorldService],
    tns='spyne.examples.hello.http',
    in_protocol=HttpRpc(),    #Soap11 for SOAP client
    out_protocol=XmlDocument()
)
if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    wsgi_app = WsgiApplication(application)
    server = make_server('127.0.0.1', 8000, wsgi_app)
    server.serve_forever()

Client code:

curl "http://localhost:8000/say_hello?times=5&name=Dave"

How should I change the code to best expose a Pandas dataframe instead of a string. And how to make the client use the SOAP protocol to make requests?

My attempt for the SOAP Client:

from zeep import Client

client = Client('http://localhost:8000/?wsdl')
result = client.service.say_hello("Antonio", 10)

print(result)

Expected output of web service should be a table-like xml. Here is an example:

enter image description here

Francesco Pegoraro
  • 778
  • 13
  • 33
  • What is a pandas data frame? What is the expected XML output? – Burak Arslan Jun 20 '19 at 15:31
  • Hi, a pandas dataframe is essentially a data structure composed like a database table with rows and columns. You can look it up here: https://www.geeksforgeeks.org/python-pandas-dataframe/ . I added an example of the desired output. – Francesco Pegoraro Jun 20 '19 at 15:39

1 Answers1

1

Soap Services use xml inherently for web services. From this question I gather that you need xml to feed to the server!!
As you said that you can convert the result to pandas DF, then from a DF to xml, link:

def to_xml(df, filename=None, mode='w'):
    def row_to_xml(row):
        xml = ['<item>']
        for i, col_name in enumerate(row.index):
            xml.append('  <field name="{0}">{1}</field>'.format(col_name, row.iloc[i]))
        xml.append('</item>')
        return '\n'.join(xml)
    res = '\n'.join(df.apply(row_to_xml, axis=1))

    if filename is None:
        return res
    with open(filename, mode) as f:
        f.write(res)
ASHu2
  • 2,027
  • 15
  • 29