This package helps to create Soap WebServices using FastAPI (What?!?!)
I know, FastAPI is a REST micro framework, but sometimes is needed to expose a Soap Interface on a already running FastAPI application for an legacy client/application that only supports, well, the Soap protocol...
Only FastAPI, Pydantic and Pydantic XML are required.
fromfastapiimportFastAPIfrompydantic_xmlimportelementfromfastapi_soapimportSoapRouter, XMLBody, SoapResponsefromfastapi_soap.modelsimportBodyContentclassOperands(BodyContent, tag="Operands"):
operands: list[float] =element(tag="Operand")
classResult(BodyContent, tag="Result"):
value: floatsoap=SoapRouter(name='Calculator', prefix='/Calculator')
@soap.operation(name="SumOperation",request_model=Operands,response_model=Result)defsum_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)
returnSoapResponse(
Result(value=result)
)
app=FastAPI()
app.include_router(soap)
if__name__=='__main__':
importuvicorn# Any Python ASGI web server can be useduvicorn.run("example.main:app")(This script is complete, it should run "as is")
The WSDL is available on webservice root path for GET method.
GET http://localhost:8000/Calculator?wsdl
XML Request
<soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<Operands>
<!--Zero or more repetitions:-->
<Operand>1</Operand>
<Operand>2</Operand>
<Operand>3</Operand>
</Operands>
</soapenv:Envelope>For this example, the sum_operation function will receive an Pydantic object as body
print(body)
# Output Operands(operands=[1.0, 2.0, 3.0])print(body.operands)
# Output [1.0, 2.0, 3.0]