Response

class spectree.response.Response[source]

SpecTree response object. The instance can only be used after calling the bind_model_adapter() method. This method is called automatically when it’s passed to SpecTree.validate method.

Parameters:
  • codes – list of HTTP status code, format(‘HTTP_[0-9]{3}’), ‘HTTP_200’

  • code_models – dict of <HTTP status code>: <model class> or None or a two element tuple of (<model class> or None) as the first item and a custom status code description string as the second item.

examples:

>>> from typing import List
>>> from spectree.response import Response
>>> response = Response("HTTP_200")
>>> response = Response(HTTP_200=None)
>>> response = Response(HTTP_200=MyModel)
>>> response = Response(HTTP_200=(MyModel, "status code description"))
>>> response = Response(HTTP_200=List[MyModel])
>>> response = Response(HTTP_200=(List[MyModel], "status code description"))
__init__(*codes: str, **code_models: None | object | Tuple[object | None, str]) None[source]
add_model(code: int, model: object, replace: bool = True, description: str | None = None) None[source]

Add data model for the specified status code.

Parameters:
  • code – An HTTP status code.

  • model – A response model class.

  • replace – If True and a data model already exists for the given status code it will be replaced, if False the existing data model will be retained.

  • description – The description string for the code.

bind_model_adapter(model_adapter: ModelAdapter[Any, Exception]) None[source]

Bind a ModelAdapter

find_model(code: int) type[Any] | None[source]
Parameters:

coder'\d{3}'

generate_spec(naming_strategy: ~typing.Callable[[type[~typing.Any]], str] = <function get_model_key>) Dict[str, Any][source]

generate the spec for responses

Returns:

JSON

get_code_description(code: str) str[source]

Get the description of the given status code.

Parameters:

code – Status code string, format(‘HTTP_[0-9]_{3}’), ‘HTTP_200’.

Returns:

The status code’s description.

has_model() bool[source]
Returns:

boolean – does this response has models or not

property models: Iterable[type[Any]]
Returns:

dict_values – all the models in this response