context.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. import typing
  3. try: # Python 3.5+
  4. from http import HTTPStatus
  5. except ImportError:
  6. from http import client as HTTPStatus
  7. from hapic.processor import RequestParameters
  8. from hapic.processor import ProcessValidationError
  9. if typing.TYPE_CHECKING:
  10. from hapic.decorator import DecoratedController
  11. class RouteRepresentation(object):
  12. def __init__(
  13. self,
  14. rule: str,
  15. method: str,
  16. original_route_object: typing.Any=None,
  17. ) -> None:
  18. self.rule = rule
  19. self.method = method
  20. self.original_route_object = original_route_object
  21. class ContextInterface(object):
  22. def get_request_parameters(self, *args, **kwargs) -> RequestParameters:
  23. raise NotImplementedError()
  24. def get_response(
  25. self,
  26. response: dict,
  27. http_code: int,
  28. ) -> typing.Any:
  29. raise NotImplementedError()
  30. def get_validation_error_response(
  31. self,
  32. error: ProcessValidationError,
  33. http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
  34. ) -> typing.Any:
  35. raise NotImplementedError()
  36. def find_route(
  37. self,
  38. decorated_controller: 'DecoratedController',
  39. ) -> RouteRepresentation:
  40. raise NotImplementedError()
  41. def get_swagger_path(self, contextualised_rule: str) -> str:
  42. """
  43. Return OpenAPI path with context path
  44. :param contextualised_rule: path of original context
  45. :return: OpenAPI path
  46. """
  47. raise NotImplementedError()
  48. def by_pass_output_wrapping(self, response: typing.Any) -> bool:
  49. """
  50. Return True if the controller response is in final state: we do not
  51. have to apply output wrapper on it.
  52. :param response: the original response of controller
  53. :return:
  54. """
  55. raise NotImplementedError()