Service.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
crap 2
1
from plugin.api.core.exceptions import ApiError
2
from plugin.api.core.manager import ApiManager
3
from plugin.core.helpers import decorator
4
5
import logging
6
7
log = logging.getLogger(__name__)
8
9
10
class AuthenticationRequiredError(ApiError):
11
    code = 'authentication.required'
12
    message = 'API authentication required'
13
14
15
class ServiceMeta(type):
16
    def __init__(cls, name, bases, attributes):
17
        super(ServiceMeta, cls).__init__(name, bases, attributes)
18
19
        if '__metaclass__' in attributes:
20
            return
21
22
        # Register API service
23
        ApiManager.register(cls)
24
25
26
class Service(object):
27
    __metaclass__ = ServiceMeta
28
    __key__ = None
29
30
    manager = None
31
32
    def __init__(self, manager):
33
        self.manager = manager
34
35
    @property
36
    def context(self):
37
        return self.manager.context
38
39
40
@decorator.wraps
41
def expose(authenticated=True):
42
    def outer(func):
43
        def inner(self, *args, **kwargs):
44
            if authenticated and self.context.token is None:
45
                raise AuthenticationRequiredError
46
47
            return func(self, *args, **kwargs)
48
49
        # Attach meta to wrapper
50
        inner.__meta__ = {
51
            'authenticated': authenticated,
52
            'exposed': True
53
        }
54
55
        return inner
56
57
    return outer
58