Introduction: Why FastAPI for Our Code Review Assistant

Welcome to your first lesson in building a Code Review Assistant! In this course, you will learn how to create a backend system that can receive code changes, store them, and process them for review. The backend is the part of your application that runs on a server and handles things like saving data, running logic, and responding to requests from users or other programs.

For this project, we are using FastAPI. FastAPI is a modern Python web framework that makes it easy to build APIs quickly and efficiently. It is known for its speed, simplicity, and automatic documentation features. By the end of this lesson, you will have a working FastAPI backend that can accept code changes and store them in a database, which is the first step toward building your own code review assistant.

Setting Up a Basic FastAPI Application

Let’s start by creating a simple FastAPI application. This will be the foundation for everything else we build.

First, you need to import FastAPI and create an app instance:

  • FastAPI is the main class you use to create your application.
  • The title and version are optional, but they help describe your API.

Now, let’s add a simple endpoint to make sure our app is running. An endpoint is a URL path that your app responds to.

  • @app.get("/") tells FastAPI to run this function when someone visits the root URL (/) with a GET request.
  • The function returns a dictionary, which FastAPI automatically converts to JSON.

Output Example:

If you run this app and visit the root URL, you will see:

On CodeSignal, FastAPI and its dependencies are already installed, so you can focus on writing code. If you want to run this on your own computer, you would need to install FastAPI and a server like uvicorn.

Connecting to a Database with SQLAlchemy

To store code changes, we need a database. In this lesson, we’ll use SQLite because it’s simple and doesn’t require any setup. We’ll use SQLAlchemy to interact with the database from Python.

First, let’s set up the database connection:

  • create_engine creates a connection to the SQLite database file test.db.
  • SessionLocal is a factory for creating new database sessions.
  • Base is used to define our database models.

Next, we need a way to get a database session for each request:

  • This function creates a new session and ensures it is closed after use.
Defining Models and Schemas

Now, let’s define the data we want to store. We need to keep track of code changes (called "changesets") and the files that are part of each changeset.

Database Models

Here’s how we define the models using SQLAlchemy:

  • Changeset represents a group of code changes.
  • ChangesetFile represents a single file in a changeset.
  • Each model is a Python class that maps to a table in the database.

To create the tables, add:

Pydantic Schemas

When building APIs, it’s important to validate the data coming in and out. FastAPI uses Pydantic for this.

  • FileChangeRequest describes a single file change.
  • ChangesetRequest describes the whole changeset, including a list of files.
Creating the Changeset Submission Endpoint

Now, let’s build the main feature: an endpoint to submit a new changeset for review.

Here’s how you define the endpoint:

Let’s break down what’s happening:

  • @app.post("/api/changesets") creates a POST endpoint at /api/changesets.
  • The function takes a ChangesetRequest (the data sent by the user), a BackgroundTasks object, and a database session.
  • It creates a new Changeset and adds it to the database.
  • For each file in the request, it creates a ChangesetFile and adds it to the database.
  • It commits the changes to save them.
  • It starts a background task to process the review (the review logic is a placeholder for now).
  • It returns a message and the ID of the new changeset.

Example Output:

If you send a POST request with a new changeset, you will get a response like:

Summary and What’s Next

In this lesson, you learned how to:

  • Set up a FastAPI application as the backend for your Code Review Assistant.
  • Connect to a SQLite database using SQLAlchemy.
  • Define models for changesets and files, and use Pydantic schemas for validation.
  • Create an endpoint to submit new changesets and store them in the database.

These are the essential building blocks for your project. In the practice exercises that follow, you will get hands-on experience with each of these steps. This will help you reinforce what you’ve learned and prepare you for adding more features to your Code Review Assistant in future lessons.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal