OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to understand the callback mechanism of fastapi?

  • Thread starter Thread starter hbx
  • Start date Start date
H

hbx

Guest
How to understand the callback mechanism of fastapi? The official document gives the following example

Code:
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"}

I try to request the route localhost:port/invoices/ with post method, but "{$callback_url}/invoices/{$request.body.id}" this route has not be requested, I don't know what it's for.

hope can help me to understand the callback mechanism of fastapi, it is best to use the example above to explain the role of callback routing, thanks
<p>How to understand the callback mechanism of fastapi? The official document gives the following example</p>
<pre class="lang-py prettyprint-override"><code>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"}
</code></pre>
<p>I try to request the route <code>localhost:port/invoices/</code> with post method, but "{$callback_url}/invoices/{$request.body.id}" this route has not be requested, I don't know what it's for.</p>
<p>hope can help me to understand the callback mechanism of fastapi, it is best to use the example above to explain the role of callback routing, thanks</p>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top