apifrom.decorators

Decorators for the APIFromAnything library.

This package provides decorators that can be used to enhance API endpoints with additional functionality.

Overview

Classes

  • Web

  • WebOptimize

Functions

Classes

class apifrom.decorators.Web(title=None, description=None, theme='default', template=None)[source]
Parameters:
  • title (Optional[str])

  • description (Optional[str])

  • theme (str)

  • template (Optional[str])

Decorator that enhances API endpoints with HTML rendering capabilities.

When an endpoint decorated with @Web is accessed with an Accept header that includes ‘text/html’ (e.g., from a web browser), the response will be rendered as HTML. Otherwise, the response will be returned as JSON (the default API behavior).

This makes API endpoints more user-friendly when accessed directly from a web browser, while maintaining their programmatic API functionality.

apifrom.decorators.title

The title to display in the HTML page

Type:

str

apifrom.decorators.description

A description of the endpoint to display in the HTML

Type:

str

apifrom.decorators.theme

The theme to use for styling (‘default’, ‘dark’, ‘light’)

Type:

str

apifrom.decorators.template

Optional path to a custom HTML template

Type:

str

Example

```python from apifrom.core.app import API from apifrom.decorators.web import Web

app = API()

@app.api(‘/users’) @Web(title=”Users API”, description=”Get a list of users”) def get_users(request):

return [

{“id”: 1, “name”: “Alice”}, {“id”: 2, “name”: “Bob”}, {“id”: 3, “name”: “Charlie”}

]

```

Initialize the Web decorator.

param title:

The title to display in the HTML page

param description:

A description of the endpoint to display in the HTML

param theme:

The theme to use for styling (‘default’, ‘dark’, ‘light’)

param template:

Optional path to a custom HTML template

apifrom.decorators.__call__(func)

Apply the decorator to the function.

param func:

The function to decorate

returns:

The decorated function

apifrom.decorators._format_value(value)

Format a value for HTML display.

param value:

The value to format

returns:

The formatted value

apifrom.decorators._get_theme_styles()

Get the CSS styles for the current theme.

returns:

The CSS styles for the theme

apifrom.decorators._render_dict(data)

Render a dictionary as HTML.

param data:

The dictionary to render

returns:

The HTML representation of the dictionary

apifrom.decorators._render_html(data)

Render the data as HTML.

param data:

The data to render

returns:

The HTML representation of the data

apifrom.decorators._render_list(data)

Render a list as HTML.

param data:

The list to render

returns:

The HTML representation of the list

apifrom.decorators._render_with_template(data)

Render the data using a custom template.

param data:

The data to render

returns:

The HTML representation of the data using the template

WebOptimize(title = None, description = None, theme = 'default', template = None, enable_profiling = False, enable_caching = False, cache_ttl = 60, enable_connection_pooling = False, pool_name = None, enable_request_coalescing = False, coalesce_window_time = 0.5, coalesce_max_requests = 10, enable_batch_processing = False, batch_size = 10, batch_wait_time = 0.5):bases: apifrom.decorators.web.Web

Decorator that combines the Web decorator with performance optimization features.

This decorator enhances API endpoints with HTML rendering capabilities and performance optimization features like profiling, caching, connection pooling, request coalescing, and batch processing.

apifrom.decorators.title

The title to display in the HTML page

Type:

str

apifrom.decorators.description

A description of the endpoint to display in the HTML

Type:

str

apifrom.decorators.theme

The theme to use for styling (‘default’, ‘dark’, ‘light’)

Type:

str

apifrom.decorators.template

Optional path to a custom HTML template

Type:

str

apifrom.decorators.enable_profiling

Whether to enable profiling

Type:

bool

apifrom.decorators.enable_caching

Whether to enable caching

Type:

bool

apifrom.decorators.cache_ttl

Time-to-live for cached responses in seconds

Type:

int

apifrom.decorators.enable_connection_pooling

Whether to enable connection pooling

Type:

bool

apifrom.decorators.pool_name

Name of the connection pool to use

Type:

str

apifrom.decorators.enable_request_coalescing

Whether to enable request coalescing

Type:

bool

apifrom.decorators.coalesce_window_time

Time window for coalescing requests in seconds

Type:

float

apifrom.decorators.coalesce_max_requests

Maximum number of requests to coalesce

Type:

int

apifrom.decorators.enable_batch_processing

Whether to enable batch processing

Type:

bool

apifrom.decorators.batch_size

Maximum batch size for batch processing

Type:

int

apifrom.decorators.batch_wait_time

Maximum wait time for batch processing in seconds

Type:

float

Example

```python from apifrom.core.app import API from apifrom.decorators.web_optimize import WebOptimize

app = API()

@app.api(‘/users’) @WebOptimize(

title=”Users API”, description=”Get a list of users”, enable_caching=True, cache_ttl=60, enable_profiling=True

) def get_users(request):

return [

{“id”: 1, “name”: “Alice”, “email”: “alice@example.com”}, {“id”: 2, “name”: “Bob”, “email”: “bob@example.com”}, {“id”: 3, “name”: “Charlie”, “email”: “charlie@example.com”}

]

```

Initialize the WebOptimize decorator.

param title:

The title to display in the HTML page

param description:

A description of the endpoint to display in the HTML

param theme:

The theme to use for styling (‘default’, ‘dark’, ‘light’)

param template:

Optional path to a custom HTML template

param enable_profiling:

Whether to enable profiling

param enable_caching:

Whether to enable caching

param cache_ttl:

Time-to-live for cached responses in seconds

param enable_connection_pooling:

Whether to enable connection pooling

param pool_name:

Name of the connection pool to use

param enable_request_coalescing:

Whether to enable request coalescing

param coalesce_window_time:

Time window for coalescing requests in seconds

param coalesce_max_requests:

Maximum number of requests to coalesce

param enable_batch_processing:

Whether to enable batch processing

param batch_size:

Maximum batch size for batch processing

param batch_wait_time:

Maximum wait time for batch processing in seconds

apifrom.decorators.__call__(func)

Apply the WebOptimize decorator to a function.

param func:

The function to decorate

returns:

The decorated function

apifrom.decorators.create_pool(name, min_size=5, max_size=20, validate_func=None)
:classmethod:

Create a connection pool with the specified settings.

param name:

Name of the connection pool

param min_size:

Minimum number of connections in the pool

param max_size:

Maximum number of connections in the pool

param validate_func:

Function to validate connections

returns:

A ConnectionPool instance

apifrom.decorators.get_cache_optimizer()
:classmethod:

Get the class-level cache optimizer instance.

apifrom.decorators.get_performance_metrics()
:classmethod:

Get performance metrics from all optimization components.

returns:

A dictionary of performance metrics

apifrom.decorators.get_pool(name)
:classmethod:

Get a connection pool by name.

param name:

Name of the connection pool

returns:

A ConnectionPool instance, or None if not found

apifrom.decorators.get_pool_manager()
:classmethod:

Get the class-level pool manager instance.

apifrom.decorators.get_profiler()
:classmethod:

Get the class-level profiler instance.

apifrom.decorators.optimize(title=None, description=None, theme='default', template=None, enable_profiling=True, enable_caching=True, cache_ttl=60, enable_connection_pooling=True, pool_name=None, enable_request_coalescing=True, coalesce_window_time=0.5, coalesce_max_requests=10, enable_batch_processing=False, batch_size=10, batch_wait_time=0.5)
:classmethod:

Class method to create a WebOptimize decorator with the specified settings.

This is a convenience method for creating a WebOptimize decorator with common optimization settings.

param title:

The title to display in the HTML page

param description:

A description of the endpoint to display in the HTML

param theme:

The theme to use for styling (‘default’, ‘dark’, ‘light’)

param template:

Optional path to a custom HTML template

param enable_profiling:

Whether to enable profiling

param enable_caching:

Whether to enable caching

param cache_ttl:

Time-to-live for cached responses in seconds

param enable_connection_pooling:

Whether to enable connection pooling

param pool_name:

Name of the connection pool to use

param enable_request_coalescing:

Whether to enable request coalescing

param coalesce_window_time:

Time window for coalescing requests in seconds

param coalesce_max_requests:

Maximum number of requests to coalesce

param enable_batch_processing:

Whether to enable batch processing

param batch_size:

Maximum batch size for batch processing

param batch_wait_time:

Maximum wait time for batch processing in seconds

returns:

A WebOptimize decorator with the specified settings

Functions

apifrom.decorators.api(route=None, method='GET', name=None, description=None, tags=None, response_model=None, status_code=200, deprecated=False, include_in_schema=True, **kwargs)[source]
Parameters:
  • route (str)

  • method (str)

  • name (str)

  • description (str)

  • tags (List[str])

  • response_model (Type)

  • status_code (int)

  • deprecated (bool)

  • include_in_schema (bool)

Decorator to expose a Python function as an API endpoint.

param route:

The route for the endpoint. If None, derived from function name.

param method:

The HTTP method for the endpoint.

param name:

The name for the endpoint. If None, derived from function name.

param description:

The description for the endpoint.

param tags:

Tags for the endpoint.

param response_model:

The response model for the endpoint.

param status_code:

The default status code for successful responses.

param deprecated:

Whether the endpoint is deprecated.

param include_in_schema:

Whether to include the endpoint in the API schema.

param **kwargs:

Additional arguments to pass to the router.

returns:

A decorator function.