FastAPI 教程翻译 - 特征

FastAPI 教程翻译 - 特征

FastAPI Features

FastAPI gives you the following:

FastAPI 为您提供以下内容:

Based on open standards

基于开放标准

  • OpenAPI for API creation, including declarations of path operations, parameters, body requests, security, etc.

    用于 API 创建的 OpenAPI,包括声明路径操作、参数、请求体、安全性等。

  • Automatic data model documentation with JSON Schema (as OpenAPI itself is based on JSON Schema).

    具有 JSON模式 的自动数据模型文档(因为 OpenAPI 本身基于 JSON 模式)。

  • Designed around these standards, after a meticulous study. Instead of an afterthought layer on top.

    经过认真研究,围绕这些标准进行设计。而不是在其上层进行封装。

  • This also allows using automatic client code generation in many languages.

    这也允许使用多种语言的自动客户端代码生成器

Automatic docs

自动文档

Interactive API documentation and exploration web user interfaces. As the framework is based on OpenAPI, there are multiple options, 2 included by default.

交互式 API 文档和可探索的网络用户界面。由于该框架基于 OpenAPI,因此有多个选项,默认情况下包含 2 个。

  • Swagger UI, with interactive exploration, call and test your API directly from the browser.

    Swagger UI,具有交互式探索功能,可直接从浏览器调用和测试您的 API。

在这里插入图片描述

  • Alternative API documentation with ReDoc.

    带有 ReDoc 的替代 API 文档。

在这里插入图片描述

Just Modern Python

仅现代的 Python

It’s all based on standard Python 3.6 type declarations (thanks to Pydantic). No new syntax to learn. Just standard modern Python.

所有这些都基于标准的 Python 3.6 类型声明(感谢 Pydantic)。没有新的语法需要学习。只是标准的现代 Python。

If you need a 2 minute refresher of how to use Python types (even if you don’t use FastAPI), check the short tutorial: Python Types.

如果您需要 2 分钟的时间来重新学习如何使用 Python 类型(即使您不使用 FastAPI),请查看简短的教程:Python 类型介绍

You write standard Python with types:

您使用以下方式编写标准的 Python 类型:

pythonfrom typing import List, Dict
from datetime import date

from pydantic import BaseModel

# Declare a variable as a str
# and get editor support inside the function
def main(user_id: str):
    return user_id


# A Pydantic model
class User(BaseModel):
    id: int
    name: str
    joined: date

That can then be used like:

然后可以这样使用:

my_user: User = User(id=3, name="John Doe", joined="2018-07-19")

second_user_data = {
    "id": 4,
    "name": "Mary",
    "joined": "2018-11-30",
}

my_second_user: User = User(**second_user_data)

Info

信息

**second_user_data means:

**second_user_data 表示:

Pass the keys and values of the second_user_data dict directly as key-value arguments, equivalent to: User(id=4, name="Mary", joined="2018-11-30")

直接将 second_user_data 字典的值作为键值参数传递,等效于: User(id=4, name="Mary", joined="2018-11-30")

Editor support

编辑器支持

All the framework was designed to be easy and intuitive to use, all the decisions where tested on multiple editors even before starting development, to ensure the best development experience.

所有框架的设计都易于使用和直观,所有决定甚至在开始开发之前就已经在多个编辑器上进行了测试,以确保最佳的开发体验。

In the last Python developer survey it was clear that the most used feature is “autocompletion”.

在上一次 Python 开发人员调查中,很明显 最常用的功能是『自动完成』

The whole FastAPI framework is based to satisfy that. Autocompletion works everywhere.

整个 FastAPI 框架就是为了满足这一要求。自动完成功能无处不在。

You will rarely need to come back to the docs.

您将很少需要返回文档。

Here’s how your editor might help you:

您的编辑器可以为您提供以下帮助:

在这里插入图片描述

在这里插入图片描述

You will get completion in code you might even consider impossible before. As for example, the price key inside a JSON body (that could have been nested) that comes from a request.

您将获得以前甚至可能认为不可能完成的代码。例如,来自请求体的 JSON(可能是嵌套的)中的 price 键。

No more typing the wrong key names, coming back and forth between docs, or scrolling up and down to find if you finally used username or user_name.

无需再键入错误的键名,在文档之间来回切换或上下滚动来查找您最终是使用的 username 还是 user_name

Short

简短

It has sensible defaults for everything, with optional configurations everywhere. All the parameters can be fine-tuned to do what you need and to define the API you need.

它对所有内容都具有合理的默认值,随处可见可选配置。可以对所有参数进行微调,以执行所需的操作并定义所需的 API。

But by default, it all “just works”.

但默认情况下,所有这些都**『仅仅可以工作』**。

Validation

验证

  • Validation for most (or all?) Python data types, including:

    验证大多数(或全部?)Python 数据类型,包括:

    • JSON objects (dict).

      JSON 对象(dict)。

    • JSON array (list) defining item types.

      定义项类型的 JSON 数组(list)。

    • String (str) fields, defining min and max lengths.

      字符串(str)字段,定义最小和最大长度。

    • Numbers (int, float) with min and max values, etc.

      带有最小值和最大值等的数字(intfloat)。

  • Validation for more exotic types, like:

    验证更多外来类型,例如:

    • URL.
    • Email.
    • UUID.
    • …and others.

All the validation is handled by the well-established and robust Pydantic.

所有验证均由完善可靠的 Pydantic 处理。

Security and authentication

安全和身份验证

Security and authentication integrated. Without any compromise with databases or data models.

集成了安全性和身份验证。无需对数据库或数据模型进行任何妥协。

All the security schemes defined in OpenAPI, including:

OpenAPI 中定义的所有安全方案,包括:

  • HTTP Basic.

    HTTP 基础

  • OAuth2 (also with JWT tokens). Check the tutorial on OAuth2 with JWT.

    OAuth2(也包括 JWT 令牌)。查看教程上的 带 JWT 的 OAuth2

  • API keys in:

    API 密钥:

    • Headers.

    • Query parameters.

      查询参数。

    • Cookies, etc.

Plus all the security features from Starlette (including session cookies).
加上 Starlette 的所有安全功能(包括会话 cookies)。

All built as reusable tools and components that are easy to integrate with your systems, data stores, relational and NoSQL databases, etc.

所有这些都是可重用的工具和组件,可轻松与您的系统、数据存储、关系型数据库和非关系型数据库等集成。

Dependency Injection

依赖注入

FastAPI includes an extremely easy to use, but extremely powerful Dependency Injection system.

FastAPI 包括一个非常易于使用但功能非常强大的依赖注入系统。

  • Even dependencies can have dependencies, creating a hierarchy or “graph” of dependencies.

    即使依赖项也可以具有依赖项,从而创建依赖项的层次结构或“图”

  • All automatically handled by the framework.

    所有事务由框架自动处理

  • All the dependencies can require data from requests and augment the path operation constraints and automatic documentation.

    所有依赖项都可能需要来自请求的数据,并增强路径操作约束和自动文档。

  • Automatic validation even for path operation parameters defined in dependencies.

    即使对于依赖项中定义的路径操作参数,也可以进行自动验证

  • Support for complex user authentication systems, database connections, etc.

    支持复杂的用户身份验证系统,数据库连接等。

  • No compromise with databases, frontends, etc. But easy integration with all of them.

    对数据库,前端等没有任何倾斜,但易于与所有的它们进行集成。

Unlimited “plug-ins”

无限的『插件』

Or in other way, no need for them, import and use the code you need.

或者有其他的方式,不需要它们,导入并使用您所需的代码。

Any integration is designed to be so simple to use (with dependencies) that you can create a “plug-in” for your application in 2 lines of code using the same structure and syntax used for your path operations.

任何集成的设计都非常易于使用(具有依赖项),因此您可以使用路径操作所用的相同的结构和语法,用 2 行代码为应用程序创建一个『插件』。

Tested

经过的测试

  • 100% test coverage.

    100% 的测试覆盖率。

  • 100% type annotated code base.

    100% 类型注释的代码库。

  • Used in production applications.

    可用于生产的应用。

Starlette features

Starlette 特征

FastAPI is fully compatible with (and based on) Starlette. So, any additional Starlette code you have, will also work.

FastAPI 与 Starlette 完全兼容(并且基于)。因此,您拥有的任何其他 Starlette 代码也将起作用。

FastAPI is actually a sub-class of Starlette. So, if you already know or use Starlette, most of the functionality will work the same way.

FastAPI 实际上是 Starlette 的子类。因此,如果您已经了解或使用 Starlette,则大多数功能将以相同的方式工作。

With FastAPI you get all of Starlette’s features (as FastAPI is just Starlette on steroids):

使用 FastAPI,您可以获得 Starlette 的所有特征(因为 FastAPI 是 Starlette 的子类):

Pydantic features

Pydantic 特征

FastAPI is fully compatible with (and based on) Pydantic. So, any additional Pydantic code you have, will also work.

FastAPI 与 Pydantic 完全兼容(并且基于)。因此,您拥有的任何其他 Pydantic 代码也将起作用。

Including external libraries also based on Pydantic, as ORMs, ODMs for databases.

包括也基于 Pydantic 的外部库,例如 ORM、数据库 ODM。

This also means that in many cases you can pass the same object you get from a request directly to the database, as everything is validated automatically.

这也意味着在许多情况下,您可以将从请求中获得的同一对象直接传递给数据库,因为所有内容都是自动验证的。

The same applies the other way around, in many cases you can just pass the object you get from the database directly to the client.

在其他情况下也是如此,在许多情况下,您只需将从数据库中获取的对象直接传递给客户端即可。

With FastAPI you get all of Pydantic’s features (as FastAPI is based on Pydantic for all the data handling):

使用 FastAPI,您可以获得 Pydantic 的所有特征(因为 FastAPI 基于 Pydantic 进行所有数据处理):

  • No brainfuck:

    无需操心

    • No new schema definition micro-language to learn.

      无需学习新的架构定义微语言。

    • If you know Python types you know how to use Pydantic.

      如果您知道 Python 类型,就会知道如何使用 Pydantic。

  • Plays nicely with your IDE/linter/brain:

    与您的 IDE/linter/brain 完美搭配:

    • Because pydantic data structures are just instances of classes you define; auto-completion, linting, mypy and your intuition should all work properly with your validated data.

      因为 pydantic 数据结构只是您定义的类的实例;自动补全、linting、mypy 和您的直觉都应与验证的数据一起正常工作。

  • Fast:

    快速:

    • in benchmarks Pydantic is faster than all other tested libraries.

      benchmarks 中,Pydantic 比所有其他经过测试的库都快。

  • Validate complex structures:

    验证复杂结构

    • Use of hierarchical Pydantic models, Python typing’s List and Dict, etc.

      使用分层的 Pydantic 模型,Python 的类型 ListDict 等。

  • And validators allow complex data schemas to be clearly and easily defined, checked and documented as JSON Schema.

验证器允许将复杂的数据模式清晰并且轻松地定义、检查、记录为 JSON 模式。

  • You can have deeply nested JSON objects and have them all validated and annotated.

    您可以拥有嵌套 JSON 对象,并对其进行验证和注释。

  • Extendible:

可扩展

  • Pydantic allows custom data types to be defined or you can extend validation with methods on a model decorated with the validator decorator.

    Pydantic 允许定义自定义数据类型,或者您可以使用由验证器装饰器装饰的模型上的方法扩展验证。

  • 100% test coverage.

松地定义、检查、记录为 JSON 模式。

  • You can have deeply nested JSON objects and have them all validated and annotated.

    您可以拥有嵌套 JSON 对象,并对其进行验证和注释。

  • Extendible:

可扩展

  • Pydantic allows custom data types to be defined or you can extend validation with methods on a model decorated with the validator decorator.

    Pydantic 允许定义自定义数据类型,或者您可以使用由验证器装饰器装饰的模型上的方法扩展验证。

  • 100% test coverage.

    100% 的测试覆盖率。

fastapi-mysql-generator 是一个用于快速生成FastAPI和MySQL的框架的工具。FastAPI是一个现代、快速(高性能)的web框架,而MySQL是一个流行的关系型数据库。 使用 fastapi-mysql-generator,可以从一个简单的命令行界面中生成 FastAPI 应用程序,并与 MySQL 数据库集成。这个工具可以自动创建数据库表和模型(Model),并提供一组 API 端点,用于执行常见的 CRUD(创建、读取、更新和删除)操作。 fastapi-mysql-generator 的主要优势之一是它的简单易用性。无论是初学者还是有经验的开发人员,都可以快速上手并生成一个完整的基于FastAPI和MySQL的应用程序。只需要几个简单的步骤,就可以生成项目的基本结构和代码。同时,fastapi-mysql-generator 还提供了一些配置选项,可以根据需求进行自定义设置,以满足特定的应用程序需求。 这个工具还提供了一些有用的特性,以增强开发的效率和便利性。例如,它可以自动生成 API 文档,包括请求和响应模型的文档说明。此外,fastapi-mysql-generator 还支持身份验证和授权功能,以确保 API 路由的安全性。 总而言之,fastapi-mysql-generator 是一个快速生成 FastAPI 和 MySQL 应用程序的方便工具。它简化了应用程序的开发过程,并提供了一些有用的特性,以提高开发效率和便利性。无论是初学者还是有经验的开发人员,都可以受益于这个工具的使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值