base#

class starlite.connection.base.ASGIConnection#

Bases: Generic[Handler, User, Auth]

The base ASGI connection container.

__init__(scope: Scope, receive: Receive = <function empty_receive>, send: Send = <function empty_send>) None#

Initialize ASGIConnection.

Parameters:
  • scope – The ASGI connection scope.

  • receive – The ASGI receive function.

  • send – The ASGI send function.

scope: Scope#

The ASGI scope attached to the connection.

receive: Receive#

The ASGI receive function.

send: Send#

The ASGI send function.

property app: Starlite#

Return the app for this connection.

Returns:

The Starlite application instance

property route_handler: Handler#

Return the route_handler for this connection.

Returns:

The target route handler instance.

property state: State#

Return the State of this connection.

Returns:

A State instance constructed from the scope[“state”] value.

property url: URL#

Return the URL of this connection’s Scope.

Returns:

A URL instance constructed from the request’s scope.

property base_url: URL#

Return the base URL of this connection’s Scope.

Returns:

A URL instance constructed from the request’s scope, representing only the base part (host + domain + prefix) of the request.

property headers: Headers#

Return the headers of this connection’s Scope.

Returns:

A Headers instance with the request’s scope[“headers”] value.

property query_params: MultiDict#

Return the query parameters of this connection’s Scope.

Returns:

A normalized dict of query parameters. Multiple values for the same key are returned as a list.

property path_params: Dict[str, Any]#

Return the path_params of this connection’s Scope.

Returns:

A string keyed dictionary of path parameter values.

property cookies: Dict[str, str]#

Return the cookies of this connection’s Scope.

Returns:

Returns any cookies stored in the header as a parsed dictionary.

property client: Optional[Address]#

Return the client data of this connection’s Scope.

Returns:

A two tuple of the host name and port number.

property auth: Auth#

Return the auth data of this connection’s Scope.

Raises:

ImproperlyConfiguredException – If auth is not set in scope via an AuthMiddleware, raises an exception

Returns:

A type correlating to the generic variable Auth.

property user: User#

Return the user data of this connection’s Scope.

Raises:

ImproperlyConfiguredException – If user is not set in scope via an AuthMiddleware, raises an exception

Returns:

A type correlating to the generic variable User.

property session: Dict[str, Any]#

Return the session for this connection if a session was previously set in the Scope

Returns:

A dictionary representing the session value - if existing.

Raises:

ImproperlyConfiguredException – if session is not set in scope.

property logger: Logger#

Return the Logger instance for this connection.

Returns:

A Logger instance.

Raises:

ImproperlyConfiguredException – if log_config has not been passed to the Starlite constructor.

property cache: Cache#

Return the Cache for this connection.

Returns:

A Cache instance.

set_session(value: Union[Dict[str, Any], BaseModel, EmptyType]) None#

Set the session in the connection’s Scope.

If the Starlite SessionMiddleware is enabled, the session will be added to the response as a cookie header.

Parameters:

value – Dictionary or pydantic model instance for the session data.

Returns:

None.

clear_session() None#

Remove the session from the connection’s Scope.

If the Starlite SessionMiddleware is enabled, this will cause the session data to be cleared.

Returns:

None.

url_for(name: str, **path_parameters: Dict[str, Any]) str#

Return the url for a given route handler name.

Parameters:
  • name – The name of the request route handler.

  • **path_parameters – Values for path parameters in the route

Raises:

NoRouteMatchFoundException – If route with name does not exist, path parameters are missing or have a wrong type.

Returns:

A string representing the absolute url of the route handler.

url_for_static_asset(name: str, file_path: str) str#

Receives a static files handler name, an asset file path and returns resolved absolute url to the asset.

Parameters:
  • name – A static handler unique name.

  • file_path – a string containing path to an asset.

Raises:

NoRouteMatchFoundException – If static files handler with name does not exist.

Returns:

A string representing absolute url to the asset.