apifrom.security.cors ===================== .. py:currentmodule:: apifrom.security.cors Overview -------- **Classes** * :py:class:`CORSMiddleware` * :py:class:`Request` * :py:class:`Response` Classes ------- .. py:class:: CORSMiddleware(allow_origins = None, allow_methods = None, allow_headers = None, allow_credentials = False, expose_headers = None, max_age = 600) Middleware for handling Cross-Origin Resource Sharing (CORS). This middleware adds appropriate CORS headers to responses and handles preflight requests with the OPTIONS method. Initialize the CORS middleware with the specified configuration. :param allow_origins: A list of origins that are allowed to make requests, or "*" to allow any origin. :param allow_methods: A list of HTTP methods that are allowed. :param allow_headers: A list of HTTP headers that are allowed. :param allow_credentials: Whether to allow credentials (cookies, authorization headers, etc). :param expose_headers: A list of headers that browsers are allowed to access. :param max_age: The maximum time (in seconds) to cache the preflight response. .. :: allow_credentials .. :: allow_headers .. :: allow_methods .. :: allow_origins .. :: expose_headers .. :: max_age .. method:: _create_preflight_response(request) Create a response for preflight requests. :param request: The preflight request. :returns: A response with appropriate CORS headers. .. method:: _get_allow_origin(request) Get the appropriate Access-Control-Allow-Origin header value. :param request: The request to process. :returns: The appropriate origin value. .. method:: process_request(request) :async: Process an incoming request and handle CORS preflight requests. :param request: The incoming request. :returns: A response for preflight requests, or None to continue processing. .. method:: process_response(request, response) Process a response by adding appropriate CORS headers. :param request: The request that led to this response. :param response: The response to process. :returns: The processed response with CORS headers. .. 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.