Introduction
Part 1, Chapter 1
App Overview
By the end of this course, you will have built an asynchronous text summarization API with Test-Driven Development. The API itself will follow RESTful design principles, using the basic HTTP verbs: GET, POST, PUT, and DELETE.
Endpoint | HTTP Method | CRUD Method | Result |
---|---|---|---|
/summaries | GET | READ | get all summaries |
/summaries/:id | GET | READ | get a single summary |
/summaries | POST | CREATE | add a summary |
/summaries/:id | PUT | UPDATE | update a summary |
/summaries/:id | DELETE | DELETE | delete a summary |
Along with Python and FastAPI, we'll use Docker to quickly set up our local development environment and simplify deployment. Tortoise ORM, an async ORM (Object Relational Mapper), will be used to interact with a Postgres database. We'll use pytest instead of unittest for writing unit and integration tests to test the API. Finally, we'll store the code on a GitHub repository and utilize GitHub Actions to run tests before deploying to Heroku.
Before diving in, let's take a minute to go over why some of the above tools are being used.
FastAPI
While Flask and Django are the two most popular Python web frameworks, FastAPI has surged in popularity since 2019 since it supports async out-of-the box and has an advanced data validation feature based on Python type hints.
Put simply, FastAPI is a modern, batteries-included Python web framework that's perfect for building RESTful APIs. It can handle both synchronous and asynchronous requests and has built-in support for data validation, JSON serialization, authentication and authorization, and OpenAPI (version 3.1.0 as of writing) documentation.
Highlights:
- Heavily inspired by Flask, it has a lightweight microframework feel with support for Flask-like route decorators.
- It takes advantage of Python type hints for parameter declaration which enables data validation (via pydantic) and OpenAPI/Swagger documentation.
- Built on top of Starlette, it supports the development of asynchronous APIs.
- It's fast. Since async is much more efficient than the traditional synchronous threading model, it can compete with Node and Go with regards to performance.
- Because it's based on and fully compatible with OpenAPI and JSON Schema, it supports a number of powerful tools, like Swagger UI.
- It has amazing documentation.
Review the Features guide from the official docs for more info. It's also encouraged to review Alternatives, Inspiration, and Comparisons, which details how FastAPI compares to other web frameworks and technologies, for context.
Docker
Docker is a container platform used to streamline application development and deployment workflows across various environments. It's used to package application code, dependencies, and system tools into lightweight containers that can be moved from development machines to production servers quickly and easily.
Pytest
pytest is a test framework for Python that makes it easy (and fun!) to write, organize, and run tests. When compared to unittest, from the Python standard library, pytest:
- Requires less boilerplate code so your test suites will be more readable.
- Supports the plain
assert
statement, which is far more readable and easier to remember compared to theassertSomething
methods -- likeassertEquals
,assertTrue
, andassertContains
-- in unittest. - Is updated more frequently since it's not part of the Python standard library.
- Simplifies setting up and tearing down test state with its fixture system.
- Uses a functional approach.
Tortoise ORM
Tortoise ORM is a async ORM inspired by the Django ORM that's designed for ease of use. It's familiar constructs help make it easier for Python developers to switch over to the world of async.
Besides Tortoise, there are a number of other ORMs, query builders, and ODMs (Object Document Mapper) in active development that support async operations. Just keep in mind that the ecosystem is not mature yet, so things are moving quickly. Breaking changes are to be expected.
ORMs:
Query Builders:
ODMs:
GitHub
GitHub Actions is a continuous integration and delivery (CI/CD) solution, fully integrated with GitHub. Along with it, we'll also use GitHub Packages, a package management service, to store Docker images.
There are a number of platforms that include remote version control along with both CI and package management:
- GitLab
- AWS (CodeCommit, CodeBuild, CodeDeploy, ECR)
- GCP (Cloud Source Repositories, Cloud Build, Container Registry)
- Azure (Repos, Pipelines, Container Registry)
Each of them have similar features and pricing models so you really can't go wrong with any of them. If you're used to using a different platform, feel free to continue to use it if that's your preference. Or, check your understanding by trying a new platform.
Heroku
Heroku is a cloud Platform as a Service (PaaS) that provides hosting for web applications. They offer abstracted environments where you don't have to manage the underlying infrastructure, making it easy to manage, deploy, and scale web applications. With just a few clicks you can have your app up and running, ready to receive traffic.
✓ Mark as Completed