api.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import difflib
  4. import inspect
  5. from itertools import chain
  6. import logging
  7. import operator
  8. import re
  9. import six
  10. import sys
  11. import warnings
  12. from collections import OrderedDict
  13. from functools import wraps, partial
  14. from types import MethodType
  15. from flask import url_for, request, current_app
  16. from flask import make_response as original_flask_make_response
  17. try:
  18. from flask.helpers import _endpoint_from_view_func
  19. except ImportError:
  20. from flask.scaffold import _endpoint_from_view_func
  21. from flask.signals import got_request_exception
  22. from jsonschema import RefResolver
  23. from werkzeug.utils import cached_property
  24. from werkzeug.datastructures import Headers
  25. from werkzeug.exceptions import (
  26. HTTPException,
  27. MethodNotAllowed,
  28. NotFound,
  29. NotAcceptable,
  30. InternalServerError,
  31. )
  32. from werkzeug import __version__ as werkzeug_version
  33. if werkzeug_version.split('.')[0] >= '2':
  34. from werkzeug.wrappers import Response as BaseResponse
  35. else:
  36. from werkzeug.wrappers import BaseResponse
  37. from . import apidoc
  38. from .mask import ParseError, MaskError
  39. from .namespace import Namespace
  40. from .postman import PostmanCollectionV1
  41. from .resource import Resource
  42. from .swagger import Swagger
  43. from .utils import default_id, camel_to_dash, unpack
  44. from .representations import output_json
  45. from ._http import HTTPStatus
  46. RE_RULES = re.compile("(<.*>)")
  47. # List headers that should never be handled by Flask-RESTX
  48. HEADERS_BLACKLIST = ("Content-Length",)
  49. DEFAULT_REPRESENTATIONS = [("application/json", output_json)]
  50. log = logging.getLogger(__name__)
  51. class Api(object):
  52. """
  53. The main entry point for the application.
  54. You need to initialize it with a Flask Application: ::
  55. >>> app = Flask(__name__)
  56. >>> api = Api(app)
  57. Alternatively, you can use :meth:`init_app` to set the Flask application
  58. after it has been constructed.
  59. The endpoint parameter prefix all views and resources:
  60. - The API root/documentation will be ``{endpoint}.root``
  61. - A resource registered as 'resource' will be available as ``{endpoint}.resource``
  62. :param flask.Flask|flask.Blueprint app: the Flask application object or a Blueprint
  63. :param str version: The API version (used in Swagger documentation)
  64. :param str title: The API title (used in Swagger documentation)
  65. :param str description: The API description (used in Swagger documentation)
  66. :param str terms_url: The API terms page URL (used in Swagger documentation)
  67. :param str contact: A contact email for the API (used in Swagger documentation)
  68. :param str license: The license associated to the API (used in Swagger documentation)
  69. :param str license_url: The license page URL (used in Swagger documentation)
  70. :param str endpoint: The API base endpoint (default to 'api).
  71. :param str default: The default namespace base name (default to 'default')
  72. :param str default_label: The default namespace label (used in Swagger documentation)
  73. :param str default_mediatype: The default media type to return
  74. :param bool validate: Whether or not the API should perform input payload validation.
  75. :param bool ordered: Whether or not preserve order models and marshalling.
  76. :param str doc: The documentation path. If set to a false value, documentation is disabled.
  77. (Default to '/')
  78. :param list decorators: Decorators to attach to every resource
  79. :param bool catch_all_404s: Use :meth:`handle_error`
  80. to handle 404 errors throughout your app
  81. :param dict authorizations: A Swagger Authorizations declaration as dictionary
  82. :param bool serve_challenge_on_401: Serve basic authentication challenge with 401
  83. responses (default 'False')
  84. :param FormatChecker format_checker: A jsonschema.FormatChecker object that is hooked into
  85. the Model validator. A default or a custom FormatChecker can be provided (e.g., with custom
  86. checkers), otherwise the default action is to not enforce any format validation.
  87. :param url_scheme: If set to a string (e.g. http, https), then the specs_url and base_url will explicitly use this
  88. scheme regardless of how the application is deployed. This is necessary for some deployments behind a reverse
  89. proxy.
  90. """
  91. def __init__(
  92. self,
  93. app=None,
  94. version="1.0",
  95. title=None,
  96. description=None,
  97. terms_url=None,
  98. license=None,
  99. license_url=None,
  100. contact=None,
  101. contact_url=None,
  102. contact_email=None,
  103. authorizations=None,
  104. security=None,
  105. doc="/",
  106. default_id=default_id,
  107. default="default",
  108. default_label="Default namespace",
  109. validate=None,
  110. tags=None,
  111. prefix="",
  112. ordered=False,
  113. default_mediatype="application/json",
  114. decorators=None,
  115. catch_all_404s=False,
  116. serve_challenge_on_401=False,
  117. format_checker=None,
  118. url_scheme=None,
  119. **kwargs
  120. ):
  121. self.version = version
  122. self.title = title or "API"
  123. self.description = description
  124. self.terms_url = terms_url
  125. self.contact = contact
  126. self.contact_email = contact_email
  127. self.contact_url = contact_url
  128. self.license = license
  129. self.license_url = license_url
  130. self.authorizations = authorizations
  131. self.security = security
  132. self.default_id = default_id
  133. self.ordered = ordered
  134. self._validate = validate
  135. self._doc = doc
  136. self._doc_view = None
  137. self._default_error_handler = None
  138. self.tags = tags or []
  139. self.error_handlers = OrderedDict({
  140. ParseError: mask_parse_error_handler,
  141. MaskError: mask_error_handler,
  142. })
  143. self._schema = None
  144. self.models = {}
  145. self._refresolver = None
  146. self.format_checker = format_checker
  147. self.namespaces = []
  148. self.ns_paths = dict()
  149. self.representations = OrderedDict(DEFAULT_REPRESENTATIONS)
  150. self.urls = {}
  151. self.prefix = prefix
  152. self.default_mediatype = default_mediatype
  153. self.decorators = decorators if decorators else []
  154. self.catch_all_404s = catch_all_404s
  155. self.serve_challenge_on_401 = serve_challenge_on_401
  156. self.blueprint_setup = None
  157. self.endpoints = set()
  158. self.resources = []
  159. self.app = None
  160. self.blueprint = None
  161. # must come after self.app initialisation to prevent __getattr__ recursion
  162. # in self._configure_namespace_logger
  163. self.default_namespace = self.namespace(
  164. default,
  165. default_label,
  166. endpoint="{0}-declaration".format(default),
  167. validate=validate,
  168. api=self,
  169. path="/",
  170. )
  171. self.url_scheme = url_scheme
  172. if app is not None:
  173. self.app = app
  174. self.init_app(app)
  175. # super(Api, self).__init__(app, **kwargs)
  176. def init_app(self, app, **kwargs):
  177. """
  178. Allow to lazy register the API on a Flask application::
  179. >>> app = Flask(__name__)
  180. >>> api = Api()
  181. >>> api.init_app(app)
  182. :param flask.Flask app: the Flask application object
  183. :param str title: The API title (used in Swagger documentation)
  184. :param str description: The API description (used in Swagger documentation)
  185. :param str terms_url: The API terms page URL (used in Swagger documentation)
  186. :param str contact: A contact email for the API (used in Swagger documentation)
  187. :param str license: The license associated to the API (used in Swagger documentation)
  188. :param str license_url: The license page URL (used in Swagger documentation)
  189. :param url_scheme: If set to a string (e.g. http, https), then the specs_url and base_url will explicitly use
  190. this scheme regardless of how the application is deployed. This is necessary for some deployments behind a
  191. reverse proxy.
  192. """
  193. self.app = app
  194. self.title = kwargs.get("title", self.title)
  195. self.description = kwargs.get("description", self.description)
  196. self.terms_url = kwargs.get("terms_url", self.terms_url)
  197. self.contact = kwargs.get("contact", self.contact)
  198. self.contact_url = kwargs.get("contact_url", self.contact_url)
  199. self.contact_email = kwargs.get("contact_email", self.contact_email)
  200. self.license = kwargs.get("license", self.license)
  201. self.license_url = kwargs.get("license_url", self.license_url)
  202. self.url_scheme = kwargs.get("url_scheme", self.url_scheme)
  203. self._add_specs = kwargs.get("add_specs", True)
  204. # If app is a blueprint, defer the initialization
  205. try:
  206. app.record(self._deferred_blueprint_init)
  207. # Flask.Blueprint has a 'record' attribute, Flask.Api does not
  208. except AttributeError:
  209. self._init_app(app)
  210. else:
  211. self.blueprint = app
  212. def _init_app(self, app):
  213. """
  214. Perform initialization actions with the given :class:`flask.Flask` object.
  215. :param flask.Flask app: The flask application object
  216. """
  217. self._register_specs(self.blueprint or app)
  218. self._register_doc(self.blueprint or app)
  219. app.handle_exception = partial(self.error_router, app.handle_exception)
  220. app.handle_user_exception = partial(
  221. self.error_router, app.handle_user_exception
  222. )
  223. if len(self.resources) > 0:
  224. for resource, namespace, urls, kwargs in self.resources:
  225. self._register_view(app, resource, namespace, *urls, **kwargs)
  226. for ns in self.namespaces:
  227. self._configure_namespace_logger(app, ns)
  228. self._register_apidoc(app)
  229. self._validate = (
  230. self._validate
  231. if self._validate is not None
  232. else app.config.get("RESTX_VALIDATE", False)
  233. )
  234. app.config.setdefault("RESTX_MASK_HEADER", "X-Fields")
  235. app.config.setdefault("RESTX_MASK_SWAGGER", True)
  236. app.config.setdefault("RESTX_INCLUDE_ALL_MODELS", False)
  237. # check for deprecated config variable names
  238. if "ERROR_404_HELP" in app.config:
  239. app.config['RESTX_ERROR_404_HELP'] = app.config['ERROR_404_HELP']
  240. warnings.warn(
  241. "'ERROR_404_HELP' config setting is deprecated and will be "
  242. "removed in the future. Use 'RESTX_ERROR_404_HELP' instead.",
  243. DeprecationWarning
  244. )
  245. def __getattr__(self, name):
  246. try:
  247. return getattr(self.default_namespace, name)
  248. except AttributeError:
  249. raise AttributeError("Api does not have {0} attribute".format(name))
  250. def _complete_url(self, url_part, registration_prefix):
  251. """
  252. This method is used to defer the construction of the final url in
  253. the case that the Api is created with a Blueprint.
  254. :param url_part: The part of the url the endpoint is registered with
  255. :param registration_prefix: The part of the url contributed by the
  256. blueprint. Generally speaking, BlueprintSetupState.url_prefix
  257. """
  258. parts = (registration_prefix, self.prefix, url_part)
  259. return "".join(part for part in parts if part)
  260. def _register_apidoc(self, app):
  261. conf = app.extensions.setdefault("restx", {})
  262. if not conf.get("apidoc_registered", False):
  263. app.register_blueprint(apidoc.apidoc)
  264. conf["apidoc_registered"] = True
  265. def _register_specs(self, app_or_blueprint):
  266. if self._add_specs:
  267. endpoint = str("specs")
  268. self._register_view(
  269. app_or_blueprint,
  270. SwaggerView,
  271. self.default_namespace,
  272. "/swagger.json",
  273. endpoint=endpoint,
  274. resource_class_args=(self,),
  275. )
  276. self.endpoints.add(endpoint)
  277. def _register_doc(self, app_or_blueprint):
  278. if self._add_specs and self._doc:
  279. # Register documentation before root if enabled
  280. app_or_blueprint.add_url_rule(self._doc, "doc", self.render_doc)
  281. app_or_blueprint.add_url_rule(self.prefix or "/", "root", self.render_root)
  282. def register_resource(self, namespace, resource, *urls, **kwargs):
  283. endpoint = kwargs.pop("endpoint", None)
  284. endpoint = str(endpoint or self.default_endpoint(resource, namespace))
  285. kwargs["endpoint"] = endpoint
  286. self.endpoints.add(endpoint)
  287. if self.app is not None:
  288. self._register_view(self.app, resource, namespace, *urls, **kwargs)
  289. else:
  290. self.resources.append((resource, namespace, urls, kwargs))
  291. return endpoint
  292. def _configure_namespace_logger(self, app, namespace):
  293. for handler in app.logger.handlers:
  294. namespace.logger.addHandler(handler)
  295. namespace.logger.setLevel(app.logger.level)
  296. def _register_view(self, app, resource, namespace, *urls, **kwargs):
  297. endpoint = kwargs.pop("endpoint", None) or camel_to_dash(resource.__name__)
  298. resource_class_args = kwargs.pop("resource_class_args", ())
  299. resource_class_kwargs = kwargs.pop("resource_class_kwargs", {})
  300. # NOTE: 'view_functions' is cleaned up from Blueprint class in Flask 1.0
  301. if endpoint in getattr(app, "view_functions", {}):
  302. previous_view_class = app.view_functions[endpoint].__dict__["view_class"]
  303. # if you override the endpoint with a different class, avoid the
  304. # collision by raising an exception
  305. if previous_view_class != resource:
  306. msg = "This endpoint (%s) is already set to the class %s."
  307. raise ValueError(msg % (endpoint, previous_view_class.__name__))
  308. resource.mediatypes = self.mediatypes_method() # Hacky
  309. resource.endpoint = endpoint
  310. resource_func = self.output(
  311. resource.as_view(
  312. endpoint, self, *resource_class_args, **resource_class_kwargs
  313. )
  314. )
  315. # Apply Namespace and Api decorators to a resource
  316. for decorator in chain(namespace.decorators, self.decorators):
  317. resource_func = decorator(resource_func)
  318. for url in urls:
  319. # If this Api has a blueprint
  320. if self.blueprint:
  321. # And this Api has been setup
  322. if self.blueprint_setup:
  323. # Set the rule to a string directly, as the blueprint is already
  324. # set up.
  325. self.blueprint_setup.add_url_rule(
  326. url, view_func=resource_func, **kwargs
  327. )
  328. continue
  329. else:
  330. # Set the rule to a function that expects the blueprint prefix
  331. # to construct the final url. Allows deferment of url finalization
  332. # in the case that the associated Blueprint has not yet been
  333. # registered to an application, so we can wait for the registration
  334. # prefix
  335. rule = partial(self._complete_url, url)
  336. else:
  337. # If we've got no Blueprint, just build a url with no prefix
  338. rule = self._complete_url(url, "")
  339. # Add the url to the application or blueprint
  340. app.add_url_rule(rule, view_func=resource_func, **kwargs)
  341. def output(self, resource):
  342. """
  343. Wraps a resource (as a flask view function),
  344. for cases where the resource does not directly return a response object
  345. :param resource: The resource as a flask view function
  346. """
  347. @wraps(resource)
  348. def wrapper(*args, **kwargs):
  349. resp = resource(*args, **kwargs)
  350. if isinstance(resp, BaseResponse):
  351. return resp
  352. data, code, headers = unpack(resp)
  353. return self.make_response(data, code, headers=headers)
  354. return wrapper
  355. def make_response(self, data, *args, **kwargs):
  356. """
  357. Looks up the representation transformer for the requested media
  358. type, invoking the transformer to create a response object. This
  359. defaults to default_mediatype if no transformer is found for the
  360. requested mediatype. If default_mediatype is None, a 406 Not
  361. Acceptable response will be sent as per RFC 2616 section 14.1
  362. :param data: Python object containing response data to be transformed
  363. """
  364. default_mediatype = (
  365. kwargs.pop("fallback_mediatype", None) or self.default_mediatype
  366. )
  367. mediatype = request.accept_mimetypes.best_match(
  368. self.representations, default=default_mediatype,
  369. )
  370. if mediatype is None:
  371. raise NotAcceptable()
  372. if mediatype in self.representations:
  373. resp = self.representations[mediatype](data, *args, **kwargs)
  374. resp.headers["Content-Type"] = mediatype
  375. return resp
  376. elif mediatype == "text/plain":
  377. resp = original_flask_make_response(str(data), *args, **kwargs)
  378. resp.headers["Content-Type"] = "text/plain"
  379. return resp
  380. else:
  381. raise InternalServerError()
  382. def documentation(self, func):
  383. """A decorator to specify a view function for the documentation"""
  384. self._doc_view = func
  385. return func
  386. def render_root(self):
  387. self.abort(HTTPStatus.NOT_FOUND)
  388. def render_doc(self):
  389. """Override this method to customize the documentation page"""
  390. if self._doc_view:
  391. return self._doc_view()
  392. elif not self._doc:
  393. self.abort(HTTPStatus.NOT_FOUND)
  394. return apidoc.ui_for(self)
  395. def default_endpoint(self, resource, namespace):
  396. """
  397. Provide a default endpoint for a resource on a given namespace.
  398. Endpoints are ensured not to collide.
  399. Override this method specify a custom algorithm for default endpoint.
  400. :param Resource resource: the resource for which we want an endpoint
  401. :param Namespace namespace: the namespace holding the resource
  402. :returns str: An endpoint name
  403. """
  404. endpoint = camel_to_dash(resource.__name__)
  405. if namespace is not self.default_namespace:
  406. endpoint = "{ns.name}_{endpoint}".format(ns=namespace, endpoint=endpoint)
  407. if endpoint in self.endpoints:
  408. suffix = 2
  409. while True:
  410. new_endpoint = "{base}_{suffix}".format(base=endpoint, suffix=suffix)
  411. if new_endpoint not in self.endpoints:
  412. endpoint = new_endpoint
  413. break
  414. suffix += 1
  415. return endpoint
  416. def get_ns_path(self, ns):
  417. return self.ns_paths.get(ns)
  418. def ns_urls(self, ns, urls):
  419. path = self.get_ns_path(ns) or ns.path
  420. return [path + url for url in urls]
  421. def add_namespace(self, ns, path=None):
  422. """
  423. This method registers resources from namespace for current instance of api.
  424. You can use argument path for definition custom prefix url for namespace.
  425. :param Namespace ns: the namespace
  426. :param path: registration prefix of namespace
  427. """
  428. if ns not in self.namespaces:
  429. self.namespaces.append(ns)
  430. if self not in ns.apis:
  431. ns.apis.append(self)
  432. # Associate ns with prefix-path
  433. if path is not None:
  434. self.ns_paths[ns] = path
  435. # Register resources
  436. for r in ns.resources:
  437. urls = self.ns_urls(ns, r.urls)
  438. self.register_resource(ns, r.resource, *urls, **r.kwargs)
  439. # Register models
  440. for name, definition in six.iteritems(ns.models):
  441. self.models[name] = definition
  442. if not self.blueprint and self.app is not None:
  443. self._configure_namespace_logger(self.app, ns)
  444. def namespace(self, *args, **kwargs):
  445. """
  446. A namespace factory.
  447. :returns Namespace: a new namespace instance
  448. """
  449. kwargs["ordered"] = kwargs.get("ordered", self.ordered)
  450. ns = Namespace(*args, **kwargs)
  451. self.add_namespace(ns)
  452. return ns
  453. def endpoint(self, name):
  454. if self.blueprint:
  455. return "{0}.{1}".format(self.blueprint.name, name)
  456. else:
  457. return name
  458. @property
  459. def specs_url(self):
  460. """
  461. The Swagger specifications relative url (ie. `swagger.json`). If
  462. the spec_url_scheme attribute is set, then the full url is provided instead
  463. (e.g. http://localhost/swaggger.json).
  464. :rtype: str
  465. """
  466. external = None if self.url_scheme is None else True
  467. return url_for(
  468. self.endpoint("specs"), _scheme=self.url_scheme, _external=external
  469. )
  470. @property
  471. def base_url(self):
  472. """
  473. The API base absolute url
  474. :rtype: str
  475. """
  476. return url_for(self.endpoint("root"), _scheme=self.url_scheme, _external=True)
  477. @property
  478. def base_path(self):
  479. """
  480. The API path
  481. :rtype: str
  482. """
  483. return url_for(self.endpoint("root"), _external=False)
  484. @cached_property
  485. def __schema__(self):
  486. """
  487. The Swagger specifications/schema for this API
  488. :returns dict: the schema as a serializable dict
  489. """
  490. if not self._schema:
  491. try:
  492. self._schema = Swagger(self).as_dict()
  493. except Exception:
  494. # Log the source exception for debugging purpose
  495. # and return an error message
  496. msg = "Unable to render schema"
  497. log.exception(msg) # This will provide a full traceback
  498. return {"error": msg}
  499. return self._schema
  500. @property
  501. def _own_and_child_error_handlers(self):
  502. rv = OrderedDict()
  503. rv.update(self.error_handlers)
  504. for ns in self.namespaces:
  505. for exception, handler in six.iteritems(ns.error_handlers):
  506. rv[exception] = handler
  507. return rv
  508. def errorhandler(self, exception):
  509. """A decorator to register an error handler for a given exception"""
  510. if inspect.isclass(exception) and issubclass(exception, Exception):
  511. # Register an error handler for a given exception
  512. def wrapper(func):
  513. self.error_handlers[exception] = func
  514. return func
  515. return wrapper
  516. else:
  517. # Register the default error handler
  518. self._default_error_handler = exception
  519. return exception
  520. def owns_endpoint(self, endpoint):
  521. """
  522. Tests if an endpoint name (not path) belongs to this Api.
  523. Takes into account the Blueprint name part of the endpoint name.
  524. :param str endpoint: The name of the endpoint being checked
  525. :return: bool
  526. """
  527. if self.blueprint:
  528. if endpoint.startswith(self.blueprint.name):
  529. endpoint = endpoint.split(self.blueprint.name + ".", 1)[-1]
  530. else:
  531. return False
  532. return endpoint in self.endpoints
  533. def _should_use_fr_error_handler(self):
  534. """
  535. Determine if error should be handled with FR or default Flask
  536. The goal is to return Flask error handlers for non-FR-related routes,
  537. and FR errors (with the correct media type) for FR endpoints. This
  538. method currently handles 404 and 405 errors.
  539. :return: bool
  540. """
  541. adapter = current_app.create_url_adapter(request)
  542. try:
  543. adapter.match()
  544. except MethodNotAllowed as e:
  545. # Check if the other HTTP methods at this url would hit the Api
  546. valid_route_method = e.valid_methods[0]
  547. rule, _ = adapter.match(method=valid_route_method, return_rule=True)
  548. return self.owns_endpoint(rule.endpoint)
  549. except NotFound:
  550. return self.catch_all_404s
  551. except Exception:
  552. # Werkzeug throws other kinds of exceptions, such as Redirect
  553. pass
  554. def _has_fr_route(self):
  555. """Encapsulating the rules for whether the request was to a Flask endpoint"""
  556. # 404's, 405's, which might not have a url_rule
  557. if self._should_use_fr_error_handler():
  558. return True
  559. # for all other errors, just check if FR dispatched the route
  560. if not request.url_rule:
  561. return False
  562. return self.owns_endpoint(request.url_rule.endpoint)
  563. def error_router(self, original_handler, e):
  564. """
  565. This function decides whether the error occurred in a flask-restx
  566. endpoint or not. If it happened in a flask-restx endpoint, our
  567. handler will be dispatched. If it happened in an unrelated view, the
  568. app's original error handler will be dispatched.
  569. In the event that the error occurred in a flask-restx endpoint but
  570. the local handler can't resolve the situation, the router will fall
  571. back onto the original_handler as last resort.
  572. :param function original_handler: the original Flask error handler for the app
  573. :param Exception e: the exception raised while handling the request
  574. """
  575. if self._has_fr_route():
  576. try:
  577. return self.handle_error(e)
  578. except Exception as f:
  579. return original_handler(f)
  580. return original_handler(e)
  581. def handle_error(self, e):
  582. """
  583. Error handler for the API transforms a raised exception into a Flask response,
  584. with the appropriate HTTP status code and body.
  585. :param Exception e: the raised Exception object
  586. """
  587. # When propagate_exceptions is set, do not return the exception to the
  588. # client if a handler is configured for the exception.
  589. if (
  590. not isinstance(e, HTTPException)
  591. and current_app.propagate_exceptions
  592. and not isinstance(e, tuple(self._own_and_child_error_handlers.keys()))
  593. ):
  594. exc_type, exc_value, tb = sys.exc_info()
  595. if exc_value is e:
  596. raise
  597. else:
  598. raise e
  599. include_message_in_response = current_app.config.get(
  600. "ERROR_INCLUDE_MESSAGE", True
  601. )
  602. default_data = {}
  603. headers = Headers()
  604. for typecheck, handler in six.iteritems(self._own_and_child_error_handlers):
  605. if isinstance(e, typecheck):
  606. result = handler(e)
  607. default_data, code, headers = unpack(
  608. result, HTTPStatus.INTERNAL_SERVER_ERROR
  609. )
  610. break
  611. else:
  612. # Flask docs say: "This signal is not sent for HTTPException or other exceptions that have error handlers
  613. # registered, unless the exception was raised from an error handler."
  614. got_request_exception.send(current_app._get_current_object(), exception=e)
  615. if isinstance(e, HTTPException):
  616. code = HTTPStatus(e.code)
  617. if include_message_in_response:
  618. default_data = {"message": getattr(e, "description", code.phrase)}
  619. headers = e.get_response().headers
  620. elif self._default_error_handler:
  621. result = self._default_error_handler(e)
  622. default_data, code, headers = unpack(
  623. result, HTTPStatus.INTERNAL_SERVER_ERROR
  624. )
  625. else:
  626. code = HTTPStatus.INTERNAL_SERVER_ERROR
  627. if include_message_in_response:
  628. default_data = {
  629. "message": code.phrase,
  630. }
  631. if include_message_in_response:
  632. default_data["message"] = default_data.get("message", str(e))
  633. data = getattr(e, "data", default_data)
  634. fallback_mediatype = None
  635. if code >= HTTPStatus.INTERNAL_SERVER_ERROR:
  636. exc_info = sys.exc_info()
  637. if exc_info[1] is None:
  638. exc_info = None
  639. current_app.log_exception(exc_info)
  640. elif (
  641. code == HTTPStatus.NOT_FOUND
  642. and current_app.config.get("RESTX_ERROR_404_HELP", True)
  643. and include_message_in_response
  644. ):
  645. data["message"] = self._help_on_404(data.get("message", None))
  646. elif code == HTTPStatus.NOT_ACCEPTABLE and self.default_mediatype is None:
  647. # if we are handling NotAcceptable (406), make sure that
  648. # make_response uses a representation we support as the
  649. # default mediatype (so that make_response doesn't throw
  650. # another NotAcceptable error).
  651. supported_mediatypes = list(self.representations.keys())
  652. fallback_mediatype = (
  653. supported_mediatypes[0] if supported_mediatypes else "text/plain"
  654. )
  655. # Remove blacklisted headers
  656. for header in HEADERS_BLACKLIST:
  657. headers.pop(header, None)
  658. resp = self.make_response(
  659. data, code, headers, fallback_mediatype=fallback_mediatype
  660. )
  661. if code == HTTPStatus.UNAUTHORIZED:
  662. resp = self.unauthorized(resp)
  663. return resp
  664. def _help_on_404(self, message=None):
  665. rules = dict(
  666. [
  667. (RE_RULES.sub("", rule.rule), rule.rule)
  668. for rule in current_app.url_map.iter_rules()
  669. ]
  670. )
  671. close_matches = difflib.get_close_matches(request.path, rules.keys())
  672. if close_matches:
  673. # If we already have a message, add punctuation and continue it.
  674. message = "".join(
  675. (
  676. (message.rstrip(".") + ". ") if message else "",
  677. "You have requested this URI [",
  678. request.path,
  679. "] but did you mean ",
  680. " or ".join((rules[match] for match in close_matches)),
  681. " ?",
  682. )
  683. )
  684. return message
  685. def as_postman(self, urlvars=False, swagger=False):
  686. """
  687. Serialize the API as Postman collection (v1)
  688. :param bool urlvars: whether to include or not placeholders for query strings
  689. :param bool swagger: whether to include or not the swagger.json specifications
  690. """
  691. return PostmanCollectionV1(self, swagger=swagger).as_dict(urlvars=urlvars)
  692. @property
  693. def payload(self):
  694. """Store the input payload in the current request context"""
  695. return request.get_json()
  696. @property
  697. def refresolver(self):
  698. if not self._refresolver:
  699. self._refresolver = RefResolver.from_schema(self.__schema__)
  700. return self._refresolver
  701. @staticmethod
  702. def _blueprint_setup_add_url_rule_patch(
  703. blueprint_setup, rule, endpoint=None, view_func=None, **options
  704. ):
  705. """
  706. Method used to patch BlueprintSetupState.add_url_rule for setup
  707. state instance corresponding to this Api instance. Exists primarily
  708. to enable _complete_url's function.
  709. :param blueprint_setup: The BlueprintSetupState instance (self)
  710. :param rule: A string or callable that takes a string and returns a
  711. string(_complete_url) that is the url rule for the endpoint
  712. being registered
  713. :param endpoint: See BlueprintSetupState.add_url_rule
  714. :param view_func: See BlueprintSetupState.add_url_rule
  715. :param **options: See BlueprintSetupState.add_url_rule
  716. """
  717. if callable(rule):
  718. rule = rule(blueprint_setup.url_prefix)
  719. elif blueprint_setup.url_prefix:
  720. rule = blueprint_setup.url_prefix + rule
  721. options.setdefault("subdomain", blueprint_setup.subdomain)
  722. if endpoint is None:
  723. endpoint = _endpoint_from_view_func(view_func)
  724. defaults = blueprint_setup.url_defaults
  725. if "defaults" in options:
  726. defaults = dict(defaults, **options.pop("defaults"))
  727. blueprint_setup.app.add_url_rule(
  728. rule,
  729. "%s.%s" % (blueprint_setup.blueprint.name, endpoint),
  730. view_func,
  731. defaults=defaults,
  732. **options
  733. )
  734. def _deferred_blueprint_init(self, setup_state):
  735. """
  736. Synchronize prefix between blueprint/api and registration options, then
  737. perform initialization with setup_state.app :class:`flask.Flask` object.
  738. When a :class:`flask_restx.Api` object is initialized with a blueprint,
  739. this method is recorded on the blueprint to be run when the blueprint is later
  740. registered to a :class:`flask.Flask` object. This method also monkeypatches
  741. BlueprintSetupState.add_url_rule with _blueprint_setup_add_url_rule_patch.
  742. :param setup_state: The setup state object passed to deferred functions
  743. during blueprint registration
  744. :type setup_state: flask.blueprints.BlueprintSetupState
  745. """
  746. self.blueprint_setup = setup_state
  747. if setup_state.add_url_rule.__name__ != "_blueprint_setup_add_url_rule_patch":
  748. setup_state._original_add_url_rule = setup_state.add_url_rule
  749. setup_state.add_url_rule = MethodType(
  750. Api._blueprint_setup_add_url_rule_patch, setup_state
  751. )
  752. if not setup_state.first_registration:
  753. raise ValueError("flask-restx blueprints can only be registered once.")
  754. self._init_app(setup_state.app)
  755. def mediatypes_method(self):
  756. """Return a method that returns a list of mediatypes"""
  757. return lambda resource_cls: self.mediatypes() + [self.default_mediatype]
  758. def mediatypes(self):
  759. """Returns a list of requested mediatypes sent in the Accept header"""
  760. return [
  761. h
  762. for h, q in sorted(
  763. request.accept_mimetypes, key=operator.itemgetter(1), reverse=True
  764. )
  765. ]
  766. def representation(self, mediatype):
  767. """
  768. Allows additional representation transformers to be declared for the
  769. api. Transformers are functions that must be decorated with this
  770. method, passing the mediatype the transformer represents. Three
  771. arguments are passed to the transformer:
  772. * The data to be represented in the response body
  773. * The http status code
  774. * A dictionary of headers
  775. The transformer should convert the data appropriately for the mediatype
  776. and return a Flask response object.
  777. Ex::
  778. @api.representation('application/xml')
  779. def xml(data, code, headers):
  780. resp = make_response(convert_data_to_xml(data), code)
  781. resp.headers.extend(headers)
  782. return resp
  783. """
  784. def wrapper(func):
  785. self.representations[mediatype] = func
  786. return func
  787. return wrapper
  788. def unauthorized(self, response):
  789. """Given a response, change it to ask for credentials"""
  790. if self.serve_challenge_on_401:
  791. realm = current_app.config.get("HTTP_BASIC_AUTH_REALM", "flask-restx")
  792. challenge = '{0} realm="{1}"'.format("Basic", realm)
  793. response.headers["WWW-Authenticate"] = challenge
  794. return response
  795. def url_for(self, resource, **values):
  796. """
  797. Generates a URL to the given resource.
  798. Works like :func:`flask.url_for`.
  799. """
  800. endpoint = resource.endpoint
  801. if self.blueprint:
  802. endpoint = "{0}.{1}".format(self.blueprint.name, endpoint)
  803. return url_for(endpoint, **values)
  804. class SwaggerView(Resource):
  805. """Render the Swagger specifications as JSON"""
  806. def get(self):
  807. schema = self.api.__schema__
  808. return (
  809. schema,
  810. HTTPStatus.INTERNAL_SERVER_ERROR if "error" in schema else HTTPStatus.OK,
  811. )
  812. def mediatypes(self):
  813. return ["application/json"]
  814. def mask_parse_error_handler(error):
  815. """When a mask can't be parsed"""
  816. return {"message": "Mask parse error: {0}".format(error)}, HTTPStatus.BAD_REQUEST
  817. def mask_error_handler(error):
  818. """When any error occurs on mask"""
  819. return {"message": "Mask error: {0}".format(error)}, HTTPStatus.BAD_REQUEST