Skip to content

Commit b163513

Browse files
committed
Migrate to B2 native API v3
1 parent ec12aae commit b163513

File tree

9 files changed

+70
-36
lines changed

9 files changed

+70
-36
lines changed

b2sdk/_internal/raw_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
]
8080

8181
# API version number to use when calling the service
82-
API_VERSION = 'v2'
82+
API_VERSION = 'v3'
8383

8484
logger = getLogger(__name__)
8585

b2sdk/_internal/raw_simulator.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -1344,12 +1344,21 @@ def authorize_account(self, realm_url, application_key_id, application_key):
13441344
return dict(
13451345
accountId=key_sim.account_id,
13461346
authorizationToken=auth_token,
1347-
apiUrl=self.API_URL,
1348-
downloadUrl=self.DOWNLOAD_URL,
1349-
recommendedPartSize=self.MIN_PART_SIZE,
1350-
absoluteMinimumPartSize=self.MIN_PART_SIZE,
1351-
allowed=allowed,
1352-
s3ApiUrl=self.S3_API_URL,
1347+
apiInfo=dict(
1348+
groupsApi=dict(),
1349+
storageApi=dict(
1350+
apiUrl=self.API_URL,
1351+
downloadUrl=self.DOWNLOAD_URL,
1352+
recommendedPartSize=self.MIN_PART_SIZE,
1353+
absoluteMinimumPartSize=self.MIN_PART_SIZE,
1354+
allowed=allowed,
1355+
s3ApiUrl=self.S3_API_URL,
1356+
bucketId=allowed['bucketId'],
1357+
bucketName=allowed['bucketName'],
1358+
capabilities=allowed['capabilities'],
1359+
namePrefix=allowed['namePrefix'],
1360+
),
1361+
),
13531362
)
13541363

13551364
def cancel_large_file(self, api_url, account_auth_token, file_id):

b2sdk/_internal/session.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,16 @@ def authorize_account(self, realm, application_key_id, application_key):
115115
realm_url = REALM_URLS.get(realm, realm)
116116
response = self.raw_api.authorize_account(realm_url, application_key_id, application_key)
117117
account_id = response['accountId']
118-
allowed = response['allowed']
118+
storage_api_info = response['apiInfo']['storageApi']
119+
120+
# `allowed` object has been deprecated in the v3 of the API, but we still
121+
# construct it artificially to avoid changes in all the reliant parts.
122+
allowed = {
123+
'bucketId': storage_api_info['bucketId'],
124+
'bucketName': storage_api_info['bucketName'],
125+
'capabilities': storage_api_info['capabilities'],
126+
'namePrefix': storage_api_info['namePrefix'],
127+
}
119128

120129
# Clear the cache if new account has been used
121130
if not self.account_info.is_same_account(account_id, realm):
@@ -125,13 +134,13 @@ def authorize_account(self, realm, application_key_id, application_key):
125134
self.account_info.set_auth_data(
126135
account_id=account_id,
127136
auth_token=response['authorizationToken'],
128-
api_url=response['apiUrl'],
129-
download_url=response['downloadUrl'],
130-
absolute_minimum_part_size=response['absoluteMinimumPartSize'],
131-
recommended_part_size=response['recommendedPartSize'],
137+
api_url=storage_api_info['apiUrl'],
138+
download_url=storage_api_info['downloadUrl'],
139+
absolute_minimum_part_size=storage_api_info['absoluteMinimumPartSize'],
140+
recommended_part_size=storage_api_info['recommendedPartSize'],
132141
application_key=application_key,
133142
realm=realm,
134-
s3_api_url=response['s3ApiUrl'],
143+
s3_api_url=storage_api_info['s3ApiUrl'],
135144
allowed=allowed,
136145
application_key_id=application_key_id,
137146
)

b2sdk/v1/session.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,16 @@ def authorize_account(self, realm, application_key_id, application_key):
4949
realm_url = self.account_info.REALM_URLS.get(realm, realm)
5050
response = self.raw_api.authorize_account(realm_url, application_key_id, application_key)
5151
account_id = response['accountId']
52-
allowed = response['allowed']
52+
storage_api_info = response['apiInfo']['storageApi']
53+
54+
# `allowed` object has been deprecated in the v3 of the API, but we still
55+
# construct it artificially to avoid changes in all the reliant parts.
56+
allowed = {
57+
'bucketId': storage_api_info['bucketId'],
58+
'bucketName': storage_api_info['bucketName'],
59+
'capabilities': storage_api_info['capabilities'],
60+
'namePrefix': storage_api_info['namePrefix'],
61+
}
5362

5463
# Clear the cache if new account has been used
5564
if not self.account_info.is_same_account(account_id, realm):
@@ -59,12 +68,12 @@ def authorize_account(self, realm, application_key_id, application_key):
5968
self.account_info.set_auth_data(
6069
account_id=account_id,
6170
auth_token=response['authorizationToken'],
62-
api_url=response['apiUrl'],
63-
download_url=response['downloadUrl'],
64-
minimum_part_size=response['recommendedPartSize'],
71+
api_url=storage_api_info['apiUrl'],
72+
download_url=storage_api_info['downloadUrl'],
73+
minimum_part_size=storage_api_info['recommendedPartSize'],
6574
application_key=application_key,
6675
realm=realm,
67-
s3_api_url=response['s3ApiUrl'],
76+
s3_api_url=storage_api_info['s3ApiUrl'],
6877
allowed=allowed,
6978
application_key_id=application_key_id
7079
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Migrate to B2 Native API v3.

test/integration/test_raw_api.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@ def raw_api_test_helper(raw_api, should_cleanup_old_buckets):
117117
}
118118
missing_capabilities = (
119119
set(ALL_CAPABILITIES) - {'readBuckets', 'listAllBucketNames'} - preview_feature_caps -
120-
set(auth_dict['allowed']['capabilities'])
120+
set(auth_dict['apiInfo']['storageApi']['capabilities'])
121121
)
122122
assert not missing_capabilities, 'it appears that the raw_api integration test is being run with a non-full key. Missing capabilities: {}'.format(
123123
missing_capabilities,
124124
)
125125

126126
account_id = auth_dict['accountId']
127127
account_auth_token = auth_dict['authorizationToken']
128-
api_url = auth_dict['apiUrl']
129-
download_url = auth_dict['downloadUrl']
128+
api_url = auth_dict['apiInfo']['storageApi']['apiUrl']
129+
download_url = auth_dict['apiInfo']['storageApi']['downloadUrl']
130130

131131
# b2_create_key
132132
print('b2_create_key')
@@ -599,7 +599,7 @@ def raw_api_test_helper(raw_api, should_cleanup_old_buckets):
599599

600600

601601
def _subtest_bucket_notification_rules(raw_api, auth_dict, api_url, account_auth_token, bucket_id):
602-
if 'writeBucketNotifications' not in auth_dict['allowed']['capabilities']:
602+
if 'writeBucketNotifications' not in auth_dict['apiInfo']['storageApi']['capabilities']:
603603
pytest.skip('Test account does not have writeBucketNotifications capability')
604604

605605
notification_rule = {
@@ -645,8 +645,11 @@ def _subtest_bucket_notification_rules(raw_api, auth_dict, api_url, account_auth
645645
def cleanup_old_buckets():
646646
raw_api = B2RawHTTPApi(B2Http())
647647
auth_dict = authorize_raw_api(raw_api)
648+
648649
bucket_list_dict = raw_api.list_buckets(
649-
auth_dict['apiUrl'], auth_dict['authorizationToken'], auth_dict['accountId']
650+
auth_dict['apiInfo']['storageApi']['apiUrl'],
651+
auth_dict['authorizationToken'],
652+
auth_dict['accountId'],
650653
)
651654
_cleanup_old_buckets(raw_api, auth_dict, bucket_list_dict)
652655

@@ -659,7 +662,7 @@ def _cleanup_old_buckets(raw_api, auth_dict, bucket_list_dict):
659662
print('cleaning up old bucket: ' + bucket_name)
660663
_clean_and_delete_bucket(
661664
raw_api,
662-
auth_dict['apiUrl'],
665+
auth_dict['apiInfo']['storageApi']['apiUrl'],
663666
auth_dict['authorizationToken'],
664667
auth_dict['accountId'],
665668
bucket_id,

test/integration/test_upload.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_ssec_key_id(self):
5757

5858
auth_dict = authorize_raw_api(raw_api)
5959
account_auth_token = auth_dict['authorizationToken']
60-
api_url = auth_dict['apiUrl']
60+
api_url = auth_dict['apiInfo']['storageApi']['apiUrl']
6161
bucket = self.create_bucket()
6262

6363
large_info = raw_api.start_large_file(

test/unit/fixtures/raw_api.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@
1919
def fake_b2_raw_api_responses():
2020
return {
2121
'authorize_account': {
22-
'absoluteMinimumPartSize': 5000000,
2322
'accountId': '6012deadbeef',
24-
'allowed': {
25-
'bucketId': None,
26-
'bucketName': None,
27-
'capabilities': copy(ALL_CAPABILITIES),
28-
'namePrefix': None,
23+
'apiInfo': {
24+
'groupsApi': {},
25+
'storageApi': {
26+
'bucketId': None,
27+
'bucketName': None,
28+
'capabilities': copy(ALL_CAPABILITIES),
29+
'namePrefix': None,
30+
'downloadUrl': 'https://f000.backblazeb2.xyz:8180',
31+
'absoluteMinimumPartSize': 5000000,
32+
'recommendedPartSize': 100000000,
33+
'apiUrl': 'https://api000.backblazeb2.xyz:8180',
34+
's3ApiUrl': 'https://s3.us-west-000.backblazeb2.xyz:8180',
35+
},
2936
},
30-
'apiUrl': 'https://api000.backblazeb2.xyz:8180',
3137
'authorizationToken': '4_1111111111111111111111111_11111111_111111_1111_1111111111111_1111_11111111=',
32-
'downloadUrl': 'https://f000.backblazeb2.xyz:8180',
33-
'recommendedPartSize': 100000000,
34-
's3ApiUrl': 'https://s3.us-west-000.backblazeb2.xyz:8180',
3538
}
3639
} # yapf: disable
3740

test/unit/v_all/test_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,4 @@ def test_get_download_url_for_fileid(self):
164164

165165
download_url = self.api.get_download_url_for_fileid('file-id')
166166

167-
assert download_url == 'http://download.example.com/b2api/v2/b2_download_file_by_id?fileId=file-id'
167+
assert download_url == 'http://download.example.com/b2api/v3/b2_download_file_by_id?fileId=file-id'

0 commit comments

Comments
 (0)