Skip to main content

18 posts tagged with "fastapi"

fastapi tag description

View All Tags

Python Annotations Rare Use Cases

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Python annotations, introduced in PEP 3107 for function parameters and return values, were initially generic metadata slots. While their primary use has become type hinting (PEP 484), expert developers leverage them for advanced and niche applications that go far beyond simple type declarations.

These use cases often involve frameworks or metaprogramming to make annotations act as declarative configuration or runtime execution instructions.

FastAPI Core Middleware Examples and Use Cases

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Middleware in FastAPI (which is built on Starlette) is a powerful mechanism for executing code before and after a request is processed by the route handler. Middleware allows you to apply logic globally or across a group of routes, such as logging, authentication, CORS, and response manipulation.

The standard way to implement custom middleware is by defining an async function that takes the request and the call_next callable.

Fastapi Depends with parameters and arguments

· 8 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

FastAPI's Dependency Injection (DI) system is remarkably flexible: dependency functions aren't just executed independently; they can be designed to accept and process arguments from the current HTTP request or the results of other dependencies.

Using parameters within a Depends function allows you to implement complex logic, dynamic configuration, and validation rules while keeping your main route handler clean.

FastAPI Depends and the Request Object

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

While dependencies primarily focus on injecting services or validated data, they can also gain direct access to the raw HTTP request object. This is an advanced technique useful for accessing metadata, non-standard headers, client information, or the complete request body/form data before it's processed by the route.

To access the request object, you simply declare a parameter with the type hint Request in your dependency function. FastAPI's Dependency Injection system automatically resolves the current request and injects it.

Internal HTTP request from one FastAPI route handler to another

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Reliable way to make an internal HTTP request from one FastAPI route handler to another within the same application, specifically to handle a POST request with a request body.

The best and most idiomatic way to handle this in FastAPI (which is built on Starlette) is by using the TestClient from the starlette.testclient module. This allows you to treat your application as an independent service and make internal requests without incurring any actual network overhead, which is crucial for testing and internal service calls.

When working with FastAPI, the correct method to call one route from another is not by importing the handler function directly, but by using the TestClient class. This simulates a genuine HTTP request, ensuring all middleware, dependencies, and validation logic run exactly as they would for an external client.

Consuming Path Arguments Directly in FastAPI Dependency Functions

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

The short answer is: Yes, it is fundamentally possible and encouraged to pass Path arguments directly into FastAPI dependency functions.

This feature is a core component of FastAPI's Dependency Injection (DI) system, allowing dependencies to be highly contextual. A dependency can perform validation, authentication, or data fetching based on the URL or request inputs before the route handler ever executes.

FastAPI Dependency Injection (DI) VS. Depends

· 4 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

That's an excellent question that gets right to the core of FastAPI's design.

Yes, FastAPI Dependency Injection (DI) and FastAPI Depends are two different but closely related concepts. You can think of them as the system and the tool used to activate that system.

Here's a breakdown of the difference:

Testing FastAPI Dependency Injection: Where to Start

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

For beginners, testing FastAPI dependencies should start with one fundamental goal: isolation. You must ensure your route logic is tested without making real network calls, hitting a live database, or relying on complex configuration settings.

The key to achieving this is using FastAPI's built-in app.dependency_overrides dictionary. This allows you to replace any real dependency function with a simple, predictable mock function for the duration of a test.

Testing FastAPI Dependency Injection: A Comprehensive Guide

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Testing code that uses Dependency Injection (DI) is crucial because it allows you to isolate the logic of your route handlers from the complexity of external services (like databases, security checks, or configuration). FastAPI makes this straightforward using the app.dependency_overrides dictionary.

By implementing overrides, you replace the original, complex dependency function with a simple mock function that returns predictable test data, ensuring your tests run fast and consistently.

Advanced FastAPI Dependency Injection for Experts

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

For expert programmers, FastAPI Dependency Injection (DI) is not just about injecting configuration; it's a foundational tool for managing application state, enforcing transaction integrity, and implementing complex authorization logic cleanly and reliably. These advanced patterns leverage yield and nested dependencies to ensure stability and separation of concerns.