base.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. import typing
  3. from http import HTTPStatus
  4. from hapic.context import ContextInterface
  5. from hapic.processor import RequestParameters
  6. from hapic.processor import ProcessValidationError
  7. class Base(object):
  8. pass
  9. class MyContext(ContextInterface):
  10. def __init__(
  11. self,
  12. fake_path_parameters=None,
  13. fake_query_parameters=None,
  14. fake_body_parameters=None,
  15. fake_form_parameters=None,
  16. fake_header_parameters=None,
  17. fake_files_parameters=None,
  18. ) -> None:
  19. self.fake_path_parameters = fake_path_parameters or {}
  20. self.fake_query_parameters = fake_query_parameters or {}
  21. self.fake_body_parameters = fake_body_parameters or {}
  22. self.fake_form_parameters = fake_form_parameters or {}
  23. self.fake_header_parameters = fake_header_parameters or {}
  24. self.fake_files_parameters = fake_files_parameters or {}
  25. def get_request_parameters(self, *args, **kwargs) -> RequestParameters:
  26. return RequestParameters(
  27. path_parameters=self.fake_path_parameters,
  28. query_parameters=self.fake_query_parameters,
  29. body_parameters=self.fake_body_parameters,
  30. form_parameters=self.fake_form_parameters,
  31. header_parameters=self.fake_header_parameters,
  32. files_parameters=self.fake_files_parameters,
  33. )
  34. def get_response(
  35. self,
  36. response: dict,
  37. http_code: int,
  38. ) -> typing.Any:
  39. return {
  40. 'original_response': response,
  41. 'http_code': http_code,
  42. }
  43. def get_validation_error_response(
  44. self,
  45. error: ProcessValidationError,
  46. http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
  47. ) -> typing.Any:
  48. return {
  49. 'original_error': error,
  50. 'http_code': http_code,
  51. }