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]ο
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]ο
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]ο
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:
params (Dict[str, Any])
func (Union[Callable, inspect.Signature])
type_hints (Optional[Dict[str, Type]])
- 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.