1
|
|
|
""" |
2
|
|
|
Copyright George Sibble 2018 |
3
|
|
|
""" |
4
|
1 |
|
import requests |
5
|
1 |
|
import datetime |
|
|
|
|
6
|
1 |
|
from requests.auth import HTTPBasicAuth |
7
|
1 |
|
from b2blaze.b2_exceptions import B2Exception, B2AuthorizationError, B2InvalidRequestType |
8
|
1 |
|
import sys |
|
|
|
|
9
|
1 |
|
from hashlib import sha1 |
|
|
|
|
10
|
1 |
|
from b2blaze.utilities import b2_url_encode, decode_error, get_content_length, StreamWithHashProgress |
|
|
|
|
11
|
1 |
|
from .api import BASE_URL, API_VERSION, API |
12
|
|
|
|
13
|
1 |
|
class B2Connector(object): |
|
|
|
|
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
|
1 |
|
self.api_session = None |
32
|
|
|
#TODO: Part Size |
|
|
|
|
33
|
1 |
|
self._authorize() |
34
|
|
|
|
35
|
|
|
|
36
|
1 |
|
@property |
37
|
|
|
def authorized(self): |
38
|
|
|
""" |
39
|
|
|
|
40
|
|
|
:return: |
41
|
|
|
""" |
42
|
1 |
|
if self.auth_token is None: |
43
|
|
|
return False |
44
|
|
|
else: |
45
|
1 |
|
if (datetime.datetime.utcnow() - self.authorized_at) > datetime.timedelta(hours=23): |
46
|
|
|
self._authorize() |
47
|
1 |
|
return True |
48
|
|
|
|
49
|
|
|
|
50
|
1 |
|
def _authorize(self): |
51
|
|
|
""" |
52
|
|
|
|
53
|
|
|
:return: |
54
|
|
|
""" |
55
|
1 |
|
path = BASE_URL + API.authorize |
56
|
|
|
|
57
|
1 |
|
result = requests.get(path, auth=HTTPBasicAuth(self.key_id, self.application_key)) |
58
|
1 |
|
if result.status_code == 200: |
59
|
1 |
|
result_json = result.json() |
60
|
1 |
|
self.authorized_at = datetime.datetime.utcnow() |
61
|
1 |
|
self.account_id = result_json['accountId'] |
62
|
1 |
|
self.auth_token = result_json['authorizationToken'] |
63
|
1 |
|
self.api_url = result_json['apiUrl'] + API_VERSION |
64
|
1 |
|
self.download_url = result_json['downloadUrl'] + API_VERSION + API.download_file_by_id |
65
|
1 |
|
self.recommended_part_size = result_json['recommendedPartSize'] |
66
|
1 |
|
self.api_session = requests.Session() |
67
|
1 |
|
self.api_session.headers.update({ |
68
|
|
|
'Authorization': self.auth_token |
69
|
|
|
}) |
70
|
|
|
else: |
71
|
|
|
raise B2Exception.parse(result) |
72
|
|
|
|
73
|
|
|
|
74
|
1 |
|
def make_request(self, path, method='get', headers={}, params={}, account_id_required=False): |
|
|
|
|
75
|
|
|
""" |
76
|
|
|
|
77
|
|
|
:param path: |
78
|
|
|
:param method: |
79
|
|
|
:param headers: |
80
|
|
|
:param params: |
81
|
|
|
:param account_id_required: |
82
|
|
|
:return: |
83
|
|
|
""" |
84
|
1 |
|
if self.authorized: |
85
|
1 |
|
url = self.api_url + path |
86
|
1 |
|
if method == 'get': |
87
|
|
|
return self.api_session.get(url, headers=headers) |
88
|
1 |
|
elif method == 'post': |
89
|
1 |
|
if account_id_required: |
90
|
1 |
|
params.update({ |
91
|
|
|
'accountId': self.account_id |
92
|
|
|
}) |
93
|
1 |
|
headers.update({ |
94
|
|
|
'Content-Type': 'application/json' |
95
|
|
|
}) |
96
|
1 |
|
return self.api_session.post(url, json=params, headers=headers) |
97
|
|
|
else: |
98
|
|
|
raise B2InvalidRequestType('Request type must be get or post') |
99
|
|
|
else: |
100
|
|
|
raise B2AuthorizationError('Unknown Error') |
101
|
|
|
|
102
|
1 |
|
def upload_file(self, file_contents, file_name, upload_url, auth_token, |
|
|
|
|
103
|
|
|
direct=False, mime_content_type=None, content_length=None, progress_listener=None): |
|
|
|
|
104
|
|
|
""" |
105
|
|
|
|
106
|
|
|
:param file_contents: |
107
|
|
|
:param file_name: |
108
|
|
|
:param upload_url: |
109
|
|
|
:param auth_token: |
110
|
|
|
:param mime_content_type: |
111
|
|
|
:param content_length |
112
|
|
|
:param progress_listener |
113
|
|
|
:return: |
114
|
|
|
""" |
115
|
1 |
|
if hasattr(file_contents, 'read'): |
116
|
1 |
|
if content_length is None: |
117
|
1 |
|
content_length = get_content_length(file_contents) |
118
|
1 |
|
file_sha = 'hex_digits_at_end' |
119
|
1 |
|
data = StreamWithHashProgress(stream=file_contents, progress_listener=progress_listener) |
120
|
1 |
|
content_length += data.hash_size() |
121
|
|
|
else: |
122
|
1 |
|
if content_length is None: |
123
|
1 |
|
content_length = len(file_contents) |
124
|
1 |
|
file_sha = sha1(file_contents).hexdigest() |
125
|
1 |
|
data = file_contents |
126
|
|
|
|
127
|
1 |
|
headers = { |
128
|
|
|
'Content-Type': mime_content_type or 'b2/x-auto', |
129
|
|
|
'Content-Length': str(content_length), |
130
|
|
|
'X-Bz-Content-Sha1': file_sha, |
131
|
|
|
'X-Bz-File-Name': b2_url_encode(file_name), |
132
|
|
|
'Authorization': auth_token |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
return requests.post(upload_url, headers=headers, data=data) |
136
|
|
|
|
137
|
1 |
|
def upload_part(self, file_contents, content_length, part_number, upload_url, auth_token, progress_listener=None): |
|
|
|
|
138
|
|
|
""" |
139
|
|
|
|
140
|
|
|
:param file_contents: |
141
|
|
|
:param content_length: |
142
|
|
|
:param part_number: |
143
|
|
|
:param upload_url: |
144
|
|
|
:param auth_token: |
145
|
|
|
:param progress_listener: |
146
|
|
|
:return: |
147
|
|
|
""" |
148
|
|
|
file_sha = 'hex_digits_at_end' |
149
|
|
|
data = StreamWithHashProgress(stream=file_contents, progress_listener=progress_listener) |
150
|
|
|
content_length += data.hash_size() |
151
|
|
|
|
152
|
|
|
headers = { |
153
|
|
|
'Content-Length': str(content_length), |
154
|
|
|
'X-Bz-Content-Sha1': file_sha, |
155
|
|
|
'X-Bz-Part-Number': str(part_number), |
156
|
|
|
'Authorization': auth_token |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return requests.post(upload_url, headers=headers, data=data) |
160
|
|
|
|
161
|
1 |
|
def download_file(self, file_id): |
162
|
|
|
""" |
163
|
|
|
|
164
|
|
|
:param file_id: |
165
|
|
|
:return: |
166
|
|
|
""" |
167
|
1 |
|
url = self.download_url |
168
|
1 |
|
params = { |
169
|
|
|
'fileId': file_id |
170
|
|
|
} |
171
|
1 |
|
headers = { |
172
|
|
|
'Authorization': self.auth_token |
173
|
|
|
} |
174
|
|
|
|
175
|
1 |
|
return requests.get(url, headers=headers, params=params) |
176
|
|
|
|
|
|
|
|
177
|
|
|
|