response_containers#

class starlite.datastructures.response_containers.ResponseContainer#

Bases: ABC, GenericModel, Generic[R]

Generic response container.

background: Optional[Union[BackgroundTask, BackgroundTasks]]#

A BackgroundTask instance or.

BackgroundTasks to execute after the response is finished. Defaults to None.

headers: Dict[str, Any]#

A string/string dictionary of response headers.

Header keys are insensitive. Defaults to None.

cookies: List[Cookie]#

A list of Cookie instances to be set under the response ‘Set-Cookie’ header.

Defaults to None.

media_type: Optional[Union[MediaType, str]]#

If defined, overrides the media type configured in the route decorator.

encoding: str#

The encoding to be used for the response headers.

abstract to_response(headers: Dict[str, Any], media_type: Union[MediaType, str], status_code: int, app: Starlite, request: Request) R#

Abstract method that should be implemented by subclasses.

Parameters:
  • headers – A dictionary of headers.

  • media_type – A string or member of the MediaType enum.

  • status_code – A response status code.

  • app – The Starlite application instance.

  • request – A Request instance.

Returns:

A Response Object

class starlite.datastructures.response_containers.File#

Bases: ResponseContainer[FileResponse]

Container type for returning File responses.

path: FilePath#

Path to the file to send.

filename: Optional[str]#

An optional filename to set in the header.

stat_result: Optional[stat_result]#

An optional result of calling ‘os.stat’.

If not provided, this will be done by the response constructor.

chunk_size: int#

The size of chunks to use when streaming the file.

content_disposition_type: Literal['attachment', 'inline']#

The type of the ‘Content-Disposition’.

Either ‘inline’ or ‘attachment’.

etag: Optional[ETag]#

An optional ETag instance.

If not provided, an etag will be automatically generated.

file_system: Any#

The file_system spec to use loading the file.

Notes

file_info: Optional[FileInfo]#

The output of calling file_system.info(..), equivalent to providing a stat_result.

classmethod validate_status_code(value: Optional[stat_result], values: Dict[str, Any]) stat_result#

Set the stat_result value for the given filepath.

Parameters:
  • value – An optional result stat result.

  • values – The dict of values.

Returns:

A stat_result

classmethod validate_file_system(value: FileSystemProtocol) FileSystemProtocol#

Ensure the value is a file system spec.

Parameters:

value – A file system spec.

Returns:

A file system spec.

to_response(headers: Dict[str, Any], media_type: Optional[Union[MediaType, str]], status_code: int, app: Starlite, request: Request) FileResponse#

Create a FileResponse instance.

Parameters:
  • headers – A dictionary of headers.

  • media_type – A string or member of the MediaType enum.

  • status_code – A response status code.

  • app – The Starlite application instance.

  • request – A Request instance.

Returns:

A FileResponse instance

class starlite.datastructures.response_containers.Redirect#

Bases: ResponseContainer[RedirectResponse]

Container type for returning Redirect responses.

path: str#

Redirection path.

to_response(headers: Dict[str, Any], media_type: Union[MediaType, str], status_code: Literal[301, 302, 303, 307, 308], app: Starlite, request: Request) RedirectResponse#

Create a RedirectResponse instance.

Parameters:
  • headers – A dictionary of headers.

  • media_type – A string or member of the MediaType enum.

  • status_code – A response status code.

  • app – The Starlite application instance.

  • request – A Request instance.

Returns:

A RedirectResponse instance

class starlite.datastructures.response_containers.Stream#

Bases: ResponseContainer[StreamingResponse]

Container type for returning Stream responses.

iterator: Union[Iterable[Union[str, bytes]], Iterator[Union[str, bytes]], AsyncIterable[Union[str, bytes]], AsyncIterator[Union[str, bytes]], Callable[[], Union[Iterable[Union[str, bytes]], Iterator[Union[str, bytes]], AsyncIterable[Union[str, bytes]], AsyncIterator[Union[str, bytes]]]]]#

Iterator, Iterable,Generator or async Iterator, Iterable or Generator returning chunks to stream.

classmethod validate_iterator(value: Union[Iterable[Union[str, bytes]], Iterator[Union[str, bytes]], AsyncIterable[Union[str, bytes]], AsyncIterator[Union[str, bytes]], Callable[[], Union[Iterable[Union[str, bytes]], Iterator[Union[str, bytes]], AsyncIterable[Union[str, bytes]], AsyncIterator[Union[str, bytes]]]]]) Union[Iterable[Union[str, bytes]], Iterator[Union[str, bytes]], AsyncIterable[Union[str, bytes]], AsyncIterator[Union[str, bytes]]]#

Set the iterator value by ensuring that the return value is iterable.

Parameters:

value – An iterable or callable returning an iterable.

Returns:

A sync or async iterable.

to_response(headers: Dict[str, Any], media_type: Union[MediaType, str], status_code: int, app: Starlite, request: Request) StreamingResponse#

Create a StreamingResponse instance.

Parameters:
  • headers – A dictionary of headers.

  • media_type – A string or member of the MediaType enum.

  • status_code – A response status code.

  • app – The Starlite application instance.

  • request – A Request instance.

Returns:

A StreamingResponse instance

class starlite.datastructures.response_containers.Template#

Bases: ResponseContainer[TemplateResponse]

Container type for returning Template responses.

name: str#

Path-like name for the template to be rendered, e.g. “index.html”.

context: Dict[str, Any]#

A dictionary of key/value pairs to be passed to the temple engine’s render method.

Defaults to None.

to_response(headers: Dict[str, Any], media_type: Union[MediaType, str], status_code: int, app: Starlite, request: Request) TemplateResponse#

Create a TemplateResponse instance.

Parameters:
  • headers – A dictionary of headers.

  • media_type – A string or member of the MediaType enum.

  • status_code – A response status code.

  • app – The Starlite application instance.

  • request – A Request instance.

Raises:

ImproperlyConfiguredException <starlite.exceptions.ImproperlyConfiguredException> – if app.template_engine is not configured.

Returns:

A TemplateResponse instance

create_template_context(request: Request) Dict[str, Any]#

Create a context object for the template.

Parameters:

request – A Request instance.

Returns:

A dictionary holding the template context