base.py 2.0KB

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