apifrom.decorators.api

API decorator for APIFromAnything.

This module defines the main API decorator that can be used to expose Python functions as API endpoints.

Overview

Classes

Functions

Classes

ErrorResponse(message, status_code = 400, error_code = None, details = None, headers = None):bases: JSONResponse

Error response for APIFromAnything.

This class represents an HTTP error response with JSON content.

Initialize a new ErrorResponse instance.

param message:

The error message.

param status_code:

The HTTP status code.

param error_code:

An optional error code.

param details:

Additional error details.

param headers:

HTTP headers.

JSONResponse(content = None, status_code = 200, headers = None):bases: Response

JSON response for APIFromAnything.

This class represents an HTTP response with JSON content.

Initialize a new JSONResponse instance.

param content:

The response content.

param status_code:

The HTTP status code.

param headers:

HTTP headers.

class apifrom.decorators.api.Request(request=None, path_params=None, method=None, path=None, query_params=None, headers=None, body=None, client_ip=None)[source]
Parameters:
  • request (Optional[starlette.requests.Request])

  • path_params (Optional[dict[Any, Any]])

  • method (Optional[str])

  • path (Optional[str])

  • query_params (Optional[dict[Any, Any]])

  • headers (Optional[dict[Any, Any]])

  • body (Optional[Union[str, bytes]])

  • client_ip (Optional[str])

Request class for APIFromAnything.

This class wraps a Starlette request and provides methods for accessing request data in a convenient way.

apifrom.decorators.api._request

The underlying Starlette request.

apifrom.decorators.api.path_params

Path parameters extracted from the URL.

apifrom.decorators.api.query_params

Query parameters extracted from the URL.

apifrom.decorators.api.headers

HTTP headers.

apifrom.decorators.api.method

HTTP method.

apifrom.decorators.api.path

Request path.

apifrom.decorators.api._body

Cached request body.

Initialize a new Request instance.

param request:

The underlying Starlette request.

param path_params:

Path parameters extracted from the URL.

param method:

The HTTP method.

param path:

The request path.

param query_params:

Query parameters.

param headers:

HTTP headers.

param body:

Request body.

param client_ip:

Client IP address.

class apifrom.decorators.api.Response(content=None, status_code=200, headers=None, content_type='application/json')[source]
Parameters:
  • content (Any)

  • status_code (int)

  • headers (Optional[Dict[str, str]])

  • content_type (str)

Response class for APIFromAnything.

This class represents an HTTP response and provides methods for setting response data, status code, and headers.

apifrom.decorators.api.content

The response content.

apifrom.decorators.api.status_code

The HTTP status code.

apifrom.decorators.api.headers

HTTP headers.

apifrom.decorators.api.content_type

The content type of the response.

Initialize a new Response instance.

param content:

The response content.

param status_code:

The HTTP status code.

param headers:

HTTP headers.

param content_type:

The content type of the response.

Functions

apifrom.decorators.api.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.

apifrom.decorators.api.deserialize_params(params, func, type_hints=None)[source]
Parameters:
Return type:

Dict[str, Any]

Deserialize parameters for a function call.

param params:

The parameters to deserialize.

param func:

The function or signature to deserialize parameters for.

param type_hints:

Optional type hints. If not provided, they will be extracted from the function.

returns:

The deserialized parameters.

apifrom.decorators.api.get_type_hints_with_extras(obj)[source]
Parameters:

obj (Callable)

Return type:

Dict[str, Type]

Get type hints for a callable, including return type.

This function extends typing.get_type_hints to include additional information about the types, such as whether they are optional.

param obj:

The callable to get type hints for.

returns:

A dictionary mapping parameter names to their types.

apifrom.decorators.api.serialize_response(result)[source]
Parameters:

result (Any)

Return type:

Any

Serialize a function result for an API response.

param result:

The function result to serialize.

returns:

A JSON-compatible representation of the result.