Coverage for docs_src/openapi_callbacks/tutorial001.py: 100%
21 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
1from typing import Union 1abcdefg
3from fastapi import APIRouter, FastAPI 1abcdefg
4from pydantic import BaseModel, HttpUrl 1abcdefg
6app = FastAPI() 1abcdefg
9class Invoice(BaseModel): 1abcdefg
10 id: str 1abcdefg
11 title: Union[str, None] = None 1abcdefg
12 customer: str 1abcdefg
13 total: float 1abcdefg
16class InvoiceEvent(BaseModel): 1abcdefg
17 description: str 1abcdefg
18 paid: bool 1abcdefg
21class InvoiceEventReceived(BaseModel): 1abcdefg
22 ok: bool 1abcdefg
25invoices_callback_router = APIRouter() 1abcdefg
28@invoices_callback_router.post( 1abcdefg
29 "{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
30)
31def invoice_notification(body: InvoiceEvent): 1abcdefg
32 pass 1hijklmn
35@app.post("/invoices/", callbacks=invoices_callback_router.routes) 1abcdefg
36def create_invoice(invoice: Invoice, callback_url: Union[HttpUrl, None] = None): 1abcdefg
37 """
38 Create an invoice.
40 This will (let's imagine) let the API user (some external developer) create an
41 invoice.
43 And this path operation will:
45 * Send the invoice to the client.
46 * Collect the money from the client.
47 * Send a notification back to the API user (the external developer), as a callback.
48 * At this point is that the API will somehow send a POST request to the
49 external API with the notification of the invoice event
50 (e.g. "payment successful").
51 """
52 # Send the invoice, collect the money, send the notification (the callback)
53 return {"msg": "Invoice received"} 1opqrstu