cercide / fastapi-xml

adds xml support to fastapi
MIT License
12 stars 2 forks source link

Example app using FastAPI routes folder #3

Closed spopp20 closed 1 year ago

spopp20 commented 1 year ago

Can you provide an example of a fastapi-xml/FastAPI app using a routes folder?

If you place your echo example in echo.py in a routes folder we no longer have the @app.get decorator defined and using

This can issue can be closed.

Here is a working routes/health.py example.

from dataclasses import dataclass, field
from fastapi import APIRouter
from fastapi_xml import XmlBody
from fastapi_xml import NonJsonRoute

router = APIRouter()
router.route_class = NonJsonRoute

@dataclass
class HealthBody:
    message: str = field(metadata={"example": "Server Health","name": "Message", "type": "Element"})

@router.post('/health', response_model=HealthBody, tags=["Health"])
async def postHealth(x: HealthBody = XmlBody()) -> HealthBody:
    x.message += ' Healthy!'
    return x