python_template_server.template_server

Template FastAPI server module.

Classes

TemplateServer([package_name, api_prefix, ...])

Template FastAPI server.

class python_template_server.template_server.TemplateServer(package_name='python-template-server', api_prefix='/api', api_key_header_name='X-API-Key', config_filepath=PosixPath('/home/runner/work/python-template-server/python-template-server/configuration/config.json'), config=None, static_dir=PosixPath('/home/runner/work/python-template-server/python-template-server/static'))[source]

Template FastAPI server.

This class provides a template for building FastAPI servers with common features such as request logging, security headers and rate limiting.

Ensure you implement the setup_routes and validate_config methods in subclasses.

__init__(package_name='python-template-server', api_prefix='/api', api_key_header_name='X-API-Key', config_filepath=PosixPath('/home/runner/work/python-template-server/python-template-server/configuration/config.json'), config=None, static_dir=PosixPath('/home/runner/work/python-template-server/python-template-server/static'))[source]

Initialize the TemplateServer.

Parameters:
  • api_prefix (str) – The API prefix for the server

  • api_key_header_name (str) – The API key header name

  • config_filepath (Path) – Path to the configuration file

  • config (TemplateServerConfig | None) – Optional pre-loaded configuration

property static_dir_exists: bool

Check if the static directory exists.

Return bool:

True if the static directory exists, False otherwise

static lifespan(app)[source]

Handle application lifespan events.

Return type:

AsyncGenerator[None]

abstractmethod validate_config(config_data)[source]

Validate configuration data against the TemplateServerConfig model.

Parameters:

config_data (dict[str, Any]) – The configuration data to validate

Return TemplateServerConfig:

The validated configuration model

Raises:

ValidationError – If the configuration data is invalid

Return type:

TemplateServerConfig

load_config(config_filepath)[source]

Load configuration from the specified json file.

Parameters:

config_filepath (Path) – Path to the configuration file

Return TemplateServerConfig:

The validated configuration model

Raises:

SystemExit – If configuration file is missing, invalid JSON, or fails validation

Return type:

TemplateServerConfig

run()[source]

Run the server using uvicorn.

Return type:

None

add_unauthenticated_route(endpoint, handler_function, response_model, methods, limited=True)[source]

Add an unauthenticated API route.

Parameters:
  • endpoint (str) – The API endpoint path

  • handler_function (Callable) – The handler function for the endpoint

  • response_model (type[BaseModel] | None) – The Pydantic model for the response

  • methods (list[str]) – The HTTP methods for the endpoint

  • limited (bool) – Whether to apply rate limiting to this route

Return type:

None

add_authenticated_route(endpoint, handler_function, response_model, methods, limited=True)[source]

Add an authenticated API route.

Parameters:
  • endpoint (str) – The API endpoint path

  • handler_function (Callable) – The handler function for the endpoint

  • response_model (type[BaseModel]) – The Pydantic model for the response

  • methods (list[str]) – The HTTP methods for the endpoint

  • limited (bool) – Whether to apply rate limiting to this route

Return type:

None

abstractmethod setup_routes()[source]

Add custom API routes.

This method must be implemented by subclasses to define API endpoints using add_unauthenticated_route and add_authenticated_route.

Return type:

None

async get_health(request)[source]

Get server health.

Parameters:

request (Request) – The incoming HTTP request

Return GetHealthResponse:

Health status response

Raises:

HTTPException – If the server token is not configured

Return type:

GetHealthResponse

async get_login(request)[source]

Handle user login and return a success response.

Parameters:

request (Request) – The incoming HTTP request

Return GetLoginResponse:

Login success response

Raises:

HTTPException – If the server token is not configured

Return type:

GetLoginResponse