Giter Club home page Giter Club logo

fastapi-soap's Introduction

FastAPI Soap

This package helps to create Soap WebServices using FastAPI (What?!?!)

Motivation

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

Installation and dependencies

Only FastAPI, Pydantic and Pydantic XML are required.

First steps

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")

(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

Examples

XML Request

<soapenv:Envelope
    xmlns: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]

fastapi-soap's People

Contributors

gcdsss avatar mittmannv8 avatar

Forkers

getsenic

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.