Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / oauthlib / oauth2 / rfc6749 / __init__.py @ 564

History | View | Annotate | Download (1.69 KB)

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

    
18
log = logging.getLogger(__name__)
19

    
20

    
21
class BaseEndpoint(object):
22

    
23
    def __init__(self):
24
        self._available = True
25
        self._catch_errors = False
26

    
27
    @property
28
    def available(self):
29
        return self._available
30

    
31
    @available.setter
32
    def available(self, available):
33
        self._available = available
34

    
35
    @property
36
    def catch_errors(self):
37
        return self._catch_errors
38

    
39
    @catch_errors.setter
40
    def catch_errors(self, catch_errors):
41
        self._catch_errors = catch_errors
42

    
43

    
44
def catch_errors_and_unavailability(f):
45
    @functools.wraps(f)
46
    def wrapper(endpoint, uri, *args, **kwargs):
47
        if not endpoint.available:
48
            e = TemporarilyUnavailableError()
49
            log.info('Endpoint unavailable, ignoring request %s.' % uri)
50
            return {}, e.json, 503
51

    
52
        if endpoint.catch_errors:
53
            try:
54
                return f(endpoint, uri, *args, **kwargs)
55
            except OAuth2Error:
56
                raise
57
            except FatalClientError:
58
                raise
59
            except Exception as e:
60
                error = ServerError()
61
                log.warning(
62
                    'Exception caught while processing request, %s.' % e)
63
                return {}, error.json, 500
64
        else:
65
            return f(endpoint, uri, *args, **kwargs)
66
    return wrapper