Utils¶
- spectree.utils.default_after_handler(req: Any, resp: Any, resp_validation_error: ValidationError, instance: Any)[source]¶
default handler called after the response validation
- Parameters:
req – request provided by the web framework
resp – response from the endpoint function (if there is no validation error) or response validation error
resp_validation_error – response validation error
instance – class instance if the endpoint function is a class method
- spectree.utils.default_before_handler(req: Any, resp: Any, req_validation_error: ValidationError, instance: Any)[source]¶
default handler called before the endpoint function after the request validation
- Parameters:
req – request provided by the web framework
resp – response generated by SpecTree that will be returned if the validation error is not None
req_validation_error – request validation error
instance – class instance if the endpoint function is a class method
- spectree.utils.flask_response_unpack(resp: Any) Tuple[Any, int, Dict[str, Any]] [source]¶
Parse Flask response object into a tuple of (payload, status_code, headers).
- spectree.utils.gen_list_model(model: Type[BaseModel]) Type[BaseModel] [source]¶
generate the corresponding list[model] class for a given model class
- spectree.utils.get_model_key(model: Type[BaseModelSubclassType]) str [source]¶
generate model name suffixed by short hashed path (instead of its path to avoid code-structure leaking)
- Parameters:
model – pydantic.BaseModel query, json, headers or cookies from request or response
- spectree.utils.get_model_schema(model: ~typing.Type[~spectree._types.BaseModelSubclassType], naming_strategy: ~typing.Callable[[~typing.Type[~spectree._types.BaseModelSubclassType]], str] = <function get_model_key>, nested_naming_strategy: ~typing.Callable[[str, str], str] = <function get_nested_key>)[source]¶
return a dictionary representing the model as JSON Schema with a hashed infix in ref to ensure name uniqueness
- Parameters:
model – pydantic.BaseModel query, json, headers or cookies from request or response
- spectree.utils.get_multidict_items(multidict: MultiDict, model: Type[BaseModelSubclassType] | None = None) Dict[str, None | str | List[str]] [source]¶
return the items of a
werkzeug.datastructures.ImmutableMultiDict
- spectree.utils.get_nested_key(parent: str, child: str) str [source]¶
generate nested model reference name suffixed by parent model name
- Parameters:
parent – string of parent name
child – string of child name
- spectree.utils.get_security(security: None | Mapping | Sequence[Any]) List[Any] [source]¶
return the correct format of security
- spectree.utils.has_model(func: Any) bool [source]¶
return True if this function have
pydantic.BaseModel
- spectree.utils.hash_module_path(module_path: str)[source]¶
generate short hash for module path to avoid the same name object defined in different Python files
- Parameters:
module_path – str module path
- spectree.utils.is_list_item(key: str, model: Type[BaseModelSubclassType] | None) bool [source]¶
Check if this key is a list item in the model.
- spectree.utils.parse_code(http_code: str) str [source]¶
get the code of this HTTP status
- Parameters:
http_code (str) – format like
HTTP_200
- spectree.utils.parse_comments(func: Callable[[...], Any]) Tuple[str | None, str | None] [source]¶
Parse function docstring into a summary and description string.
The first few lines of the docstring up to the first empty line will be extracted as the summary, and the rest of the docstring, following the empty line will become the description.
If the function’s docstring also contains parameter documentation, you can avoid parsing it as part of the summary or description by prefixing it with the “\f” form feed character. Everything after the “\f” character will be ignored in the docstring.
- Parameters:
func – The callable whose docstring should be parsed.
- Returns:
A two element tuple with the summary and the description strings.
- spectree.utils.parse_name(func: Callable[[...], Any]) str [source]¶
the func can be
undecorated functions
decorated functions
decorated class methods
- spectree.utils.parse_params(func: Callable[[...], Any], params: List[Mapping[str, Any]], models: Mapping[str, Any]) List[Mapping[str, Any]] [source]¶
get spec for (query, headers, cookies)
- spectree.utils.parse_resp(func: ~typing.Any, naming_strategy: ~typing.Callable[[~typing.Type[~spectree._types.BaseModelSubclassType]], str] = <function get_model_key>)[source]¶
get the response spec
If this function does not have explicit
resp
but have other models, a422 Validation Error
will be appended to the response spec, since this may be triggered in the validation step.
- spectree.utils.werkzeug_parse_rule(rule: str) Iterator[Tuple[str | None, str | None, str]] [source]¶
A copy of werkzeug.parse_rule which is now removed.
Parse a rule and return it as generator. Each iteration yields tuples in the form
(converter, arguments, variable)
. If the converter is None it’s a static url part, otherwise it’s a dynamic one.