๐ โฒ¶
๐ ๐ช โ ๐ ๏ธ โฎ๏ธ โก ๐ ๏ธ ๐ ๐ช โฒ ๐จ ๐ข ๐ ๏ธ โ ๐ฑ ๐ (๐ฒ ๐ ๐ฉโ๐ป ๐ ๐ โ๏ธ ๐ ๐ ๏ธ).
๐ ๏ธ ๐ ๐จ ๐โ ๐ ๐ ๏ธ ๐ฑ ๐ค ๐ข ๐ ๏ธ ๐ "โฒ". โฉ๏ธ ๐ฅ ๐ ๐ข ๐ฉโ๐ป โ ๐จ ๐จ ๐ ๐ ๏ธ & โคด๏ธ ๐ ๐ ๏ธ ๐ค ๐, ๐จ ๐จ ๐ข ๐ ๏ธ (๐ ๐ฒ โ ๐ ๐ฉโ๐ป).
๐ ๐ผ, ๐ ๐ช ๐ ๐ โ ๐ ๐ข ๐ ๏ธ ๐ ๐ ๐. โซ๏ธโ โก ๐ ๏ธ โซ๏ธ ๐ โ๏ธ, โซ๏ธโ ๐ช โซ๏ธ ๐ โ, โซ๏ธโ ๐จ โซ๏ธ ๐ ๐จ, โ๏ธ.
๐ฑ โฎ๏ธ โฒ¶
โก๏ธ ๐ ๐ ๐ โฎ๏ธ ๐ผ.
๐ ๐ ๐ ๏ธ ๐ฑ ๐ โ ๐ ๐งพ.
๐ ๐งพ ๐ โ๏ธ id
, title
(๐ฆ), customer
, & total
.
๐ฉโ๐ป ๐ ๐ ๏ธ (๐ข ๐ฉโ๐ป) ๐ โ ๐งพ ๐ ๐ ๏ธ โฎ๏ธ ๐ค ๐จ.
โคด๏ธ ๐ ๐ ๏ธ ๐ (โก๏ธ ๐):
- ๐จ ๐งพ ๐ด ๐ข ๐ฉโ๐ป.
- ๐ ๐ธ.
- ๐จ ๐จ ๐ ๐ ๏ธ ๐ฉโ๐ป (๐ข ๐ฉโ๐ป).
- ๐ ๐ ๐จ ๐จ ๐ค ๐จ (โช๏ธโก๏ธ ๐ ๐ ๏ธ) ๐ข ๐ ๏ธ ๐ ๐ ๐ข ๐ฉโ๐ป (๐ "โฒ").
๐ FastAPI ๐ฑ¶
โก๏ธ ๐ฅ ๐ โ ๐ ๐ ๏ธ ๐ฑ ๐ ๐ ๐ โญ โ โฒ.
โซ๏ธ ๐ โ๏ธ โก ๐ ๏ธ ๐ ๐ ๐จ Invoice
๐ช, & ๐ข ๐ข callback_url
๐ ๐ ๐ ๐ โฒ.
๐ ๐ ๐ถ ๐, ๐ ๐ ๐ฒ โช ๐ฐ ๐:
from typing import Union
from fastapi import APIRouter, FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Invoice(BaseModel):
id: str
title: Union[str, None] = None
customer: str
total: float
class InvoiceEvent(BaseModel):
description: str
paid: bool
class InvoiceEventReceived(BaseModel):
ok: bool
invoices_callback_router = APIRouter()
@invoices_callback_router.post(
"{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
)
def invoice_notification(body: InvoiceEvent):
pass
@app.post("/invoices/", callbacks=invoices_callback_router.routes)
def create_invoice(invoice: Invoice, callback_url: Union[HttpUrl, None] = None):
"""
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"}
Tip
callback_url
๐ข ๐ข โ๏ธ Pydantic ๐ ๐.
๐ด ๐ ๐ callbacks=messages_callback_router.routes
โ โก ๐ ๏ธ ๐จโ๐จ. ๐ฅ ๐ ๐ โซ๏ธโ ๐ โญ.
๐ฌ โฒ¶
โ โฒ ๐ ๐ ๐ช ๐ ๐ ๐ ๐ ๐ ๏ธ ๐ฑ.
& โซ๏ธ ๐ ๐ฒ ๐ช ๐ โช๏ธโก๏ธ 1๏ธโฃ ๐ฑ โญ.
โซ๏ธ ๐ช 1๏ธโฃ โ๏ธ 2๏ธโฃ โธ ๐, ๐:
callback_url = "https://example.com/api/v1/invoices/events/"
httpx.post(callback_url, json={"description": "Invoice paid", "paid": True})
โ๏ธ ๐ฒ ๐ โ ๐ โฒ โ ๐ญ ๐ ๐ ๐ ๏ธ ๐ฉโ๐ป (๐ข ๐ฉโ๐ป) ๐ ๏ธ ๐ข ๐ ๏ธ โ, ๐ ๐ฝ ๐ ๐ ๐ ๏ธ ๐ ๐จ ๐จ ๐ช โฒ, โ๏ธ.
, โซ๏ธโ ๐ฅ ๐ โญ ๐ฎ ๐ ๐ โ ๐ ๐ข ๐ ๏ธ ๐ ๐ ๐ ๐จ โฒ โช๏ธโก๏ธ ๐ ๐ ๏ธ.
๐ ๐งพ ๐ ๐ฆ ๐ ๐ฆ ๐ /docs
๐ ๐ ๏ธ, & โซ๏ธ ๐ โก๏ธ ๐ข ๐ฉโ๐ป ๐ญ โ ๐ ๐ข ๐ ๏ธ.
๐ ๐ผ ๐ซ ๐ ๏ธ โฒ โซ๏ธ (๐ ๐ช โธ ๐), ๐ด ๐งพ ๐.
Tip
โ โฒ ๐บ๐ธ๐ ๐จ.
๐โ ๐ ๏ธ โฒ ๐, ๐ ๐ช โ๏ธ ๐ณ ๐ ๐ธ๐ฒ โ๏ธ ๐จ.
โ โฒ ๐งพ ๐¶
๐ ๐ ๐ ๐ซ ๐ ๏ธ ๐ ๐ฑ, ๐ฅ ๐ด ๐ช โซ๏ธ ๐ โ ๐ ๐ข ๐ ๏ธ ๐ ๐ ๐.
โ๏ธ, ๐ โช ๐ญ โ ๐ช โ ๐ง ๐งพ ๐ ๏ธ โฎ๏ธ FastAPI.
๐ฅ ๐ โ๏ธ ๐ ๐ ๐ก ๐ โ ๐ข ๐ ๏ธ ๐ ๐ ๐... ๐ โก ๐ ๏ธ(โ) ๐ ๐ข ๐ ๏ธ ๐ ๐ ๏ธ (๐ ๐ ๐ ๏ธ ๐ ๐ค).
Tip
๐โ โ ๐ ๐ โฒ, โซ๏ธ ๐ช โ ๐ ๐ ๐ ๐ ๐ข ๐ฉโ๐ป. & ๐ ๐ โณ ๐ ๏ธ ๐ข ๐ ๏ธ, ๐ซ ๐ ๐ ๏ธ.
๐ ๐ ๏ธ ๐ โ ๐ ( ๐ข ๐ฉโ๐ป) ๐ช โน ๐ ๐ญ ๐ โซ๏ธ ๐ โญ ๐โ ๐ฎ ๐ข, Pydantic ๐ท ๐ช, ๐จ, โ๏ธ. ๐ ๐ข ๐ ๏ธ.
โ โฒ APIRouter
¶
๐ฅ โ ๐ APIRouter
๐ ๐ ๐ 1๏ธโฃ โ๏ธ ๐
โฒ.
from typing import Union
from fastapi import APIRouter, FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Invoice(BaseModel):
id: str
title: Union[str, None] = None
customer: str
total: float
class InvoiceEvent(BaseModel):
description: str
paid: bool
class InvoiceEventReceived(BaseModel):
ok: bool
invoices_callback_router = APIRouter()
@invoices_callback_router.post(
"{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
)
def invoice_notification(body: InvoiceEvent):
pass
@app.post("/invoices/", callbacks=invoices_callback_router.routes)
def create_invoice(invoice: Invoice, callback_url: Union[HttpUrl, None] = None):
"""
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"}
โ โฒ โก ๐ ๏ธ¶
โ โฒ โก ๐ ๏ธ โ๏ธ ๐ APIRouter
๐ โ ๐.
โซ๏ธ ๐ ๐ ๐ ๐ FastAPI โก ๐ ๏ธ:
- โซ๏ธ ๐ ๐ฒ โ๏ธ ๐ ๐ช โซ๏ธ ๐ ๐จ, โ
body: InvoiceEvent
. - & โซ๏ธ ๐ช โ๏ธ ๐ ๐จ โซ๏ธ ๐ ๐จ, โ
response_model=InvoiceEventReceived
.
from typing import Union
from fastapi import APIRouter, FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Invoice(BaseModel):
id: str
title: Union[str, None] = None
customer: str
total: float
class InvoiceEvent(BaseModel):
description: str
paid: bool
class InvoiceEventReceived(BaseModel):
ok: bool
invoices_callback_router = APIRouter()
@invoices_callback_router.post(
"{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
)
def invoice_notification(body: InvoiceEvent):
pass
@app.post("/invoices/", callbacks=invoices_callback_router.routes)
def create_invoice(invoice: Invoice, callback_url: Union[HttpUrl, None] = None):
"""
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"}
๐ค 2๏ธโฃ ๐ ๐บ โช๏ธโก๏ธ ๐ โก ๐ ๏ธ:
- โซ๏ธ ๐ซ ๐ช โ๏ธ ๐ โ ๐, โฉ๏ธ ๐ ๐ฑ ๐ ๐
๐ค ๐ ๐. โซ๏ธ ๐ด โ๏ธ ๐ ๐ข ๐ ๏ธ. , ๐ข ๐ช โ๏ธ
pass
. - โก ๐ช ๐ ๐ 3๏ธโฃ ๐งฌ (๐ ๐ ๐) ๐โ โซ๏ธ ๐ช โ๏ธ ๐ข โฎ๏ธ ๐ข & ๐ โฎ๏ธ ๐จ ๐จ ๐ ๐ ๏ธ.
โฒ โก ๐งฌ¶
โฒ โก ๐ช โ๏ธ ๐ 3๏ธโฃ ๐งฌ ๐ ๐ช ๐ ๐ โฎ๏ธ ๐จ ๐จ ๐ ๐ ๏ธ.
๐ ๐ผ, โซ๏ธ str
:
"{$callback_url}/invoices/{$request.body.id}"
, ๐ฅ ๐ ๐ ๏ธ ๐ฉโ๐ป (๐ข ๐ฉโ๐ป) ๐จ ๐จ ๐ ๐ ๏ธ :
https://yourapi.com/invoices/?callback_url=https://www.external.org/events
โฎ๏ธ ๐ป ๐ช:
{
"id": "2expen51ve",
"customer": "Mr. Richie Rich",
"total": "9999"
}
โคด๏ธ ๐ ๐ ๏ธ ๐ ๐ ๏ธ ๐งพ, & โ โช, ๐จ โฒ ๐จ callback_url
( ๐ข ๐ ๏ธ):
https://www.external.org/events/invoices/2expen51ve
โฎ๏ธ ๐ป ๐ช โ ๐ณ ๐:
{
"description": "Payment celebration",
"paid": true
}
& โซ๏ธ ๐ โ ๐จ โช๏ธโก๏ธ ๐ ๐ข ๐ ๏ธ โฎ๏ธ ๐ป ๐ช ๐:
{
"ok": true
}
Tip
๐ โ โฒ ๐ โ๏ธ ๐ ๐ ๐จ ๐ข ๐ข callback_url
(https://www.external.org/events
) & ๐งพ id
โช๏ธโก๏ธ ๐ ๐ป ๐ช (2expen51ve
).
๐ฎ โฒ ๐ป¶
๐ โ ๐ โ๏ธ โฒ โก ๐ ๏ธ(โ) ๐ช (1๏ธโฃ(โ) ๐ ๐ข ๐ฉโ๐ป ๐ ๐ ๏ธ ๐ข ๐ ๏ธ) โฒ ๐ป ๐ โ ๐.
๐ โ๏ธ ๐ข callbacks
๐ ๐ ๏ธ โก ๐ ๏ธ ๐จโ๐จ ๐ถโโ๏ธ ๐ข .routes
(๐ ๐ค list
๐ฃ/โก ๐ ๏ธ) โช๏ธโก๏ธ ๐ โฒ ๐ป:
from typing import Union
from fastapi import APIRouter, FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Invoice(BaseModel):
id: str
title: Union[str, None] = None
customer: str
total: float
class InvoiceEvent(BaseModel):
description: str
paid: bool
class InvoiceEventReceived(BaseModel):
ok: bool
invoices_callback_router = APIRouter()
@invoices_callback_router.post(
"{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
)
def invoice_notification(body: InvoiceEvent):
pass
@app.post("/invoices/", callbacks=invoices_callback_router.routes)
def create_invoice(invoice: Invoice, callback_url: Union[HttpUrl, None] = None):
"""
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"}
Tip
๐ ๐ ๐ ๐ซ ๐ถโโ๏ธ ๐ป โซ๏ธ (invoices_callback_router
) callback=
, โ๏ธ ๐ข .routes
, invoices_callback_router.routes
.
โ ๐ฉบ¶
๐ ๐ ๐ช โถ๏ธ ๐ ๐ฑ โฎ๏ธ Uvicorn & ๐ถ http://127.0.0.1:8000/docs.
๐ ๐ ๐ ๐ ๐ฉบ โ "โฒ" ๐ ๐ โก ๐ ๏ธ ๐ ๐ฆ โ ๐ข ๐ ๏ธ ๐ ๐ ๐: