base.py 1.9KB

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