__init__.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. """
  3. oauthlib.oauth2.rfc6749
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. This module is an implementation of various logic needed
  6. for consuming and providing OAuth 2.0 RFC6749.
  7. """
  8. from __future__ import absolute_import, unicode_literals
  9. import functools
  10. import logging
  11. from .errors import TemporarilyUnavailableError, ServerError
  12. from .errors import FatalClientError, OAuth2Error
  13. log = logging.getLogger(__name__)
  14. class BaseEndpoint(object):
  15. def __init__(self):
  16. self._available = True
  17. self._catch_errors = False
  18. @property
  19. def available(self):
  20. return self._available
  21. @available.setter
  22. def available(self, available):
  23. self._available = available
  24. @property
  25. def catch_errors(self):
  26. return self._catch_errors
  27. @catch_errors.setter
  28. def catch_errors(self, catch_errors):
  29. self._catch_errors = catch_errors
  30. def catch_errors_and_unavailability(f):
  31. @functools.wraps(f)
  32. def wrapper(endpoint, uri, *args, **kwargs):
  33. if not endpoint.available:
  34. e = TemporarilyUnavailableError()
  35. log.info('Endpoint unavailable, ignoring request %s.' % uri)
  36. return {}, e.json, 503
  37. if endpoint.catch_errors:
  38. try:
  39. return f(endpoint, uri, *args, **kwargs)
  40. except OAuth2Error:
  41. raise
  42. except FatalClientError:
  43. raise
  44. except Exception as e:
  45. error = ServerError()
  46. log.warning(
  47. 'Exception caught while processing request, %s.' % e)
  48. return {}, error.json, 500
  49. else:
  50. return f(endpoint, uri, *args, **kwargs)
  51. return wrapper