base.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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: str,
  44. http_code: int,
  45. mimetype: str='application/json',
  46. ) -> typing.Any:
  47. return {
  48. 'original_response': response,
  49. 'http_code': http_code,
  50. }
  51. def get_validation_error_response(
  52. self,
  53. error: ProcessValidationError,
  54. http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
  55. ) -> typing.Any:
  56. return {
  57. 'original_error': error,
  58. 'http_code': http_code,
  59. }