Coverage for docs_src / openapi_callbacks / tutorial001_py310.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from fastapi import APIRouter, FastAPI 1abfgcde

2from pydantic import BaseModel, HttpUrl 1abfgcde

3 

4app = FastAPI() 1abfgcde

5 

6 

7class Invoice(BaseModel): 1abfgcde

8 id: str 1abcde

9 title: str | None = None 1abfgcde

10 customer: str 1abcde

11 total: float 1abcde

12 

13 

14class InvoiceEvent(BaseModel): 1abfgcde

15 description: str 1abcde

16 paid: bool 1abcde

17 

18 

19class InvoiceEventReceived(BaseModel): 1abfgcde

20 ok: bool 1abcde

21 

22 

23invoices_callback_router = APIRouter() 1abfgcde

24 

25 

26@invoices_callback_router.post( 1abfgcde

27 "{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived 

28) 

29def invoice_notification(body: InvoiceEvent): 1abfgcde

30 pass 1hijk

31 

32 

33@app.post("/invoices/", callbacks=invoices_callback_router.routes) 1abfgcde

34def create_invoice(invoice: Invoice, callback_url: HttpUrl | None = None): 1abfgcde

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"} 1lmno