Welcome back! In our previous lesson, you started to see how Dependency Injection (DI) is used in FastAPI through the use of the Depends function, particularly in the login endpoint. We didn't dive deeply into the concept at that time, but now it's time to explore it in detail.
In this lesson, we will explain what Dependency Injection is and why it's important in software development. By the end of this lesson, you'll understand how it helps to keep code clean and allows us to secure an endpoint using in FastAPI.
Dependency Injection might sound complicated, but it's a simple and powerful concept. Think of it like this: instead of a function creating everything it needs by itself, it receives those things from the outside. This way, the function doesn't worry about how to get what it needs; it just uses it. This leads to cleaner, more manageable code.
In FastAPI, we use the Depends function to handle Dependency Injection. Imagine having a function that needs certain pieces of information to work. Using Depends, we can provide those pieces of information from outside the function, making our code more organized and easier to test.
Let's quickly recap what we covered last time. We created a FastAPI app, set up a mock database, and implemented a user authentication function along with a login endpoint.
Here is a quick summary of the code:
In our login endpoint, we briefly used Dependency Injection with the Depends function. Here's how we did it:
-
Depends(): In the login endpoint, we usedDepends()to get an instance ofOAuth2PasswordRequestForm, which contains the username and password. -
Parameter Injection: Instead of manually extracting username and password from the request,
Dependsautomatically handled it for us and injectedform_datadirectly into the endpoint function. -
Cleaner Code: This allowed our
loginfunction to focus on authenticating the user without worrying about how to extract the credentials from the request.
This initial use of Depends made our endpoint simpler and more declarative. Now, we'll build on this concept to secure our endpoints further.
Now, let's translate the use of Dependency Injection from our login form data to securing an endpoint. We'll use Depends to ensure only authenticated users can access a certain endpoint.
The endpoint function read_secure_message takes three parameters:
- username: The username provided by the user as a query parameter
- password: The password provided by the user as a query parameter
- user: A dictionary that holds the result of the
authenticate_userfunction
In this endpoint, we use Depends(authenticate_user) for Dependency Injection:
- Explicit Dependency: By using
Depends(authenticate_user), we ensure FastAPI will callauthenticate_userusing theusernameandpasswordfrom the query parameters. - Parameter Passing: When a request is made, FastAPI extracts
usernameandpasswordfrom the query parameters and passes them toauthenticate_user. - Injection: The result of
authenticate_useris then injected into theuserparameter of theread_secure_messagefunction.
This approach ensures our secure endpoint's user parameter contains the result of the authentication check, allowing only authenticated users access to the endpoint.
Here's the flow when a request is made to the /secure-message endpoint:
-
Input Request: The client sends a request with
usernameandpasswordas query parameters. -
Dependency Injection: FastAPI uses
Dependsto callauthenticate_userwith the provided credentials. -
Authentication Process in
authenticate_user:- If the credentials match an entry in
users_db, it returns{"username": username}. - If not, it raises
HTTPExceptionwith a 401 status code.
- If the credentials match an entry in
-
Response Handling: Once
authenticate_userreturns a value, it's injected into theuserparameter of theread_secure_messagefunction. This confirms the user is authenticated, allowing the endpoint to return the secured message.
In this lesson, we introduced Dependency Injection and its benefits. You learned how to use FastAPI's Depends function to secure an endpoint, ensuring only authenticated users can access it. We walked through the complete solution code and explained each part to make the concept clear.
Now, it's time to practice what you've learned! You will work on exercises designed to help you master securing endpoints using Dependency Injection. By now, you should feel confident in implementing basic user authentication and securing endpoints in FastAPI.
