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