Package: pymtom-xop
SOAP MTOM-XOP Support for PythonThis library adds SOAP MTOM-XOP support for python 3 using Python Zeep.The library uses custom classes to override the behavior of Python Zeep, tranforming a basic SOAP message into a MTOM-XOP message before handling it back to Zeep to be sent in a POST request.Its main actors are the MtomTransport and MtomAttachment classes.MtomTransportinherits from Zeep's Transport class and overrides its post_xml method.MtomAttachmentis used to represent each file in the request body.Installation# from PyPIpipinstallpymtom-xop# or from githubpipinstallgit+https://github.com/Gebrel07/pymtom-xop.git@mainHow to useConsider the following type definitions in a WSDL where the operation name is "UploadFile":<xs:complexTypename="uploadFileWs"><xs:sequence><xs:elementminOccurs="0"name="file"type="xs:base64Binary"/><xs:elementminOccurs="0"name="fileName"type="xs:string"/><xs:elementminOccurs="0"name="fileExtension"type="xs:string"/></xs:sequence></xs:complexType>To use this Web Service we can use theMtomTransportandMtomAttachmentclasses like this:frompymtom_xopimportMtomAttachment,MtomTransportfromzeepimportClient,Settings# create a MtomAttachment instance to represent the file# the "file" argument can be a file path or a BytesIO object, in this case, lets use a file stored in the "documents" foldermtom_attachment=MtomAttachment(file="documents/python.pdf")# use MtomTransport instead of Zeep's standard Tranportmtom_transport=MtomTransport()# use the add_files method to add files to the transport# the "files" argument must be a list of MtomAttachment objectsmtom_transport.add_files(files=[mtom_attachment])# set up a Client using MtomTransportclient=Client(wsdl="documents/UploadWSDL.wsdl",transport=mtom_transport)# WARNING: namespace might change according to your Webservice's configurationfactory=client.type_factory(namespace="ns0")# build SOAP Envelope normally using Zeep# NOTE: use mtom_attachment's get_cid method to insert the attachment's Content-ID in the "file" fieldarg0=factory.uploadFileWs(file=mtom_attachment.get_cid(),fileName="python",fileExtension="pdf")# call the service normally using Zeepresponse=client.service.uploadFile(arg0)ClassesMTOMAttachment:TheMTOMAttachmentclass is responsible for setting up all of the necessary information about the file before adding it to the request body.When inserting a file in the SOAP Envelope, the get_cid method must be used in place of the file's binary data.Methodsget_cid:Returns cid without the < > parts

Returns:
    bytes: File's Content-ID

    Example: b"168954589437.10472.2748258243972472116@pymtom-xop"MTOMTransport:After calling the service's operation, Zeep will parse the SOAP message as it normally does. The SOAP message and HTTP headers will be passed on to theMTOMTransportclass.MTOMTransportuses its methods to transform the SOAP message into a MTOM-XOP message, adds the necessary HTTP headers and gives it back toZeepto be sent as aPOSTrequest.MTOMTransport objects will accept any of zeep Tranport arguments when initialized, such as: cache, timeout, operation_timeout, session etc...ExamplesSee "documents" folder for examples of MTOM Request and Response in XMLSee "demo.py" for demonstration of a requestReferences:inspired by pymtom by zvolsky (https://github.com/pyutil/pymtom)based on requests made with SOAPUISeehttps://docs.python-zeep.org/en/master/for Python Zeep's official documentaion.