apifrom.security.cors

Overview

Classes

Classes

class apifrom.security.cors.CORSMiddleware(allow_origins=None, allow_methods=None, allow_headers=None, allow_credentials=False, expose_headers=None, max_age=600)[source]
Parameters:
  • allow_origins (Optional[Union[List[str], str]])

  • allow_methods (Optional[List[str]])

  • allow_headers (Optional[List[str]])

  • allow_credentials (bool)

  • expose_headers (Optional[List[str]])

  • max_age (int)

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.

apifrom.security.cors._create_preflight_response(request)

Create a response for preflight requests.

param request:

The preflight request.

returns:

A response with appropriate CORS headers.

apifrom.security.cors._get_allow_origin(request)

Get the appropriate Access-Control-Allow-Origin header value.

param request:

The request to process.

returns:

The appropriate origin value.

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

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

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

The underlying Starlette request.

apifrom.security.cors.path_params

Path parameters extracted from the URL.

apifrom.security.cors.query_params

Query parameters extracted from the URL.

apifrom.security.cors.headers

HTTP headers.

apifrom.security.cors.method

HTTP method.

apifrom.security.cors.path

Request path.

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

The response content.

apifrom.security.cors.status_code

The HTTP status code.

apifrom.security.cors.headers

HTTP headers.

apifrom.security.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.