context.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import re
  4. import typing
  5. try: # Python 3.5+
  6. from http import HTTPStatus
  7. except ImportError:
  8. from http import client as HTTPStatus
  9. from hapic.context import ContextInterface
  10. from hapic.context import RouteRepresentation
  11. from hapic.decorator import DecoratedController
  12. from hapic.decorator import DECORATION_ATTRIBUTE_NAME
  13. from hapic.exception import OutputValidationException
  14. from hapic.processor import RequestParameters, ProcessValidationError
  15. from flask import Flask
  16. if typing.TYPE_CHECKING:
  17. from flask import Response
  18. # flask regular expression to locate url parameters
  19. FLASK_RE_PATH_URL = re.compile(r'<(?:[^:<>]+:)?([^<>]+)>')
  20. class FlaskContext(ContextInterface):
  21. def __init__(self, app: Flask):
  22. self.app = app
  23. def get_request_parameters(self, *args, **kwargs) -> RequestParameters:
  24. from flask import request
  25. return RequestParameters(
  26. path_parameters=request.view_args,
  27. query_parameters=request.args, # TODO: Check
  28. body_parameters=request.get_json(), # TODO: Check
  29. form_parameters=request.form,
  30. header_parameters=request.headers,
  31. files_parameters={}, # TODO: BS 20171115: Code it
  32. )
  33. def get_response(
  34. self,
  35. response: dict,
  36. http_code: int,
  37. ) -> 'Response':
  38. from flask import Response
  39. return Response(
  40. response=json.dumps(response),
  41. mimetype='application/json',
  42. status=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. # TODO BS 20171010: Manage error schemas, see #4
  50. from hapic.hapic import _default_global_error_schema
  51. unmarshall = _default_global_error_schema.dump(error)
  52. if unmarshall.errors:
  53. raise OutputValidationException(
  54. 'Validation error during dump of error response: {}'.format(
  55. str(unmarshall.errors)
  56. )
  57. )
  58. from flask import Response
  59. return Response(
  60. response=json.dumps(unmarshall.data),
  61. mimetype='application/json',
  62. status=int(http_code),
  63. )
  64. def find_route(
  65. self,
  66. decorated_controller: 'DecoratedController',
  67. ):
  68. reference = decorated_controller.reference
  69. for route in self.app.url_map.iter_rules():
  70. if route.endpoint not in self.app.view_functions:
  71. continue
  72. route_callback = self.app.view_functions[route.endpoint]
  73. route_token = getattr(
  74. route_callback,
  75. DECORATION_ATTRIBUTE_NAME,
  76. None,
  77. )
  78. match_with_wrapper = route_callback == reference.wrapper
  79. match_with_wrapped = route_callback == reference.wrapped
  80. match_with_token = route_token == reference.token
  81. # FIXME - G.M - 2017-12-04 - return list instead of one method
  82. # This fix, return only 1 allowed method, change this when
  83. # RouteRepresentation is adapted to return multiples methods.
  84. method = [x for x in route.methods
  85. if x not in ['OPTIONS', 'HEAD']][0]
  86. if match_with_wrapper or match_with_wrapped or match_with_token:
  87. return RouteRepresentation(
  88. rule=self.get_swagger_path(route.rule),
  89. method=method,
  90. original_route_object=route,
  91. )
  92. def get_swagger_path(self, contextualised_rule: str) -> str:
  93. # TODO - G.M - 2017-12-05 Check if all route path are handled correctly
  94. return FLASK_RE_PATH_URL.sub(r'{\1}', contextualised_rule)
  95. def by_pass_output_wrapping(self, response: typing.Any) -> bool:
  96. return False