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 toSpecTree.validatemethod.- 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"))
- 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
- 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.
- property models: Iterable[type[Any]]¶
- Returns:
dict_values – all the models in this response