Skip to main content

A file based router for FastAPI that helps with defining endpoints

Project description

FastAPI Endpoints

CI checks

This is a file-based router for FastAPI that automatically discovers and registers route files based on their filenames. This tool simplifies the organization and scaling of your FastAPI projects by allowing you to structure your endpoints in a modular way. With fastapi-endpoints, you can easily manage complex applications by keeping your routes clean, intuitive, and maintainable.

Installation

pip install fastapi-endpoints

Usage

Auto discovery

The auto discovery feature allows you to organize your routes in separate files and directories and will be automatically registered by the router. All routers should be under the routers directory within your FastAPI application.

# src/app.py

from fastapi import FastAPI
from fastapi_endpoints import auto_include_routers

from . import routers

app = FastAPI()
auto_include_routers(app, routers)
# src/routers/users.py
from fastapi import APIRouter

users_router = APIRouter()

# Define your routes here

The routes under src/routers/users.py will be automatically registered by the router. The prefix to the routes will be the path to the file relative to the routers directory. For the example above, the routes will be available under /users.

The auto discovery feature also supports nested directories. For example, if you have the following directory structure:

routers
|── __init__.py
├── api_v1
│   ├── __init__.py
│   ├── users.py
│   └── posts.py
└── api_v2
    ├── __init__.py
    ├── users.py
    └── posts.py
app.py

The routes under src/routers/api_v1/users.py will be available under /api/v1/users. The same applies to the other files. The routes under src/routers/api_v2/users.py will be available under /api/v2/users.

Exclude Routers

You can exclude routers from being registered by the router by defining the EXCLUDED_ROUTERS variable in the __init__.py file of any submodule of the routers module. All excluded routers will be bundled together and excluded from the registration process.

For example, if you have the following directory structure:

routers
|── __init__.py
├── api_v1
│   ├── __init__.py
│   ├── users.py
│   └── posts.py
app.py

You can exclude the users.py router from being registered by defining the EXCLUDED_ROUTERS variable in the api_v1/__init__.py file.

# src/routers/api_v1/__init__.py
from . import users

EXCLUDED_ROUTERS = [users]

You can also exclude an entire directory by defining the EXCLUDED_ROUTERS variable in the __init__.py file of the directory.

# src/routers/__init__.py
from . import api_v1

EXCLUDED_ROUTERS = [api_v1]

Development

You are required to have Poetry installed in your system to manage the dependencies.

Setup

poetry install

Running tests

pytest

Linting & Formatting

To check the format:

ruff format --check src/

To check the lint:

ruff check src/

After running both commands, the format errors can be applied by running the formatter without the --check flag.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_endpoints-0.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

fastapi_endpoints-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_endpoints-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi_endpoints-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.0 CPython/3.10.12 Linux/5.14.0-1051-oem

File hashes

Hashes for fastapi_endpoints-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bd642ec03fd045a766aa5a0de1a5ec43ff71734d2e98b57b0184cd593f39f2a1
MD5 6be1bc164c8086cb42b57e6950065bd9
BLAKE2b-256 385f5f772155f6d0ecbe3a6b2635ea32294fb982148d909278c8d7767788b1eb

See more details on using hashes here.

File details

Details for the file fastapi_endpoints-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fastapi_endpoints-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.0 CPython/3.10.12 Linux/5.14.0-1051-oem

File hashes

Hashes for fastapi_endpoints-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 122b95ea9448a804599f0738e310d3f32313d39be52900a8c43760acecc3b242
MD5 ffb9dc422a9159927e4b61302e4176c7
BLAKE2b-256 342c37ce394b2e39558f186518998c774bf4a644a08b52f39ac8ee04e8a5ad19

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page