Python > Working with External Resources > APIs (Application Programming Interfaces) > SOAP APIs

Accessing a SOAP API with Suds

This snippet demonstrates how to interact with a SOAP API using the suds library in Python. SOAP (Simple Object Access Protocol) is a messaging protocol that allows applications to communicate over a network. This example will guide you through installing the necessary library, making a request to a SOAP API, and handling the response. Keep in mind that suds is an older library and might not be compatible with all SOAP services. Consider using zeep for more modern SOAP implementations.

Prerequisites

Before running this code, you need to install the suds library. You can install it using pip: pip install suds-jurko Note: We are using suds-jurko because the original suds is no longer maintained.

Code Implementation

This code snippet first imports the Client class from the suds.client module. Then, it creates a Client object using the URL of the WSDL file. The WSDL file describes the services provided by the SOAP API. We then call the Add method of the service, passing in two integers as arguments. Finally, the result is printed to the console. Error handling is included within a try...except block to catch potential exceptions that might occur during the API call (e.g., network errors, invalid WSDL format, incorrect method parameters).

from suds.client import Client

# The URL of the SOAP WSDL (Web Services Description Language) file.
# Replace this with the actual URL of the WSDL for the SOAP API you want to use.
wsdl_url = 'http://www.dneonline.com/calculator.asmx?WSDL'

try:
    # Create a Suds client instance.
    client = Client(wsdl_url)

    # Print the methods available in the SOAP service. (Optional).
    # print(client)

    # Call the 'Add' method of the SOAP service.
    # Replace 5 and 3 with the actual numbers you want to add.
    result = client.service.Add(intA=5, intB=3)

    # Print the result.
    print(f'The result of 5 + 3 is: {result}')

except Exception as e:
    print(f'An error occurred: {e}')

Explanation of Key Components

  • WSDL URL: The URL pointing to the WSDL file. This file contains the description of the SOAP service, including available methods, parameters, and data types.
  • Client Object: An instance of the suds.client.Client class. It handles the communication with the SOAP service.
  • Service Call: Calling a specific method (e.g., client.service.Add) triggers the execution of the corresponding operation on the SOAP service. The arguments are passed as keyword arguments (e.g., intA=5, intB=3).

Real-Life Use Case

Imagine you are building an e-commerce application and need to integrate with a third-party payment gateway that exposes a SOAP API. You could use suds (or a more modern alternative like zeep) to make requests to the payment gateway's API, passing in transaction details such as the amount, currency, and credit card information. The API would then process the payment and return a response indicating whether the transaction was successful.

Best Practices

  • Error Handling: Always include error handling to gracefully handle potential exceptions during API calls.
  • WSDL Validation: Ensure the WSDL file is valid and accessible. Invalid WSDL files will prevent the suds client from creating a connection to the service.
  • Security: When dealing with sensitive data, such as financial information, use secure connections (HTTPS) and implement appropriate authentication mechanisms as specified by the SOAP API provider.
  • Data Validation: Validate the data you are sending to the API to prevent errors and ensure data integrity.
  • Consider Zeep: For newer projects, consider using the zeep library instead of suds, as it is actively maintained and offers better support for modern SOAP features.

When to use SOAP APIs

SOAP APIs are often used in enterprise environments where strict standards and security are paramount. They are suitable for applications requiring strong data typing, transaction management, and security features like WS-Security.

Alternatives

  • REST APIs: REST (Representational State Transfer) APIs are a more modern alternative to SOAP. They are simpler to use and often preferred for web applications. Libraries like requests can be used to interact with REST APIs.
  • Zeep: Zeep is a more modern and actively maintained library for interacting with SOAP APIs in Python.
  • gSOAP: gSOAP is a C and C++ toolkit for developing SOAP web services. While not Python-specific, it can be used to create web services that Python applications can interact with.

Pros and Cons of Using SOAP

Pros:

  • Standardization: SOAP is a well-defined standard, ensuring interoperability between different systems.
  • Security: SOAP supports WS-Security, providing robust security features.
  • Transaction Management: SOAP supports transaction management, ensuring data consistency.
Cons:
  • Complexity: SOAP is more complex than REST, requiring more overhead for message parsing and processing.
  • Performance: SOAP messages are typically larger than REST messages, leading to slower performance.
  • Limited Browser Support: SOAP is not as well-suited for browser-based applications as REST.

FAQ

  • What is a WSDL file?

    A WSDL (Web Services Description Language) file is an XML document that describes the services offered by a SOAP API. It defines the methods, parameters, and data types that can be used to interact with the API.
  • Why am I getting an error when running the code?

    Possible causes include an invalid WSDL URL, network connectivity issues, incorrect method parameters, or an unsupported SOAP version. Check the WSDL URL, ensure you have an internet connection, and verify that the method parameters are correct. Also, make sure your suds version is compatible with the SOAP service you are trying to access.
  • Is suds still maintained?

    The original suds library is no longer actively maintained. It is recommended to use suds-jurko, a maintained fork, or consider using zeep for newer projects.