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

20 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi import APIRouter, FastAPI 1abc

2from pydantic import BaseModel, HttpUrl 1abc

3 

4app = FastAPI() 1abc

5 

6 

7class Invoice(BaseModel): 1abc

8 id: str 1abc

9 title: str | None = None 1abc

10 customer: str 1abc

11 total: float 1abc

12 

13 

14class InvoiceEvent(BaseModel): 1abc

15 description: str 1abc

16 paid: bool 1abc

17 

18 

19class InvoiceEventReceived(BaseModel): 1abc

20 ok: bool 1abc

21 

22 

23invoices_callback_router = APIRouter() 1abc

24 

25 

26@invoices_callback_router.post( 1abc

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

28) 

29def invoice_notification(body: InvoiceEvent): 1abc

30 pass 1def

31 

32 

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

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

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