Coverage for docs_src/openapi_callbacks/tutorial001.py: 100%
21 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from typing import Union 1abcde
3from fastapi import APIRouter, FastAPI 1abcde
4from pydantic import BaseModel, HttpUrl 1abcde
6app = FastAPI() 1abcde
9class Invoice(BaseModel): 1abcde
10 id: str 1abcde
11 title: Union[str, None] = None 1abcde
12 customer: str 1abcde
13 total: float 1abcde
16class InvoiceEvent(BaseModel): 1abcde
17 description: str 1abcde
18 paid: bool 1abcde
21class InvoiceEventReceived(BaseModel): 1abcde
22 ok: bool 1abcde
25invoices_callback_router = APIRouter() 1abcde
28@invoices_callback_router.post( 1abcde
29 "{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
30)
31def invoice_notification(body: InvoiceEvent): 1abcde
32 pass 1abcde
35@app.post("/invoices/", callbacks=invoices_callback_router.routes) 1abcde
36def create_invoice(invoice: Invoice, callback_url: Union[HttpUrl, None] = None): 1abcde
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"} 1abcde