apifrom.middleware.cors ======================= CORS (Cross-Origin Resource Sharing) middleware for APIFromAnything. This module provides middleware for handling CORS headers to allow controlled cross-origin requests to the API. .. py:currentmodule:: apifrom.middleware.cors Overview -------- **Classes** * :py:class:`BaseMiddleware` * :py:class:`CORSMiddleware` * :py:class:`Request` * :py:class:`Response` Classes ------- .. py:class:: 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. .. attribute:: options Options for the middleware. :type: dict Initialize a new BaseMiddleware instance. :param \*\*options: Options for the middleware. .. py:class:: CORSMiddleware(allow_origins = None, allow_methods = None, allow_headers = None, allow_credentials = False, expose_headers = None, max_age = 600, preflight_continue = False, options_success_status = 204):bases: apifrom.middleware.base.BaseMiddleware Middleware for handling Cross-Origin Resource Sharing (CORS). This middleware adds appropriate CORS headers to responses to allow controlled cross-origin requests to the API. Initialize the CORS middleware. :param allow_origins: List of origins that are allowed to make cross-origin requests, or "*" to allow any origin :param allow_methods: List of HTTP methods that are allowed for cross-origin requests :param allow_headers: List of HTTP headers that can be used in cross-origin requests :param allow_credentials: Whether cookies and credentials can be included in cross-origin requests :param expose_headers: List of HTTP headers that can be exposed to the browser :param max_age: How long the results of a preflight request can be cached (in seconds) :param preflight_continue: Whether to continue processing after a preflight request :param options_success_status: Status code to use for successful OPTIONS requests .. :: allow_credentials .. :: allow_headers .. :: allow_methods .. :: allow_origins .. :: expose_headers .. :: max_age .. :: options_success_status .. :: preflight_continue .. method:: _add_cors_headers(request, response) Add CORS headers to a response. :param request: The request :param response: The response to add headers to .. method:: _handle_preflight(request) Handle a CORS preflight request. :param request: The preflight request :returns: A response for the preflight request, or None to continue processing .. method:: _is_origin_allowed(origin) Check if an origin is allowed. :param origin: The origin to check :returns: True if the origin is allowed, False otherwise .. method:: process_request(request) :async: Process a request through the CORS middleware. :param request: The request to process :returns: The processed request .. method:: process_response(response) :async: Process a response through the CORS middleware. :param response: The response to process :returns: The processed response .. py:class:: Request(request = None, path_params = None, method = None, path = None, query_params = None, headers = None, body = None, client_ip = None) Request class for APIFromAnything. This class wraps a Starlette request and provides methods for accessing request data in a convenient way. .. attribute:: _request The underlying Starlette request. .. attribute:: path_params Path parameters extracted from the URL. .. attribute:: query_params Query parameters extracted from the URL. .. attribute:: headers HTTP headers. .. attribute:: method HTTP method. .. attribute:: path Request path. .. attribute:: _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. .. py:class:: Response(content = None, status_code = 200, headers = None, content_type = 'application/json') Response class for APIFromAnything. This class represents an HTTP response and provides methods for setting response data, status code, and headers. .. attribute:: content The response content. .. attribute:: status_code The HTTP status code. .. attribute:: headers HTTP headers. .. attribute:: 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.