| 1 |
from fastapi import APIRouter, FastAPI |
| 2 |
from pydantic import BaseModel, HttpUrl |
| 3 |
|
| 4 |
app = FastAPI() |
| 5 |
|
| 6 |
|
| 7 |
class Invoice(BaseModel): |
| 8 |
id: str |
| 9 |
title: str | None = None |
| 10 |
customer: str |
| 11 |
total: float |
| 12 |
|
| 13 |
|
| 14 |
class InvoiceEvent(BaseModel): |
| 15 |
description: str |
| 16 |
paid: bool |
| 17 |
|
| 18 |
|
| 19 |
class InvoiceEventReceived(BaseModel): |
| 20 |
ok: bool |
| 21 |
|
| 22 |
|
| 23 |
invoices_callback_router = APIRouter() |
| 24 |
|
| 25 |
|
| 26 |
@invoices_callback_router.post( |
| 27 |
"{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived |
| 28 |
) |
| 29 |
def invoice_notification(body: InvoiceEvent): |
| 30 |
pass |
| 31 |
|
| 32 |
|
| 33 |
@app.post("/invoices/", callbacks=invoices_callback_router.routes) |
| 34 |
def create_invoice(invoice: Invoice, callback_url: HttpUrl | None = None): |
| 35 |
""" |
| 36 |
Create an invoice. |
| 37 |
|
| 38 |
This will (let's imagine) let the API user (some external developer) create an |
| 39 |
invoice. |
| 40 |
|
| 41 |
And this path operation will: |
| 42 |
|
| 43 |
* Send the invoice to the client. |
| 44 |
* Collect the money from the client. |
| 45 |
* Send a notification back to the API user (the external developer), as a callback. |
| 46 |
* At this point is that the API will somehow send a POST request to the |
| 47 |
external API with the notification of the invoice event |
| 48 |
(e.g. "payment successful"). |
| 49 |
""" |
| 50 |
# Send the invoice, collect the money, send the notification (the callback) |
| 51 |
return {"msg": "Invoice received"} |