base.py 2.0KB

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