2022-05-14 06:59:59 -05:00
|
|
|
from typing import Union
|
2020-06-28 20:13:30 +02:00
|
|
|
|
2019-12-11 18:58:01 +02:00
|
|
|
from fastapi import APIRouter, FastAPI
|
|
|
|
|
from pydantic import BaseModel, HttpUrl
|
|
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Invoice(BaseModel):
|
|
|
|
|
id: str
|
2022-05-14 06:59:59 -05:00
|
|
|
title: Union[str, None] = None
|
2019-12-11 18:58:01 +02:00
|
|
|
customer: str
|
|
|
|
|
total: float
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InvoiceEvent(BaseModel):
|
|
|
|
|
description: str
|
|
|
|
|
paid: bool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InvoiceEventReceived(BaseModel):
|
|
|
|
|
ok: bool
|
|
|
|
|
|
|
|
|
|
|
✨ Add support for shared/top-level parameters (dependencies, tags, etc) (#2434)
* ✨ Add Default and DefaultPlaceholder data structures
to handle defaults and overrides
* ✨ Add utils to get values by priority handling DefaultPlaceholders
* ✨ Add support for top-level parameters in FastAPI, APIRouter, include_router
including: prefix, tags, dependencies, deprecated, include_in_schema, responses, default_response_class, callbacks
* ♻️ Update openapi utils to handle DefaultPlaceholder for response_class
* 📝 Update bigger-application example code to use top-level params
and showcase them in APIRouter, FastAPI, include_router
* 📝 Update docs for Bigger Applications, include diagrams, top-level params
* 🔥 Simplify code and docs for callbacks as default_response_class is no longer required
* 📝 Add docs for top-level dependencies, in FastAPI()
* 📝 Add docs reference to top-level dependencies in docs for decorator
* ✅ Update/increase tests for Bigger Applications including shared parameters
* ✅ Add tests for top-level dependencies in FastAPI()
* ✅ Add tests for internal DefaultPlaceholder
* ✅ Update/increase tests for callbacks with top-level parameters
* ✅ Add LOTS of tests covering branches and cases for shared parameters
in top-level FastAPI, path operations, include_router, APIRouter, its path operations, nested include_router, nested APIRouter, and its path operations
* 🎨 Format/reorder parameters for consistency in FastAPI, APIRouter, include_router
2020-11-29 18:32:18 +01:00
|
|
|
invoices_callback_router = APIRouter()
|
2019-12-11 18:58:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@invoices_callback_router.post(
|
2020-12-20 19:50:00 +01:00
|
|
|
"{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
|
2019-12-11 18:58:01 +02:00
|
|
|
)
|
|
|
|
|
def invoice_notification(body: InvoiceEvent):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/invoices/", callbacks=invoices_callback_router.routes)
|
2022-05-14 06:59:59 -05:00
|
|
|
def create_invoice(invoice: Invoice, callback_url: Union[HttpUrl, None] = None):
|
2019-12-11 18:58:01 +02:00
|
|
|
"""
|
|
|
|
|
Create an invoice.
|
|
|
|
|
|
|
|
|
|
This will (let's imagine) let the API user (some external developer) create an
|
|
|
|
|
invoice.
|
|
|
|
|
|
|
|
|
|
And this path operation will:
|
|
|
|
|
|
|
|
|
|
* Send the invoice to the client.
|
|
|
|
|
* Collect the money from the client.
|
|
|
|
|
* Send a notification back to the API user (the external developer), as a callback.
|
|
|
|
|
* At this point is that the API will somehow send a POST request to the
|
|
|
|
|
external API with the notification of the invoice event
|
|
|
|
|
(e.g. "payment successful").
|
|
|
|
|
"""
|
|
|
|
|
# Send the invoice, collect the money, send the notification (the callback)
|
|
|
|
|
return {"msg": "Invoice received"}
|