Response

class spectree.response.Response[source]

response object

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

  • code_models – dict of <HTTP status code>: <pydantic.BaseModel> or None or a two element tuple of (<pydantic.BaseModel> 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
>>> from pydantic import BaseModel
...
>>> class User(BaseModel):
...     id: int
...
>>> response = Response("HTTP_200")
>>> response = Response(HTTP_200=None)
>>> response = Response(HTTP_200=User)
>>> response = Response(HTTP_200=(User, "status code description"))
>>> response = Response(HTTP_200=List[User])
>>> response = Response(HTTP_200=(List[User], "status code description"))
__init__(*codes: str, **code_models: Type[BaseModelSubclassType] | None | Tuple[Type[BaseModelSubclassType] | None, str] | Type[List[BaseModelSubclassType]] | Tuple[Type[List[BaseModelSubclassType]], str]) None[source]
add_model(code: int, model: Type[BaseModelSubclassType], 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 pydantic.BaseModel.

  • 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.

expect_list_result(code: int) bool[source]

Check whether a specific HTTP code expects a list result.

Parameters:

code – Status code (example: 200)

find_model(code: int) Type[BaseModelSubclassType] | None[source]
Parameters:

coder'\d{3}'

generate_spec(naming_strategy: ~typing.Callable[[~typing.Type[~spectree._types.BaseModelSubclassType]], 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.

get_expected_list_item_type(code: int) Type[BaseModelSubclassType][source]

Get the expected list result item type.

Parameters:

code – Status code (example: 200)

has_model() bool[source]
Returns:

boolean – does this response has models or not

property models: Iterable[Type[BaseModelSubclassType]]
Returns:

dict_values – all the models in this response