apifrom.core.appο
Core application container for APIFromAnything.
This module defines the main API class that serves as the container for all registered endpoints, middleware, and configuration.
Overviewο
Classes
BaseMiddlewareJSONResponse
Classesο
- class apifrom.core.app.API(debug=False, title='APIFromAnything API', description='API created with APIFromAnything', version='1.0.0', docs_url='/docs', openapi_config=None, swagger_ui_config=None, enable_docs=True)[source]ο
- Parameters:
debug (bool)
title (str)
description (str)
version (str)
docs_url (str)
openapi_config (apifrom.docs.openapi.OpenAPIConfig | None)
swagger_ui_config (apifrom.docs.swagger_ui.SwaggerUIConfig | None)
enable_docs (bool)
Main application container for APIFromAnything.
This class serves as the central registry for all API endpoints, middleware, and configuration. It provides methods for registering endpoints, middleware, and starting the server.
Initialize a new API instance.
- param debug:
Whether to run in debug mode.
- param title:
The title of the API.
- param description:
The description of the API.
- param version:
The version of the API.
- param docs_url:
The URL for the API documentation.
- param openapi_config:
Configuration for OpenAPI documentation.
- param swagger_ui_config:
Configuration for Swagger UI.
- param enable_docs:
Whether to enable API documentation.
- apifrom.core.app.__call__(scope, receive, send)ο
ASGI callable.
This method allows the API instance to be used as an ASGI application.
- param scope:
The ASGI scope.
- param receive:
The ASGI receive function.
- param send:
The ASGI send function.
- apifrom.core.app._build_app()ο
Build the Starlette application.
- returns:
The Starlette application instance.
- apifrom.core.app._get_current_instance()ο
- :classmethod:
Get the current API instance.
- returns:
The current API instance, or None if no instance has been created.
- apifrom.core.app.add_middleware(middleware)ο
Add middleware to the API.
- param middleware:
The middleware instance to add.
- apifrom.core.app.add_route(path, endpoint=None, methods=None, name=None, include_in_schema=True, **kwargs)ο
Add a route to the API.
This method can be used as a decorator or called directly.
- param path:
The path for the route.
- param endpoint:
The endpoint function.
- param methods:
The HTTP methods for the route.
- param name:
The name for the route.
- param include_in_schema:
Whether to include the route in the OpenAPI schema.
- param **kwargs:
Additional arguments to pass to the router.
- returns:
The endpoint function if used as a decorator, or None if called directly.
- apifrom.core.app.add_routes(routes)ο
Add multiple routes to the API.
This method adds multiple routes to the API at once. The routes should be functions decorated with the @api decorator.
- param routes:
A list of functions decorated with the @api decorator.
- apifrom.core.app.process_request(request)ο
- :async:
Process a request and return a response.
This method is used by adapters to process requests without going through the ASGI interface.
- param request:
The request to process.
- returns:
The response to the request.
- apifrom.core.app.register_endpoint(handler, route, method='GET', name=None, **kwargs)ο
Register an endpoint with the API.
- param handler:
The handler function for the endpoint.
- param route:
The route for the endpoint.
- param method:
The HTTP method for the endpoint.
- param name:
The name of the endpoint.
- param **kwargs:
Additional arguments to pass to the router.
- apifrom.core.app.run(host='127.0.0.1', port=8000, **kwargs)ο
Run the API server.
- param host:
The host to bind to.
- param port:
The port to bind to.
- param **kwargs:
Additional arguments to pass to uvicorn.run.
- BaseMiddleware(**options):bases: abc.ABC
Base middleware class for APIFromAnything.
- 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.core.app.OpenAPIConfig(title, description, version, terms_of_service=None, contact=None, license_info=None, servers=None, external_docs=None, tags=None, security=None, components=None)[source]ο
Configuration for OpenAPI documentation generation.
This class provides configuration options for customizing the OpenAPI documentation generation process.
Initialize the OpenAPI configuration.
- param title:
The title of the API
- param description:
The description of the API
- param version:
The version of the API
- param terms_of_service:
URL to the terms of service
- param contact:
Contact information (name, url, email)
- param license_info:
License information (name, url)
- param servers:
Server information (url, description, variables)
- param external_docs:
External documentation (description, url)
- param tags:
API tags with descriptions
- param security:
Global security requirements
- param components:
Pre-defined components (schemas, responses, parameters, etc.)
- class apifrom.core.app.OpenAPIGenerator(title, description, version, router, config=None)[source]ο
- Parameters:
title (str)
description (str)
version (str)
router (apifrom.core.router.Router)
config (Optional[OpenAPIConfig])
Generator for OpenAPI documentation.
This class generates OpenAPI/Swagger documentation for APIs created with the APIFromAnything library, with extensive customization options.
Initialize the OpenAPI generator.
- param title:
The title of the API
- param description:
The description of the API
- param version:
The version of the API
- param router:
The router instance containing the API routes
- param config:
Additional configuration options
- class apifrom.core.app.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.core.app._requestο
The underlying Starlette request.
- apifrom.core.app.path_paramsο
Path parameters extracted from the URL.
- apifrom.core.app.query_paramsο
Query parameters extracted from the URL.
- apifrom.core.app.headersο
HTTP headers.
- apifrom.core.app.methodο
HTTP method.
- apifrom.core.app.pathο
Request path.
- apifrom.core.app._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.core.app.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.core.app.contentο
The response content.
- apifrom.core.app.status_codeο
The HTTP status code.
- apifrom.core.app.headersο
HTTP headers.
- apifrom.core.app.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.
Router for managing API endpoints.
- class apifrom.core.app.SwaggerUI(openapi_generator, url_prefix='/docs', config=None, assets_dir=None, static_files_route='/static')[source]ο
- Parameters:
openapi_generator (apifrom.docs.openapi.OpenAPIGenerator)
url_prefix (str)
config (Optional[SwaggerUIConfig])
assets_dir (Optional[str])
static_files_route (str)
Swagger UI for APIFromAnything.
This class provides a Swagger UI integration for APIs created with the APIFromAnything library, with extensive customization options.
Initialize the Swagger UI.
- param openapi_generator:
The OpenAPI generator instance
- param url_prefix:
The URL prefix for the documentation
- param config:
The Swagger UI configuration
- param assets_dir:
Directory containing custom assets (CSS, JS, images)
- param static_files_route:
The route for static files
- class apifrom.core.app.SwaggerUIConfig(theme='default', deep_linking=True, display_operation_id=False, default_models_expand_depth=1, default_model_expand_depth=1, default_model_rendering='example', display_request_duration=True, doc_expansion='list', filter=True, max_displayed_tags=None, operations_sorter=None, show_extensions=False, show_common_extensions=False, tag_sorter=None, use_unicode_characters=True, persist_authorization=False, syntax_highlight='monokai', oauth2_redirect_url=None, custom_css=None, custom_js=None, custom_favicon=None, custom_swagger_ui_version='5.9.1', dom_id='#swagger-ui', layout='StandaloneLayout', plugins=None, presets=None)[source]ο
- Parameters:
theme (str)
deep_linking (bool)
display_operation_id (bool)
default_models_expand_depth (int)
default_model_expand_depth (int)
default_model_rendering (str)
display_request_duration (bool)
doc_expansion (str)
filter (bool)
max_displayed_tags (Optional[int])
operations_sorter (Optional[str])
show_extensions (bool)
show_common_extensions (bool)
tag_sorter (Optional[str])
use_unicode_characters (bool)
persist_authorization (bool)
syntax_highlight (str)
oauth2_redirect_url (Optional[str])
custom_css (Optional[str])
custom_js (Optional[str])
custom_favicon (Optional[str])
custom_swagger_ui_version (str)
dom_id (str)
layout (str)
plugins (Optional[List[str]])
presets (Optional[List[str]])
Configuration for Swagger UI customization.
This class provides extensive configuration options for customizing the appearance and behavior of the Swagger UI documentation.
Initialize the Swagger UI configuration.
- param theme:
The theme to use (default, material, muted, outline, flattop)
- param deep_linking:
If set to true, enables deep linking for tags and operations
- param display_operation_id:
Controls the display of operationId in operations list
- param default_models_expand_depth:
The default expansion depth for models (set to -1 completely hide the models)
- param default_model_expand_depth:
The default expansion depth for the model on the model-example section
- param default_model_rendering:
Controls how the model is shown when the API is first rendered
- param display_request_duration:
Controls the display of the request duration (in milliseconds) for Try-It-Out requests
- param doc_expansion:
Controls the default expansion setting for the operations and tags
- param filter:
If set, enables filtering. The top bar will show an edit box that you can use to filter the tagged operations that are shown
- param max_displayed_tags:
If set, limits the number of tagged operations displayed to at most this many
- param operations_sorter:
Apply a sort to the operation list of each API
- param show_extensions:
Controls the display of vendor extension (x-) fields and values
- param show_common_extensions:
Controls the display of extensions (pattern, maxLength, minLength, maximum, minimum) fields and values
- param tag_sorter:
Apply a sort to the tag list
- param use_unicode_characters:
Controls whether unicode characters are used in rendered descriptions
- param persist_authorization:
If set, will persist authorization data
- param syntax_highlight:
Syntax highlighting theme (agate, arta, monokai, nord, obsidian, etc.)
- param oauth2_redirect_url:
OAuth2 redirect URL
- param custom_css:
URL to a custom CSS file
- param custom_js:
URL to a custom JavaScript file
- param custom_favicon:
URL to a custom favicon
- param custom_swagger_ui_version:
The version of Swagger UI to use
- param dom_id:
The DOM element ID to bind to
- param layout:
The layout to use (BaseLayout, StandaloneLayout)
- param plugins:
The plugins to use
- param presets:
The presets to use