Skip to main content

SOAP MTOM-XOP Support for Python

Project description

SOAP MTOM-XOP Support for Python

This 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.

MtomTransport inherits from Zeep's Transport class and overrides its post_xml method.

MtomAttachment is used to represent each file in the request body.

Installation

# from PyPI
pip install pymtom-xop

# or from github
pip install git+https://github.com/Gebrel07/pymtom-xop.git@main

How to use

Consider the following type definitions in a WSDL where the operation name is "UploadFile":

<xs:complexType name="uploadFileWs">
    <xs:sequence>
        <xs:element minOccurs="0" name="file" type="xs:base64Binary"/>
        <xs:element minOccurs="0" name="fileName" type="xs:string"/>
        <xs:element minOccurs="0" name="fileExtension" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

To use this Web Service we can use the MtomTransport and MtomAttachment classes like this:

from pymtom_xop import MtomAttachment, MtomTransport
from zeep import Client, 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" folder
mtom_attachment = MtomAttachment(file="documents/python.pdf")

# use MtomTransport instead of Zeep's standard Tranport
mtom_transport = MtomTransport()
# use the add_files method to add files to the transport
# the "files" argument must be a list of MtomAttachment objects
mtom_transport.add_files(files=[mtom_attachment])

# set up a Client using MtomTransport
client = Client(wsdl="documents/UploadWSDL.wsdl", transport=mtom_transport)

# WARNING: namespace might change according to your Webservice's configuration
factory = 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" field
arg0 = factory.uploadFileWs(
    file=mtom_attachment.get_cid(),
    fileName="python",
    fileExtension="pdf"
)

# call the service normally using Zeep
response = client.service.uploadFile(arg0)

Classes

MTOMAttachment:

The MTOMAttachment class 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.

Methods

get_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 the MTOMTransport class.

MTOMTransport uses its methods to transform the SOAP message into a MTOM-XOP message, adds the necessary HTTP headers and gives it back to Zeep to be sent as a POST request.

MTOMTransport objects will accept any of zeep Tranport arguments when initialized, such as: cache, timeout, operation_timeout, session etc...

Examples

  • See "documents" folder for examples of MTOM Request and Response in XML
  • See "demo.py" for demonstration of a request

References:

See https://docs.python-zeep.org/en/master/ for Python Zeep's official documentaion.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymtom_xop-0.0.2.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymtom_xop-0.0.2-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file pymtom_xop-0.0.2.tar.gz.

File metadata

  • Download URL: pymtom_xop-0.0.2.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for pymtom_xop-0.0.2.tar.gz
Algorithm Hash digest
SHA256 281460deb89ea2909be6b5060839e2be95ee5863d3acdcf0b8c3fcddac866073
MD5 efcae1f4b910cda0eeae1679b59b5565
BLAKE2b-256 17d48f49a087c15a1177202a0e0c76ec72471f42bf020e01cf378247926891e0

See more details on using hashes here.

File details

Details for the file pymtom_xop-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pymtom_xop-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for pymtom_xop-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c053040341c534d682e5c55ea7e5ce298b7bd0e94474522b6d961d4817f989fe
MD5 f62c60c45d215521c8a3779818896d2c
BLAKE2b-256 117362671388675ff737b99a190a6a46e6730f5a7631864d6aa72339ef6a98ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page