Team Ai
Apppublic

openenv/coding_env

sourceHugging Faceupdated 3mo agoView on Hugging Face
21likes
route_config.py58 linesDownload Raw Back to env_server
1# Copyright (c) Meta Platforms, Inc. and affiliates.2# All rights reserved.3#4# This source code is licensed under the BSD-style license found in the5# LICENSE file in the root directory of this source tree.6 7"""8Route configuration utilities for declarative FastAPI route registration.9 10This module provides utilities to reduce boilerplate in route registration11by using configuration objects instead of repeated function calls.12"""13 14from dataclasses import dataclass15from typing import Callable, List, Type16 17from fastapi import FastAPI18from pydantic import BaseModel19 20 21@dataclass22class GetEndpointConfig:23    """Configuration for a simple GET endpoint."""24 25    path: str26    handler: Callable[[], BaseModel | dict]27    response_model: Type[BaseModel] | type[dict]28    tag: str29    summary: str30    description: str31 32 33def register_get_endpoints(app: FastAPI, configs: List[GetEndpointConfig]) -> None:34    """35    Register multiple GET endpoints from configuration.36 37    Args:38        app: FastAPI application instance39        configs: List of GET endpoint configurations40    """41    for config in configs:42        # Capture handler in a closure to avoid non-serializable default parameter43        def make_endpoint(44            handler: Callable[[], BaseModel | dict],45        ) -> Callable[[], BaseModel | dict]:46            async def endpoint() -> BaseModel | dict:47                return handler()48 49            return endpoint50 51        app.get(52            config.path,53            response_model=config.response_model,54            tags=[config.tag],55            summary=config.summary,56            description=config.description,57        )(make_endpoint(config.handler))58