|
1
|
|
|
""" |
|
2
|
|
|
Copyright George Sibble 2018 |
|
3
|
|
|
""" |
|
4
|
|
|
|
|
5
|
1 |
|
import datetime |
|
6
|
1 |
|
from hashlib import sha1 |
|
7
|
|
|
|
|
8
|
1 |
|
import requests |
|
9
|
1 |
|
from requests.auth import HTTPBasicAuth |
|
10
|
1 |
|
from requests.adapters import HTTPAdapter |
|
11
|
1 |
|
from requests.packages.urllib3.util.retry import Retry |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
1 |
|
import requests_cache |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
from b2blaze.b2_exceptions import ( |
|
16
|
|
|
B2Exception, |
|
17
|
|
|
B2AuthorizationError, |
|
18
|
|
|
B2InvalidRequestType, |
|
19
|
|
|
) |
|
20
|
|
|
|
|
21
|
|
|
from b2blaze.utilities import b2_url_encode, get_content_length, StreamWithHashProgress |
|
22
|
|
|
|
|
23
|
|
|
from .api import BASE_URL, API_VERSION, API |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
# TODO: Fix this quick hack |
|
|
|
|
|
|
27
|
|
|
requests_cache.install_cache("backblaze_cache", backend="redis", expire_after=180) |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
def requests_retry_session( |
|
|
|
|
|
|
31
|
|
|
retries=3, |
|
|
|
|
|
|
32
|
|
|
backoff_factor=0.3, |
|
|
|
|
|
|
33
|
|
|
status_forcelist=(408, 500, 501, 502, 503, 504), |
|
|
|
|
|
|
34
|
|
|
session=None, |
|
|
|
|
|
|
35
|
|
|
): |
|
36
|
|
|
session = session or requests.Session() |
|
37
|
|
|
retry = Retry( |
|
38
|
|
|
total=retries, |
|
39
|
|
|
read=retries, |
|
40
|
|
|
connect=retries, |
|
41
|
|
|
backoff_factor=backoff_factor, |
|
42
|
|
|
status_forcelist=status_forcelist, |
|
43
|
|
|
) |
|
44
|
|
|
adapter = HTTPAdapter(max_retries=retry) |
|
45
|
|
|
session.mount("http://", adapter) |
|
46
|
|
|
session.mount("https://", adapter) |
|
47
|
|
|
return session |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
class B2Connector(object): |
|
|
|
|
|
|
51
|
|
|
""" |
|
52
|
|
|
|
|
53
|
|
|
""" |
|
54
|
|
|
|
|
55
|
|
|
def __init__(self, key_id, application_key): |
|
56
|
|
|
""" |
|
57
|
|
|
|
|
58
|
|
|
:param key_id: |
|
59
|
|
|
:param application_key: |
|
60
|
|
|
""" |
|
61
|
|
|
self.key_id = key_id |
|
62
|
|
|
self.application_key = application_key |
|
63
|
|
|
self.account_id = None |
|
64
|
|
|
self.auth_token = None |
|
65
|
|
|
self.authorized_at = None |
|
66
|
|
|
self.api_url = None |
|
67
|
|
|
self.download_url = None |
|
68
|
|
|
self.recommended_part_size = None |
|
69
|
|
|
# TODO: Part Size |
|
|
|
|
|
|
70
|
|
|
self._authorize() |
|
71
|
|
|
|
|
72
|
|
|
@property |
|
73
|
|
|
def authorized(self): |
|
74
|
|
|
""" |
|
75
|
|
|
|
|
76
|
|
|
:return: |
|
77
|
|
|
""" |
|
78
|
|
|
if self.auth_token is None: |
|
79
|
|
|
return False |
|
80
|
|
|
else: |
|
81
|
|
|
if (datetime.datetime.utcnow() - self.authorized_at) > datetime.timedelta( |
|
82
|
|
|
hours=23 |
|
|
|
|
|
|
83
|
|
|
): |
|
84
|
|
|
self._authorize() |
|
85
|
|
|
return True |
|
86
|
|
|
|
|
87
|
|
|
def _authorize(self): |
|
88
|
|
|
""" |
|
89
|
|
|
|
|
90
|
|
|
:return: |
|
91
|
|
|
""" |
|
92
|
|
|
path = BASE_URL + API.authorize |
|
93
|
|
|
|
|
94
|
|
|
result = requests_retry_session().get( |
|
95
|
|
|
path, auth=HTTPBasicAuth(self.key_id, self.application_key) |
|
96
|
|
|
) |
|
97
|
|
|
if result.status_code == 200: |
|
98
|
|
|
result_json = result.json() |
|
99
|
|
|
self.authorized_at = datetime.datetime.utcnow() |
|
100
|
|
|
self.account_id = result_json["accountId"] |
|
101
|
|
|
self.auth_token = result_json["authorizationToken"] |
|
102
|
|
|
self.api_url = result_json["apiUrl"] + API_VERSION |
|
103
|
|
|
self.download_url = ( |
|
104
|
|
|
result_json["downloadUrl"] + API_VERSION + API.download_file_by_id |
|
105
|
|
|
) |
|
106
|
|
|
self.recommended_part_size = result_json["recommendedPartSize"] |
|
107
|
|
|
else: |
|
108
|
|
|
raise B2Exception.parse(result) |
|
109
|
|
|
|
|
110
|
|
|
def make_request( |
|
|
|
|
|
|
111
|
|
|
self, path, method="get", headers={}, params={}, account_id_required=False |
|
|
|
|
|
|
112
|
|
|
): |
|
113
|
|
|
""" |
|
114
|
|
|
|
|
115
|
|
|
:param path: |
|
116
|
|
|
:param method: |
|
117
|
|
|
:param headers: |
|
118
|
|
|
:param params: |
|
119
|
|
|
:param account_id_required: |
|
120
|
|
|
:return: |
|
121
|
|
|
""" |
|
122
|
|
|
headers.update({"Authorization": self.auth_token}) |
|
123
|
|
|
|
|
124
|
|
|
if self.authorized: |
|
125
|
|
|
url = self.api_url + path |
|
126
|
|
|
if method == "get": |
|
127
|
|
|
return requests_retry_session().get(url, headers=headers) |
|
128
|
|
|
elif method == "post": |
|
129
|
|
|
if account_id_required: |
|
130
|
|
|
params.update({"accountId": self.account_id}) |
|
131
|
|
|
headers.update({"Content-Type": "application/json"}) |
|
132
|
|
|
return requests_retry_session().post(url, json=params, headers=headers) |
|
133
|
|
|
else: |
|
134
|
|
|
raise B2InvalidRequestType("Request type must be get or post") |
|
135
|
|
|
else: |
|
136
|
|
|
raise B2AuthorizationError("Unknown Error") |
|
137
|
|
|
|
|
138
|
|
|
def upload_file( |
|
|
|
|
|
|
139
|
|
|
self, |
|
|
|
|
|
|
140
|
|
|
file_contents, |
|
|
|
|
|
|
141
|
|
|
file_name, |
|
|
|
|
|
|
142
|
|
|
upload_url, |
|
|
|
|
|
|
143
|
|
|
auth_token, |
|
|
|
|
|
|
144
|
|
|
direct=False, |
|
|
|
|
|
|
145
|
|
|
mime_content_type=None, |
|
|
|
|
|
|
146
|
|
|
content_length=None, |
|
|
|
|
|
|
147
|
|
|
progress_listener=None, |
|
|
|
|
|
|
148
|
|
|
): |
|
149
|
|
|
""" |
|
150
|
|
|
|
|
151
|
|
|
:param file_contents: |
|
152
|
|
|
:param file_name: |
|
153
|
|
|
:param upload_url: |
|
154
|
|
|
:param auth_token: |
|
155
|
|
|
:param mime_content_type: |
|
156
|
|
|
:param content_length |
|
157
|
|
|
:param progress_listener |
|
158
|
|
|
:return: |
|
159
|
|
|
""" |
|
160
|
|
|
if hasattr(file_contents, "read"): |
|
161
|
|
|
if content_length is None: |
|
162
|
|
|
content_length = get_content_length(file_contents) |
|
163
|
|
|
file_sha = "hex_digits_at_end" |
|
164
|
|
|
data = StreamWithHashProgress( |
|
165
|
|
|
stream=file_contents, progress_listener=progress_listener |
|
166
|
|
|
) |
|
167
|
|
|
content_length += data.hash_size() |
|
168
|
|
|
else: |
|
169
|
|
|
if content_length is None: |
|
170
|
|
|
content_length = len(file_contents) |
|
171
|
|
|
file_sha = sha1(file_contents).hexdigest() |
|
172
|
|
|
data = file_contents |
|
173
|
|
|
|
|
174
|
|
|
headers = { |
|
175
|
|
|
"Content-Type": mime_content_type or "b2/x-auto", |
|
176
|
|
|
"Content-Length": str(content_length), |
|
177
|
|
|
"X-Bz-Content-Sha1": file_sha, |
|
178
|
|
|
"X-Bz-File-Name": b2_url_encode(file_name), |
|
179
|
|
|
"Authorization": auth_token, |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return requests_retry_session().post(upload_url, headers=headers, data=data) |
|
183
|
|
|
|
|
184
|
|
|
def upload_part( |
|
|
|
|
|
|
185
|
|
|
self, |
|
|
|
|
|
|
186
|
|
|
file_contents, |
|
|
|
|
|
|
187
|
|
|
content_length, |
|
|
|
|
|
|
188
|
|
|
part_number, |
|
|
|
|
|
|
189
|
|
|
upload_url, |
|
|
|
|
|
|
190
|
|
|
auth_token, |
|
|
|
|
|
|
191
|
|
|
progress_listener=None, |
|
|
|
|
|
|
192
|
|
|
): |
|
193
|
|
|
""" |
|
194
|
|
|
|
|
195
|
|
|
:param file_contents: |
|
196
|
|
|
:param content_length: |
|
197
|
|
|
:param part_number: |
|
198
|
|
|
:param upload_url: |
|
199
|
|
|
:param auth_token: |
|
200
|
|
|
:param progress_listener: |
|
201
|
|
|
:return: |
|
202
|
|
|
""" |
|
203
|
|
|
file_sha = "hex_digits_at_end" |
|
204
|
|
|
data = StreamWithHashProgress( |
|
205
|
|
|
stream=file_contents, progress_listener=progress_listener |
|
206
|
|
|
) |
|
207
|
|
|
content_length += data.hash_size() |
|
208
|
|
|
|
|
209
|
|
|
headers = { |
|
210
|
|
|
"Content-Length": str(content_length), |
|
211
|
|
|
"X-Bz-Content-Sha1": file_sha, |
|
212
|
|
|
"X-Bz-Part-Number": str(part_number), |
|
213
|
|
|
"Authorization": auth_token, |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return requests_retry_session().post(upload_url, headers=headers, data=data) |
|
217
|
|
|
|
|
218
|
|
|
def download_file(self, file_id): |
|
219
|
|
|
""" |
|
220
|
|
|
|
|
221
|
|
|
:param file_id: |
|
222
|
|
|
:return: |
|
223
|
|
|
""" |
|
224
|
|
|
url = self.download_url |
|
225
|
|
|
params = {"fileId": file_id} |
|
226
|
|
|
headers = {"Authorization": self.auth_token} |
|
227
|
|
|
|
|
228
|
|
|
return requests_retry_session().get(url, headers=headers, params=params) |
|
229
|
|
|
|