context.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: utf-8 -*-
  2. import typing
  3. from hapic.error import ErrorBuilderInterface
  4. try: # Python 3.5+
  5. from http import HTTPStatus
  6. except ImportError:
  7. from http import client as HTTPStatus
  8. from hapic.processor import RequestParameters
  9. from hapic.processor import ProcessValidationError
  10. if typing.TYPE_CHECKING:
  11. from hapic.decorator import DecoratedController
  12. class RouteRepresentation(object):
  13. def __init__(
  14. self,
  15. rule: str,
  16. method: str,
  17. original_route_object: typing.Any=None,
  18. ) -> None:
  19. self.rule = rule
  20. self.method = method
  21. self.original_route_object = original_route_object
  22. class ContextInterface(object):
  23. def get_request_parameters(self, *args, **kwargs) -> RequestParameters:
  24. raise NotImplementedError()
  25. def get_response(
  26. self,
  27. # TODO BS 20171228: rename into response_content
  28. response: str,
  29. http_code: int,
  30. mimetype: str='application/json',
  31. ) -> typing.Any:
  32. raise NotImplementedError()
  33. def get_validation_error_response(
  34. self,
  35. error: ProcessValidationError,
  36. http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
  37. ) -> typing.Any:
  38. raise NotImplementedError()
  39. def find_route(
  40. self,
  41. decorated_controller: 'DecoratedController',
  42. ) -> RouteRepresentation:
  43. raise NotImplementedError()
  44. # TODO BS 20171228: rename into openapi !
  45. def get_swagger_path(self, contextualised_rule: str) -> str:
  46. """
  47. Return OpenAPI path with context path
  48. TODO BS 20171228: Give example
  49. :param contextualised_rule: path of original context
  50. :return: OpenAPI path
  51. """
  52. raise NotImplementedError()
  53. # TODO BS 20171228: rename into "bypass"
  54. def by_pass_output_wrapping(self, response: typing.Any) -> bool:
  55. """
  56. Return True if the controller response is the final response object:
  57. we do not have to apply any processing on it.
  58. :param response: the original response of controller
  59. :return:
  60. """
  61. raise NotImplementedError()
  62. def get_default_error_builder(self) -> ErrorBuilderInterface:
  63. """
  64. Return a ErrorBuilder who will be used to build default errors
  65. :return: ErrorBuilderInterface instance
  66. """
  67. raise NotImplementedError()
  68. def add_view(
  69. self,
  70. route: str,
  71. http_method: str,
  72. view_func: typing.Callable[..., typing.Any],
  73. ) -> None:
  74. """
  75. This method must permit to add a view in current context
  76. :param route: The route depending of framework format, ex "/foo"
  77. :param http_method: HTTP method like GET, POST, etc ...
  78. :param view_func: The view callable
  79. """
  80. raise NotImplementedError()
  81. def serve_directory(
  82. self,
  83. route_prefix: str,
  84. directory_path: str,
  85. ) -> None:
  86. """
  87. Configure a path to serve a directory content
  88. :param route_prefix: The base url for serve the directory, eg /static
  89. :param directory_path: The file system path
  90. """
  91. raise NotImplementedError()
  92. class BaseContext(ContextInterface):
  93. def get_default_error_builder(self) -> ErrorBuilderInterface:
  94. """ see hapic.context.ContextInterface#get_default_error_builder"""
  95. return self.default_error_builder