Chapter03 Getting Started with FastAPI
11 Summary
FastAPI+React全栈开发11 开始使用FastAPI
Arguably the most important part of our FARM stack is going to be the application programming interface(API). The AIP is the brain of our system, it implements the business logic (how the data flows in and out of the system, but more importantly how it relates to the business requirements inside our system).
可以说,FARM堆栈中最重要的部分将是应用程序编程接口(API)。AIP是我们系统的大脑,它实现业务逻辑(数据如何流入和流出系统,但更重要的是它如何与系统内的业务需求相关联)。
I believe that frameworks such as FastAPI are much easier to showcase through simple examples. So, in this chapter, we will go through some simple endpoints that are minimal self-contained REST APIs. We will use them to get acquainted with how this powerful framework handles requests and responses, the cardiovascular system of the web.
我相信像FastAPI这样的框架更容易通过简单的例子来展示。因此,在本章中,我们将浏览一些简单的端点,它们是最小的自包含REST api。我们将使用它们来熟悉这个强大的框架如何处理请求和响应,web的心血管系统。
This chapter is meant to be a quick start introduction to the framework, here we will focus on standard REST API practices and how they are implemented in FastAPI. I will show how to send requests and modify them according to our needs, and I will explain how to retrieve all the data from HTTP requests, parameters and the body. We will see how easy it is to work with the response and how intuitive FastAPI is when it comes to setting cookies, headers, and other standard web-related topics.
本章旨在快速入门框架,这里我们将重点关注标准REST API实践以及它们如何在FastAPI中实现。我将展示如何发送请求并根据我们的需要修改它们,我将解释如何从HTTP请求、参数和主体中检索所有数据。我们将看到使用响应是多么容易,以及FastAPI在设置cookie、header和其他标准web相关主题时是多么直观。
In this chapter, we will cover the following topics:
- An overview of the FastAPI framework's main features
- Setup and requirements for a simple FastAPI app
- Specific Python features used in FastAPI (type hinting, annotations, and the async await syntax)
- How FastAPI handles typical REST API tasks, path and query parameters, the request and response body, headers, and cookies
- How to work with form data
- Anatomy of a FastAPI project and routers
在本章中,我们将讨论以下主题:
- FastAPI框架主要特性的概述
- 一个简单的FastAPI应用程序的设置和要求
- FastAPI中使用的特定Python特性(类型提示,注释和async await语法)
- FastAPI如何处理典型的REST API任务、路径和查询参数、请求和响应体、报头和cookie
- 如何使用表单数据
- FastAPI项目和路由器剖析
It is not easy to cover the basics of such a rich web framework in a relatively short number of pages. Rather than diving deep into specific topics, we covered very simple examples of how FastAPI achives the most common REST API tasks and the way it can help you, as a developer, by leveraging moder Python features and libraries such as Pydantic.
在相对较短的页数中涵盖如此丰富的web框架的基础知识并不容易。我们没有深入探讨具体的主题,而是介绍了一些非常简单的例子,说明FastAPI如何实现最常见的REST API任务,以及作为开发人员,它如何利用现代Python功能和库(如Pydantic)来帮助您。
In this chapter, you learned how FastAPI enables you to perform requests and responses through HTTP and how you can tap into it, at any point, and customize and access the elements of the request, as well as the response. We briefly addressed headers and cookies, query strings and paths, forms and files, and the main part of the cycle: the request and the response bodies.
在本章中,您了解了FastAPI如何使您能够通过HTTP执行请求和响应,以及如何在任何时候使用它,并自定义和访问请求以及响应的元素。我们简要地介绍了报头和cookie、查询字符串和路径、表单和文件,以及循环的主要部分:请求和响应体。
Finally, you learned how to split your API into routers and how to organize your app into logical resource-based units.
最后,您学习了如何将API拆分为路由器,以及如何将应用程序组织为基于逻辑资源的单元。
There are many features of the framework that we haven't mentioned and that you are highly likely to encounter in even the simplest web applications. In Chapter 5, Building the Backend for Our Application, when we begin developing our simple application with FastAPI and MongoDB, we will learn how to connect the API to a MongoDB database and when to use an asynchronous driver. There, we will learn how to split our API into logical units using routers and make extensive use of the FastAPI dependency injection system.
这个框架有很多我们没有提到的特性,即使是在最简单的web应用程序中,你也很可能会遇到这些特性。在第5章,为我们的应用程序构建后端,当我们开始使用FastAPI和MongoDB开发我们的简单应用程序时,我们将学习如何将API连接到MongoDB数据库以及何时使用异步驱动程序。在那里,我们将学习如何使用路由器将我们的API拆分为逻辑单元,并广泛使用FastAPI依赖注入系统。
In the next chapter, we will provide a quick introduction to React, our user interface library of choice. We will try to cover the absolute minimum necessary so that we can start using and discovering its incredibly powerful features and simplicity.
在下一章中,我们将快速介绍React,我们选择的用户界面库。我们将尝试涵盖绝对最小的必要,以便我们可以开始使用和发现其令人难以置信的强大功能和简单性。