Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

How can I create a Pydantic Model for my FastAPI endpoint when my JSON is something like this?

{
  'events': [
    {
      'type': 'message',
      'replyToken': '0bc647fc5282423cde13fffbc947a8',
      'source': {
        'userId': 'U996ed69353d3c962ee17b33d9af3e2',
        'type': 'user'
      },
      'timestamp': 161185209914,
      'mode': 'active',
      'message': {
        'type': 'text',
        'id': '1346188304367',
        'text': ' hello'
      }
    }
  ],
  'destination': 'Uf44eb3ba6c4b87adbfaa4a517e'
}

this is the json from the webhook I'm using it's contained like this, how can I write a Pytantic model for it?

question from:https://stackoverflow.com/questions/65942184/how-can-i-create-a-pydantic-model-for-my-fastapi-endpoint-when-my-json-is-someth

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should work fine with the request body you given above.

from pydantic import BaseModel
from typing import List

class Source(BaseModel):
    userId: str
    type: str


class Message(BaseModel):
    type: str
    id: str
    text: str


class Event(BaseModel):
    type: str
    replyToken: str
    source: Source
    timestamp: int
    mode: str
    message: Message


class Webhook(BaseModel):
    events: List[Event]
    destination: str

Here is the OpenAPI schema for Webhook model.

{
  "title": "Webhook",
  "type": "object",
  "properties": {
    "events": {
      "title": "Events",
      "type": "array",
      "items": {
        "$ref": "#/definitions/Event"
      }
    },
    "destination": {
      "title": "Destination",
      "type": "string"
    }
  },
  "required": [
    "events",
    "destination"
  ],
  "definitions": {
    "Source": {
      "title": "Source",
      "type": "object",
      "properties": {
        "userId": {
          "title": "Userid",
          "type": "string"
        },
        "type": {
          "title": "Type",
          "type": "string"
        }
      },
      "required": [
        "userId",
        "type"
      ]
    },
    "Message": {
      "title": "Message",
      "type": "object",
      "properties": {
        "type": {
          "title": "Type",
          "type": "string"
        },
        "id": {
          "title": "Id",
          "type": "string"
        },
        "text": {
          "title": "Text",
          "type": "string"
        }
      },
      "required": [
        "type",
        "id",
        "text"
      ]
    },
    "Event": {
      "title": "Event",
      "type": "object",
      "properties": {
        "type": {
          "title": "Type",
          "type": "string"
        },
        "replyToken": {
          "title": "Replytoken",
          "type": "string"
        },
        "source": {
          "$ref": "#/definitions/Source"
        },
        "timestamp": {
          "title": "Timestamp",
          "type": "integer"
        },
        "mode": {
          "title": "Mode",
          "type": "string"
        },
        "message": {
          "$ref": "#/definitions/Message"
        }
      },
      "required": [
        "type",
        "replyToken",
        "source",
        "timestamp",
        "mode",
        "message"
      ]
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...