mirror of
https://github.com/fastapi/fastapi.git
synced 2026-11-23 01:48:43 +08:00
38 lines
766 B
Python
38 lines
766 B
Python
from typing import Literal
|
|
|
|
from fastapi import FastAPI, Form
|
|
from pydantic import BaseModel
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class Person(BaseModel):
|
|
contact_type: Literal["person"]
|
|
name: str
|
|
|
|
|
|
class Company(BaseModel):
|
|
contact_type: Literal["company"]
|
|
company_name: str
|
|
|
|
|
|
class Email(BaseModel):
|
|
notification_type: Literal["email"]
|
|
email: str
|
|
|
|
|
|
class SMS(BaseModel):
|
|
notification_type: Literal["sms"]
|
|
phone: str
|
|
|
|
|
|
@app.post("/contacts/")
|
|
async def create_contact(
|
|
contact: Person | Company = Form(discriminator="contact_type"),
|
|
token: str = Form(),
|
|
notification: Email | SMS | None = Form(
|
|
default=None, discriminator="notification_type"
|
|
),
|
|
):
|
|
return {"contact": contact, "notification": notification, "token": token}
|