plugin.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """Base class for Plugin classes."""
  2. from __future__ import annotations
  3. import typing
  4. from .core import APISpec
  5. from .exceptions import PluginMethodNotImplementedError
  6. class BasePlugin:
  7. """Base class for APISpec plugin classes."""
  8. def init_spec(self, spec: APISpec) -> None:
  9. """Initialize plugin with APISpec object
  10. :param APISpec spec: APISpec object this plugin instance is attached to
  11. """
  12. def schema_helper(
  13. self, name: str, definition: dict, **kwargs: typing.Any
  14. ) -> dict | None:
  15. """May return definition as a dict.
  16. :param str name: Identifier by which schema may be referenced
  17. :param dict definition: Schema definition
  18. :param kwargs: All additional keywords arguments sent to `APISpec.schema()`
  19. """
  20. raise PluginMethodNotImplementedError
  21. def response_helper(self, response: dict, **kwargs: typing.Any) -> dict | None:
  22. """May return response component description as a dict.
  23. :param dict response: Response fields
  24. :param kwargs: All additional keywords arguments sent to `APISpec.response()`
  25. """
  26. raise PluginMethodNotImplementedError
  27. def parameter_helper(self, parameter: dict, **kwargs: typing.Any) -> dict | None:
  28. """May return parameter component description as a dict.
  29. :param dict parameter: Parameter fields
  30. :param kwargs: All additional keywords arguments sent to `APISpec.parameter()`
  31. """
  32. raise PluginMethodNotImplementedError
  33. def header_helper(self, header: dict, **kwargs: typing.Any) -> dict | None:
  34. """May return header component description as a dict.
  35. :param dict header: Header fields
  36. :param kwargs: All additional keywords arguments sent to `APISpec.header()`
  37. """
  38. raise PluginMethodNotImplementedError
  39. def path_helper(
  40. self,
  41. path: str | None = None,
  42. operations: dict | None = None,
  43. parameters: list[dict] | None = None,
  44. **kwargs: typing.Any,
  45. ) -> str | None:
  46. """May return a path as string and mutate operations dict and parameters list.
  47. :param str path: Path to the resource
  48. :param dict operations: A `dict` mapping HTTP methods to operation object. See
  49. https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#operationObject
  50. :param list parameters: A `list` of parameters objects or references for the path. See
  51. https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#parameterObject
  52. and https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#referenceObject
  53. :param kwargs: All additional keywords arguments sent to `APISpec.path()`
  54. Return value should be a string or None. If a string is returned, it
  55. is set as the path.
  56. The last path helper returning a string sets the path value. Therefore,
  57. the order of plugin registration matters. However, generally, registering
  58. several plugins that return a path does not make sense.
  59. """
  60. raise PluginMethodNotImplementedError
  61. def operation_helper(
  62. self,
  63. path: str | None = None,
  64. operations: dict | None = None,
  65. **kwargs: typing.Any,
  66. ) -> None:
  67. """May mutate operations.
  68. :param str path: Path to the resource
  69. :param dict operations: A `dict` mapping HTTP methods to operation object.
  70. See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#operationObject
  71. :param kwargs: All additional keywords arguments sent to `APISpec.path()`
  72. """
  73. raise PluginMethodNotImplementedError