GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Pull Request — master (#29)
by
unknown
02:03
created

b2blaze.connector   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Test Coverage

Coverage 39.19%

Importance

Changes 0
Metric Value
eloc 90
dl 0
loc 173
ccs 29
cts 74
cp 0.3919
rs 10
c 0
b 0
f 0
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A B2Connector.download_file() 0 15 1
A B2Connector.authorized() 0 12 3
A B2Connector.upload_file() 0 34 4
A B2Connector._authorize() 0 18 2
A B2Connector.upload_part() 0 23 1
A B2Connector.make_request() 0 29 5
A B2Connector.__init__() 0 16 1
1
"""
2
Copyright George Sibble 2018
3
"""
4 1
import requests
5 1
import datetime
0 ignored issues
show
introduced by
standard import "import datetime" should be placed before "import requests"
Loading history...
6 1
from requests.auth import HTTPBasicAuth
7 1
from b2blaze.b2_exceptions import B2Exception, B2AuthorizationError, B2InvalidRequestType
8 1
import sys
0 ignored issues
show
Unused Code introduced by
The import sys seems to be unused.
Loading history...
introduced by
standard import "import sys" should be placed before "import requests"
Loading history...
9 1
from hashlib import sha1
0 ignored issues
show
introduced by
standard import "from hashlib import sha1" should be placed before "import requests"
Loading history...
10 1
from b2blaze.utilities import b2_url_encode, decode_error, get_content_length, StreamWithHashProgress
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Unused Code introduced by
Unused decode_error imported from b2blaze.utilities
Loading history...
11 1
from .api import BASE_URL, API_VERSION, API
12
13 1
class B2Connector(object):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
best-practice introduced by
Too many instance attributes (8/7)
Loading history...
14
    """
15
16
    """
17 1
    def __init__(self, key_id, application_key):
18
        """
19
20
        :param key_id:
21
        :param application_key:
22
        """
23 1
        self.key_id = key_id
24 1
        self.application_key = application_key
25 1
        self.account_id = None
26 1
        self.auth_token = None
27 1
        self.authorized_at = None
28 1
        self.api_url = None
29 1
        self.download_url = None
30 1
        self.recommended_part_size = None
31
        #TODO:  Part Size
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
32 1
        self._authorize()
33
34
35 1
    @property
36
    def authorized(self):
37
        """
38
39
        :return:
40
        """
41
        if self.auth_token is None:
42
            return False
43
        else:
44
            if (datetime.datetime.utcnow() - self.authorized_at) > datetime.timedelta(hours=23):
45
                self._authorize()
46
            return True
47
48
49 1
    def _authorize(self):
50
        """
51
52
        :return:
53
        """
54 1
        path = BASE_URL + API.authorize
55
56 1
        result = requests.get(path, auth=HTTPBasicAuth(self.key_id, self.application_key))
57 1
        if result.status_code == 200:
58
            result_json = result.json()
59
            self.authorized_at = datetime.datetime.utcnow()
60
            self.account_id = result_json['accountId']
61
            self.auth_token = result_json['authorizationToken']
62
            self.api_url = result_json['apiUrl'] + API_VERSION
63
            self.download_url = result_json['downloadUrl'] + API_VERSION + API.download_file_by_id
64
            self.recommended_part_size = result_json['recommendedPartSize']
65
        else:
66 1
            raise B2Exception.parse(result)
67
68
69 1
    def make_request(self, path, method='get', headers={}, params={}, account_id_required=False):
0 ignored issues
show
Bug Best Practice introduced by
The default value {} might cause unintended side-effects.

Objects as default values are only created once in Python and not on each invocation of the function. If the default object is modified, this modification is carried over to the next invocation of the method.

# Bad:
# If array_param is modified inside the function, the next invocation will
# receive the modified object.
def some_function(array_param=[]):
    # ...

# Better: Create an array on each invocation
def some_function(array_param=None):
    array_param = array_param or []
    # ...
Loading history...
best-practice introduced by
Too many arguments (6/5)
Loading history...
70
        """
71
72
        :param path:
73
        :param method:
74
        :param headers:
75
        :param params:
76
        :param account_id_required:
77
        :return:
78
        """
79
        headers.update({'Authorization': self.auth_token})
80
81
        if self.authorized:
82
            url = self.api_url + path
83
            if method == 'get':
84
                return requests.get(url, headers=headers)
85
            elif method == 'post':
86
                if account_id_required:
87
                    params.update({
88
                        'accountId': self.account_id
89
                    })
90
                headers.update({
91
                    'Content-Type': 'application/json'
92
                })
93
                return requests.post(url, json=params, headers=headers)
94
            else:
95
                raise B2InvalidRequestType('Request type must be get or post')
96
        else:
97
            raise B2AuthorizationError('Unknown Error')
98
99 1
    def upload_file(self, file_contents, file_name, upload_url, auth_token,
0 ignored issues
show
best-practice introduced by
Too many arguments (9/5)
Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
100
                    direct=False, mime_content_type=None, content_length=None, progress_listener=None):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Unused Code introduced by
The argument direct seems to be unused.
Loading history...
101
        """
102
103
        :param file_contents:
104
        :param file_name:
105
        :param upload_url:
106
        :param auth_token:
107
        :param mime_content_type:
108
        :param content_length
109
        :param progress_listener
110
        :return:
111
        """
112
        if hasattr(file_contents, 'read'):
113
            if content_length is None:
114
                content_length = get_content_length(file_contents)
115
            file_sha = 'hex_digits_at_end'
116
            data = StreamWithHashProgress(stream=file_contents, progress_listener=progress_listener)
117
            content_length += data.hash_size()
118
        else:
119
            if content_length is None:
120
                content_length = len(file_contents)
121
            file_sha = sha1(file_contents).hexdigest()
122
            data = file_contents
123
124
        headers = {
125
            'Content-Type': mime_content_type or 'b2/x-auto',
126
            'Content-Length': str(content_length),
127
            'X-Bz-Content-Sha1': file_sha,
128
            'X-Bz-File-Name': b2_url_encode(file_name),
129
            'Authorization': auth_token
130
        }
131
132
        return requests.post(upload_url, headers=headers, data=data)
133
134 1
    def upload_part(self, file_contents, content_length, part_number, upload_url, auth_token, progress_listener=None):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (118/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
best-practice introduced by
Too many arguments (7/5)
Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
135
        """
136
137
        :param file_contents:
138
        :param content_length:
139
        :param part_number:
140
        :param upload_url:
141
        :param auth_token:
142
        :param progress_listener:
143
        :return:
144
        """
145
        file_sha = 'hex_digits_at_end'
146
        data = StreamWithHashProgress(stream=file_contents, progress_listener=progress_listener)
147
        content_length += data.hash_size()
148
149
        headers = {
150
            'Content-Length': str(content_length),
151
            'X-Bz-Content-Sha1': file_sha,
152
            'X-Bz-Part-Number': str(part_number),
153
            'Authorization': auth_token
154
        }
155
156
        return requests.post(upload_url, headers=headers, data=data)
157
158 1
    def download_file(self, file_id):
159
        """
160
161
        :param file_id:
162
        :return:
163
        """
164
        url = self.download_url
165
        params = {
166
            'fileId': file_id
167
        }
168
        headers = {
169
            'Authorization': self.auth_token
170
        }
171
172
        return requests.get(url, headers=headers, params=params)
173
0 ignored issues
show
coding-style introduced by
Trailing newlines
Loading history...
174