context.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. # TODO BS 20171228: rename into response_content
  27. response: dict,
  28. http_code: int,
  29. ) -> typing.Any:
  30. raise NotImplementedError()
  31. def get_validation_error_response(
  32. self,
  33. error: ProcessValidationError,
  34. http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
  35. ) -> typing.Any:
  36. raise NotImplementedError()
  37. def find_route(
  38. self,
  39. decorated_controller: 'DecoratedController',
  40. ) -> RouteRepresentation:
  41. raise NotImplementedError()
  42. # TODO BS 20171228: rename into openapi !
  43. def get_swagger_path(self, contextualised_rule: str) -> str:
  44. """
  45. Return OpenAPI path with context path
  46. TODO BS 20171228: Give example
  47. :param contextualised_rule: path of original context
  48. :return: OpenAPI path
  49. """
  50. raise NotImplementedError()
  51. # TODO BS 20171228: rename into "bypass"
  52. def by_pass_output_wrapping(self, response: typing.Any) -> bool:
  53. """
  54. Return True if the controller response is the final response object:
  55. we do not have to apply any processing on it.
  56. :param response: the original response of controller
  57. :return:
  58. """
  59. raise NotImplementedError()