【问题标题】:How can I create a Pydantic Model for my FastAPI endpoint when my JSON is something like this?当我的 JSON 是这样的时候,如何为我的 FastAPI 端点创建一个 Pydantic 模型?
【发布时间】:2021-05-02 15:32:26
【问题描述】:
{
  'events': [
    {
      'type': 'message',
      'replyToken': '0bc647fc5282423cde13fffbc947a8',
      'source': {
        'userId': 'U996ed69353d3c962ee17b33d9af3e2',
        'type': 'user'
      },
      'timestamp': 161185209914,
      'mode': 'active',
      'message': {
        'type': 'text',
        'id': '1346188304367',
        'text': ' hello'
      }
    }
  ],
  'destination': 'Uf44eb3ba6c4b87adbfaa4a517e'
}

这是我正在使用的 webhook 中的 json 它是这样包含的,我怎样才能为它写一个 Pytantic 模型?

【问题讨论】:

    标签: fastapi pydantic


    【解决方案1】:

    这应该适用于您上面给出的请求正文。

    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
    

    这是 Webhook 模型的 OpenAPI 架构。

    {
      "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"
          ]
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-18
      • 2019-04-16
      • 2021-09-24
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      相关资源
      最近更新 更多