Auto-Generated API Reference¶
This page provides auto-generated documentation from the source code docstrings using mkdocstrings.
For hand-written endpoint guides, see the other pages in the API Reference section.
Configuration¶
file_organizer.api.config ¶
API configuration and settings loader.
Classes¶
ApiSettings ¶
Bases: BaseModel
Settings for the FastAPI backend.
Functions¶
load_settings() ¶
Load API settings from a config file and environment variables.
Environment variables override config file values when present.
Source code in src/file_organizer/api/config.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | |
Authentication¶
file_organizer.api.auth ¶
Authentication helpers for JWT and password hashing.
Classes¶
TokenError ¶
Bases: Exception
Raised when a JWT token is invalid.
TokenBundle(access_token, refresh_token, access_jti, refresh_jti, access_expires_at, refresh_expires_at) dataclass ¶
Bundle of access and refresh tokens.
Functions¶
verify_password(plain_password, hashed_password) ¶
Return True if plain_password matches hashed_password.
hash_password(password) ¶
validate_password(password, settings) ¶
Validate password strength based on API settings.
Source code in src/file_organizer/api/auth.py
create_token_bundle(user_id, username, settings) ¶
Create a new access and refresh token bundle for a user.
Source code in src/file_organizer/api/auth.py
decode_token(token, settings) ¶
Decode and return the JWT payload, raising TokenError if invalid.
Source code in src/file_organizer/api/auth.py
is_access_token(payload) ¶
API Key Management¶
file_organizer.api.api_keys ¶
API key helpers for external integrations.
Functions¶
generate_api_key(prefix='fo') ¶
hash_api_key(api_key) ¶
match_api_key_hash(api_key, hashes) ¶
Return the stored hash matching an API key, if any.
Source code in src/file_organizer/api/api_keys.py
verify_api_key(api_key, hashes) ¶
api_key_identifier(api_key, hashes) ¶
Return a stable identifier derived from the stored hash.
Source code in src/file_organizer/api/api_keys.py
Rate Limiting¶
file_organizer.api.rate_limit ¶
Rate limiting helpers for API requests.
Classes¶
RateLimitResult(allowed, remaining, reset_at) dataclass ¶
Result of a rate limit check.
RateLimitState(count, reset_at) dataclass ¶
Mutable rate limit state for a single key.
InMemoryRateLimiter(max_entries=10000, sweep_interval_seconds=60) ¶
Simple in-memory fixed-window rate limiter.
Initialize InMemoryRateLimiter with given capacity and sweep interval.
Source code in src/file_organizer/api/rate_limit.py
Functions¶
check(key, limit, window_seconds) ¶
Check rate limit for key and return the result.
Source code in src/file_organizer/api/rate_limit.py
RedisRateLimiter(redis, prefix='ratelimit:') ¶
Redis-backed fixed-window rate limiter.
Initialize RedisRateLimiter with a Redis client and key prefix.
Source code in src/file_organizer/api/rate_limit.py
Functions¶
check(key, limit, window_seconds) ¶
Check rate limit for key against Redis and return the result.
Source code in src/file_organizer/api/rate_limit.py
Functions¶
build_rate_limiter(redis_url) ¶
Create a rate limiter instance.
Source code in src/file_organizer/api/rate_limit.py
Authentication Rate Limiting¶
file_organizer.api.auth_rate_limit ¶
Login rate limiting helpers.
Classes¶
LoginRateLimiter ¶
RateLimitState(count, expires_at) dataclass ¶
Track rate limit count and expiry for a key.
InMemoryLoginRateLimiter(max_attempts, window_seconds, _state=dict()) dataclass ¶
In-memory fixed-window rate limiter for login attempts.
Functions¶
is_blocked(key) ¶
Return whether the key is currently blocked and retry-after seconds.
Source code in src/file_organizer/api/auth_rate_limit.py
record_failure(key) ¶
Record a failed attempt and return blocked status and retry-after seconds.
Source code in src/file_organizer/api/auth_rate_limit.py
RedisLoginRateLimiter(redis, max_attempts, window_seconds, prefix='auth:login:') dataclass ¶
Redis-backed fixed-window login rate limiter.
Functions¶
is_blocked(key) ¶
Return whether the key is currently blocked and retry-after seconds.
Source code in src/file_organizer/api/auth_rate_limit.py
record_failure(key) ¶
Record a failed attempt and return blocked status and retry-after seconds.
Source code in src/file_organizer/api/auth_rate_limit.py
Functions¶
build_login_rate_limiter(redis_url, max_attempts, window_seconds) ¶
Create a login rate limiter, preferring Redis when configured.
Source code in src/file_organizer/api/auth_rate_limit.py
Token Store¶
file_organizer.api.auth_store ¶
Token storage for authentication sessions.
Classes¶
TokenStore ¶
InMemoryTokenStore() ¶
Simple in-memory token store for testing or local fallback.
Initialize InMemoryTokenStore with empty refresh and revoked buckets.
Source code in src/file_organizer/api/auth_store.py
RedisTokenStore(redis, refresh_prefix='auth:refresh:', revoked_prefix='auth:revoked:') dataclass ¶
Functions¶
build_token_store(redis_url) ¶
Create a token store, preferring Redis when configured.
Source code in src/file_organizer/api/auth_store.py
Caching¶
file_organizer.api.cache ¶
Cache abstraction for API persistence layers.
Provides a small key/value interface with an in-memory implementation and an optional Redis backend.
Classes¶
CacheBackend ¶
InMemoryCache() ¶
In-process TTL cache implementation.
Thread-safe: all access to _entries is protected by a lock.
Initialize InMemoryCache with empty entries dict.
Source code in src/file_organizer/api/cache.py
RedisCache(redis_url) ¶
Redis-backed cache implementation.
Initialize RedisCache with a connection to redis_url.
Source code in src/file_organizer/api/cache.py
Functions¶
build_cache_backend(redis_url) ¶
Build a cache backend from configuration.
Falls back to in-memory cache when Redis is unavailable or connection validation fails.
Source code in src/file_organizer/api/cache.py
Middleware¶
file_organizer.api.middleware ¶
Middleware setup for the API layer.
Classes¶
RateLimitMiddleware(app, settings, limiter) ¶
Bases: BaseHTTPMiddleware
Apply rate limiting based on endpoint and client identity.
Initialize RateLimitMiddleware with app, settings, and limiter.
Source code in src/file_organizer/api/middleware.py
Functions¶
dispatch(request, call_next) async ¶
Process an HTTP request through the rate limiter.
Source code in src/file_organizer/api/middleware.py
SecurityHeadersMiddleware(app, settings) ¶
Bases: BaseHTTPMiddleware
Attach security headers to API responses.
Initialize SecurityHeadersMiddleware with app and settings.
Source code in src/file_organizer/api/middleware.py
Functions¶
dispatch(request, call_next) async ¶
Process an HTTP request and attach security headers to the response.
Source code in src/file_organizer/api/middleware.py
Functions¶
setup_middleware(app, settings) ¶
Configure middleware on the FastAPI app.
Source code in src/file_organizer/api/middleware.py
Dependencies¶
file_organizer.api.dependencies ¶
Dependency providers for the API layer.
Classes¶
AnonymousUser(id='anonymous', username='anonymous', email='anonymous@example.com', full_name=None, is_active=True, is_admin=True, created_at=(lambda: datetime.now(UTC))(), last_login=None) dataclass ¶
Anonymous user identity used when auth is disabled.
ApiKeyIdentity(id, username, email='api-key@example.com', full_name=None, is_active=True, is_admin=False, created_at=(lambda: datetime.now(UTC))(), last_login=None, auth_type='api_key') dataclass ¶
API key-based user identity.
Functions¶
get_settings() cached ¶
get_config_manager() cached ¶
Return a config manager, optionally overridden by FO_CONFIG_DIR.
get_db(settings=Depends(get_settings)) ¶
Yield a database session for auth data.
Source code in src/file_organizer/api/dependencies.py
get_token_store(settings=Depends(get_settings)) ¶
get_login_rate_limiter(settings=Depends(get_settings)) ¶
Return the login rate limiter for the current settings.
Source code in src/file_organizer/api/dependencies.py
get_current_user(request, token=Depends(oauth2_scheme), settings=Depends(get_settings), db=Depends(get_db), token_store=Depends(get_token_store)) ¶
Resolve and return the current authenticated user.
Source code in src/file_organizer/api/dependencies.py
get_current_active_user(user=Depends(get_current_user), settings=Depends(get_settings)) ¶
Return the current user, raising 400 if inactive.
Source code in src/file_organizer/api/dependencies.py
require_admin_user(user=Depends(get_current_active_user), settings=Depends(get_settings)) ¶
Return the current user, raising 403 if not an admin.
Source code in src/file_organizer/api/dependencies.py
Utilities¶
file_organizer.api.utils ¶
Shared helpers for API routers.
Classes¶
Functions¶
resolve_path(path_value, allowed_paths=None) ¶
Expand and normalize a filesystem path.
Source code in src/file_organizer/api/utils.py
is_hidden(path) ¶
file_info_from_path(path) ¶
Build FileInfo from a filesystem path, raising ApiError on failure.
Source code in src/file_organizer/api/utils.py
Exceptions¶
file_organizer.api.exceptions ¶
Exception handlers for the API layer.
Classes¶
ApiError(status_code, error, message, details=None) dataclass ¶
Functions¶
setup_exception_handlers(app) ¶
Register exception handlers on the app.
Source code in src/file_organizer/api/exceptions.py
Authentication Models¶
file_organizer.api.auth_models ¶
SQLAlchemy models for API authentication.
Server¶
file_organizer.api.main ¶
FastAPI application entrypoint.
Classes¶
Functions¶
configure_logging(settings) ¶
Configure structured logging to console and file.
Source code in src/file_organizer/api/main.py
create_app(settings=None) ¶
Create the FastAPI application.
Source code in src/file_organizer/api/main.py
get_app() ¶
Get or create the FastAPI application instance (thread-safe).
This function implements lazy initialization with thread safety to avoid: - Import-time side effects (creating .config directories) - Multiple app instances due to race conditions in multi-threaded contexts - Test isolation issues in concurrent test environments
The first call to this function will trigger app creation via create_app(). Subsequent calls return the cached instance. Thread-safe via lock protection.
Intended for: Test infrastructure, application startup hooks, ASGI servers with multiple worker threads
Returns:
| Type | Description |
|---|---|
FastAPI | The initialized and cached FastAPI application instance. |