David's Blog

Getting Started with SOAP APIs Using Zeep in Python

By David Li on 2024-04-10T10:00:00.000Z

Getting Started with SOAP APIs Using Zeep in Python

In the realm of web services, SOAP (Simple Object Access Protocol) remains a pivotal protocol for exchanging structured information in a decentralized, distributed environment. Although REST APIs have gained prominence for their simplicity and scalability, SOAP APIs are still widely used, particularly in enterprise and financial applications, due to their robustness, security, and standardization. This article will guide you through the basics of interacting with SOAP APIs using Zeep, a modern Python SOAP client.

Understanding SOAP APIs

SOAP is a protocol or standard for sending and receiving web service requests and responses. SOAP messages are formatted in XML and can be transported via various protocols such as HTTP, SMTP, etc. A key component of a SOAP message is the envelope, which encapsulates the entire message and includes an optional header for transmitting application-specific information and a mandatory body that contains the request or response information.

WSDL (Web Services Description Language) is an XML-based interface definition language that describes the functionality offered by a web service. It provides a machine-readable description of how the service can be called, what parameters it expects, and what data structures it returns. Zeep uses the WSDL to generate Python classes for the service operations and types.

Setting Up Your Environment

Before diving into coding, ensure you have Python installed on your system. You will need Python 3.6 or later. You can download Python from the official website. Once Python is set up, install Zeep with pip, Python’s package installer.

pip install zeep

This command installs Zeep along with its dependencies.

Using Zeep to Consume a SOAP API

Step 1: Inspecting the WSDL

The first step is to inspect the WSDL of the SOAP service you intend to use. This document provides the necessary information to understand the available methods, their input parameters, and the expected response structures. Zeep simplifies this process by allowing you to inspect the WSDL directly from your Python code:

from zeep import Client

wsdl = 'http://example.com/service?wsdl'
client = Client(wsdl)

# Print out a list of available operations
print(client.service.__dict__)

Step 2: Making a Request

Once you’re familiar with the operations available, you can make requests to the SOAP service. Assume you want to call an operation named GetUserInfo that takes a user ID as an argument:

user_id = 'abc123'
response = client.service.GetUserInfo(user_id)
print(response)

Zeep automatically converts the Python data types to the required XML structure for the SOAP request and parses the XML response back into Python objects.

Step 3: Handling Complex Types

SOAP services often involve complex types like structures or lists. Zeep excels in simplifying the interaction with these types by automatically converting them to and from Python native types or objects. If an operation requires a complex type, you can construct it as follows:

user_info = client.get_type('ns1:UserInfo')(name="John Doe", age=30)
response = client.service.UpdateUserInfo(user_id, user_info)
print(response)

Step 4: Working with Attachments

SOAP messages can include attachments which are typically used to transfer files or binary data. Zeep supports both MTOM (Message Transmission Optimization Mechanism) and SWA (SOAP with Attachments) standards for handling attachments.

with open('document.pdf', 'rb') as file:
    client.service.UploadDocument(user_id, _soapheaders={'Content-Type': 'application/pdf'}, file=file)

Best Practices and Debugging

When working with SOAP APIs, it’s crucial to handle exceptions and errors gracefully. Zeep provides detailed exceptions that can help in debugging issues:

from zeep.exceptions import Fault

try:
    response = client.service.FaultyOperation()
except Fault as error:
    print(error)

To debug requests and responses, you can enable logging:

import logging
logging.basicConfig(level=logging.DEBUG)

This will log the raw XML of the requests and responses, allowing you to inspect the data being sent and received.

Conclusion

Despite the dominance of RESTful APIs, SOAP remains vital in many sectors due to its standardization, security features, and support for complex transactions. Zeep is a powerful tool for Python developers to interact with SOAP services, abstracting much of the complexity involved in generating and parsing SOAP messages. By following the steps outlined in this article, you can quickly get started with consuming SOAP APIs using Zeep, making your integration tasks more manageable.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-12-19.