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.

Overview

Classes

Classes

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.middleware.cors.options

Options for the middleware.

Type:

dict

Initialize a new BaseMiddleware instance.

param **options:

Options for the middleware.

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

apifrom.middleware.cors._add_cors_headers(request, response)

Add CORS headers to a response.

param request:

The request

param response:

The response to add headers to

apifrom.middleware.cors._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

apifrom.middleware.cors._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

apifrom.middleware.cors.process_request(request)
:async:

Process a request through the CORS middleware.

param request:

The request to process

returns:

The processed request

apifrom.middleware.cors.process_response(response)
:async:

Process a response through the CORS middleware.

param response:

The response to process

returns:

The processed response

class apifrom.middleware.cors.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.middleware.cors._request

The underlying Starlette request.

apifrom.middleware.cors.path_params

Path parameters extracted from the URL.

apifrom.middleware.cors.query_params

Query parameters extracted from the URL.

apifrom.middleware.cors.headers

HTTP headers.

apifrom.middleware.cors.method

HTTP method.

apifrom.middleware.cors.path

Request path.

apifrom.middleware.cors._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.middleware.cors.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.middleware.cors.content

The response content.

apifrom.middleware.cors.status_code

The HTTP status code.

apifrom.middleware.cors.headers

HTTP headers.

apifrom.middleware.cors.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.