### Define and Run a SOAP Service with FastAPI
Source: https://github.com/mittmannv8/fastapi-soap/blob/main/README.md
This example demonstrates how to set up a SOAP service using FastAPI Soap. It defines Pydantic models for request and response, creates a SoapRouter, registers an operation, and includes the router in a FastAPI application. Dependencies include FastAPI, Pydantic, and Pydantic XML. The code runs as a complete, executable script.
```python
from fastapi import FastAPI
from pydantic_xml import element
from fastapi_soap import SoapRouter, XMLBody, SoapResponse
from fastapi_soap.models import BodyContent
class Operands(BodyContent, tag="Operands"):
operands: list[float] = element(tag="Operand")
class Result(BodyContent, tag="Result"):
value: float
soap = SoapRouter(name='Calculator', prefix='/Calculator')
@soap.operation(
name="SumOperation",
request_model=Operands,
response_model=Result
)
def sum_operation(body: Operands = XMLBody(Operands)):
"""
Receives an Operands object and returns a Result object.
Args:
body (Operands): Operands object with a list of operands
"""
result = sum(body.operands)
return SoapResponse(
Result(value=result)
)
app = FastAPI()
app.include_router(soap)
if __name__ == '__main__':
import uvicorn # Any Python ASGI web server can be used
uvicorn.run("example.main:app")
```
--------------------------------
### Example SOAP XML Request
Source: https://github.com/mittmannv8/fastapi-soap/blob/main/README.md
Illustrates the structure of an XML request payload for a SOAP service. This example shows a SOAP envelope containing an 'Operands' element with a list of float values, used as input for an operation like 'SumOperation'.
```xml
1
2
3
```
--------------------------------
### Accessing WSDL
Source: https://github.com/mittmannv8/fastapi-soap/blob/main/README.md
Shows how to retrieve the Web Services Description Language (WSDL) for a SOAP service hosted by FastAPI. The WSDL is typically accessed via an HTTP GET request to the service's root path with a '?wsdl' query parameter.
```http
GET http://localhost:8000/Calculator?wsdl
```
--------------------------------
### WSDL Endpoint
Source: https://github.com/mittmannv8/fastapi-soap/blob/main/README.md
Provides access to the Web Services Description Language (WSDL) document for the SOAP service.
```APIDOC
## GET /Calculator?wsdl
### Description
Retrieves the WSDL document for the Calculator SOAP service.
### Method
GET
### Endpoint
/Calculator?wsdl
### Response
#### Success Response (200 OK)
- **WSDL Document** - The WSDL XML content describing the available SOAP operations.
```
--------------------------------
### Calculator SOAP Service
Source: https://github.com/mittmannv8/fastapi-soap/blob/main/README.md
Defines a SOAP service named 'Calculator' with a 'SumOperation' endpoint. This operation accepts a list of operands and returns their sum.
```APIDOC
## POST /Calculator
### Description
Exposes a SOAP calculator service. The `SumOperation` endpoint takes a list of operands and returns their sum.
### Method
POST
### Endpoint
/Calculator
### Parameters
#### Request Body
- **body** (Operands) - Required - The SOAP request body containing the list of operands.
### Request Example
```xml
1
2
3
```
### Response
#### Success Response (200 OK)
- **Result** (Result) - The SOAP response body containing the calculated sum.
#### Response Example
```xml
6.0
```
```
--------------------------------
### Accessing Request Body Data in Python
Source: https://github.com/mittmannv8/fastapi-soap/blob/main/README.md
Demonstrates how to access and process the data from an incoming SOAP request within a FastAPI endpoint. The request body, parsed into a Pydantic model (e.g., 'Operands'), can be directly accessed and manipulated in Python.
```python
print(body)
# Output Operands(operands=[1.0, 2.0, 3.0])
print(body.operands)
# Output [1.0, 2.0, 3.0]
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.