Passed
Push — develop ( 1e512e...b44848 )
by Dean
02:45
created

UpdateOAuthCredential   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 30.76%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
dl 0
loc 23
ccs 4
cts 13
cp 0.3076
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A from_dict() 0 6 2
A from_pin() 0 13 2
1 1
from plugin.managers.core.base import Manager, Update
2 1
from plugin.models import TraktBasicCredential, TraktOAuthCredential
3
4 1
from trakt import Trakt
5 1
import inspect
6 1
import logging
7
8 1
log = logging.getLogger(__name__)
9
10
11 1
class UpdateBasicCredential(Update):
12 1
    keys = ['password', 'token']
13
14 1
    def from_dict(self, basic_credential, changes, save=True):
15
        # Resolve `basic_credential`
16
        if inspect.isfunction(basic_credential):
17
            basic_credential = basic_credential()
18
19
        # Request new token on credential changes
20
        if 'username' in changes or 'password' in changes:
21
            # Retrieve credentials
22
            username = changes.get('username', basic_credential.account.username)
23
            password = changes.get('password', basic_credential.password)
24
25
            # Retrieve new token
26
            changes['token'] = Trakt['auth'].login(username, password)
27
28
        # Update `TraktBasicCredential`
29
        if not super(UpdateBasicCredential, self).from_dict(basic_credential, changes, save=save):
30
            return False
31
32
        return True
33
34
35 1
class TraktBasicCredentialManager(Manager):
36 1
    update = UpdateBasicCredential
37
38 1
    model = TraktBasicCredential
39
40 1
    @classmethod
41
    def delete(cls, *query, **kwargs):
42
        # Retrieve basic credential
43
        try:
44
            credential = cls.get(*query, **kwargs)
45
        except Exception as ex:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
46
            log.warn('Unable to find basic credential (query: %r, kwargs: %r): %r', query, kwargs, ex)
47
            return False
48
49
        # Clear basic credential
50
        cls.update(credential, {
51
            'password': None,
52
53
            'token': None
54
        })
55
56
        return True
57
58
59 1
class UpdateOAuthCredential(Update):
60 1
    keys = ['code', 'access_token', 'refresh_token', 'created_at', 'expires_in', 'token_type', 'scope']
61
62 1
    def from_dict(self, oauth_credential, changes, save=True):
63
        # Update `TraktOAuthCredential`
64
        if not super(UpdateOAuthCredential, self).from_dict(oauth_credential, changes, save=save):
65
            return False
66
67
        return True
68
69 1
    def from_pin(self, oauth, pin, save=True):
70
        data = {'code': pin}
71
72
        # Exchange `pin` for token authorization
73
        authorization = Trakt['oauth'].token_exchange(pin, 'urn:ietf:wg:oauth:2.0:oob')
74
75
        if authorization:
76
            data.update(authorization)
77
        else:
78
            log.warn('Token exchange failed for %r', oauth.account)
79
80
        # Update `OAuthCredential`
81
        return self(oauth, data, save=save)
82
83
84 1
class TraktOAuthCredentialManager(Manager):
85 1
    update = UpdateOAuthCredential
86
87 1
    model = TraktOAuthCredential
88
89 1
    @classmethod
90
    def delete(cls, *query, **kwargs):
91
        # Retrieve oauth credential
92
        try:
93
            credential = cls.get(*query, **kwargs)
94
        except Exception as ex:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
95
            log.warn('Unable to find oauth credential (query: %r, kwargs: %r): %r', query, kwargs, ex)
96
            return False
97
98
        # Clear oauth credential
99
        cls.update(credential, {
100
            'code': None,
101
102
            'access_token': None,
103
            'refresh_token': None,
104
105
            'created_at': None,
106
            'expires_in': None,
107
108
            'token_type': None,
109
            'scope': None
110
        })
111
112
        return True
113