context.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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: dict,
  29. http_code: int,
  30. ) -> typing.Any:
  31. raise NotImplementedError()
  32. def get_validation_error_response(
  33. self,
  34. error: ProcessValidationError,
  35. http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
  36. ) -> typing.Any:
  37. raise NotImplementedError()
  38. def find_route(
  39. self,
  40. decorated_controller: 'DecoratedController',
  41. ) -> RouteRepresentation:
  42. raise NotImplementedError()
  43. # TODO BS 20171228: rename into openapi !
  44. def get_swagger_path(self, contextualised_rule: str) -> str:
  45. """
  46. Return OpenAPI path with context path
  47. TODO BS 20171228: Give example
  48. :param contextualised_rule: path of original context
  49. :return: OpenAPI path
  50. """
  51. raise NotImplementedError()
  52. # TODO BS 20171228: rename into "bypass"
  53. def by_pass_output_wrapping(self, response: typing.Any) -> bool:
  54. """
  55. Return True if the controller response is the final response object:
  56. we do not have to apply any processing on it.
  57. :param response: the original response of controller
  58. :return:
  59. """
  60. raise NotImplementedError()
  61. def get_default_error_builder(self) -> ErrorBuilderInterface:
  62. """
  63. Return a ErrorBuilder who will be used to build default errors
  64. :return: ErrorBuilderInterface instance
  65. """
  66. raise NotImplementedError()
  67. class BaseContext(ContextInterface):
  68. def get_default_error_builder(self) -> ErrorBuilderInterface:
  69. """ see hapic.context.ContextInterface#get_default_error_builder"""
  70. return self.default_error_builder