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

Benjamin Mako Hill || Want to submit a patch?