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

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:

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.

apifrom.core.app.router

The router instance for managing routes.

Type:

Router

apifrom.core.app.middleware

List of middleware instances.

Type:

list

apifrom.core.app.debug

Whether to run in debug mode.

Type:

bool

apifrom.core.app.title

The title of the API.

Type:

str

apifrom.core.app.description

The description of the API.

Type:

str

apifrom.core.app.version

The version of the API.

Type:

str

apifrom.core.app.docs_url

The URL for the API documentation.

Type:

str

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.

This abstract class defines the interface for middleware components. Middleware components can process requests and responses.

apifrom.core.app.options

Options for the middleware.

Type:

dict

Initialize a new BaseMiddleware instance.

param **options:

Options for the middleware.

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]
Parameters:
  • title (str)

  • description (str)

  • version (str)

  • terms_of_service (Optional[str])

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

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

  • servers (Optional[List[Dict[str, str]]])

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

  • tags (Optional[List[Dict[str, Any]]])

  • security (Optional[List[Dict[str, List[str]]]])

  • components (Optional[Dict[str, Any]])

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:

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]
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.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]
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.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.

class apifrom.core.app.Router[source]

Router for managing API endpoints.

This class is responsible for registering and looking up API endpoints. It maintains a registry of routes and their associated handlers.

apifrom.core.app.routes

List of registered routes.

Type:

list

Initialize a new Router instance.

class apifrom.core.app.SwaggerUI(openapi_generator, url_prefix='/docs', config=None, assets_dir=None, static_files_route='/static')[source]
Parameters:

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